/
routes.py
876 lines (760 loc) · 37.7 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
from flask import Flask, flash, render_template, request, session, redirect, url_for
import numpy as np
from csv import reader
import datetime
from dateutil import parser
from forms import SignupForm, LoginForm, DemandForm, BidForm, ApplicantApprovalForm, BecomeUserForm, JustifyDeveloperChoiceForm, ProtestForm, ProtestApprovalForm, SubmitSystemForm, RatingForm,RatingMessageForm, TransactionApprovalForm, DeleteAccountForm, DeleteAccountApprovalForm, AddFundsForm, EditProfileForm, validate_user_id, validate_email
from models import User, Client, Developer, Applicant, Demand, Bid, BlacklistedUser, SuperUser, SystemWarning, Notification, Rating, Transaction, DeleteRequest
import helpers
app = Flask(__name__)
app.secret_key = 'development-key'
@app.route("/")
def index():
number_of_clients = Client.get_number_of_clients()
number_of_developers = Developer.get_number_of_developers()
# clients with the most projects
top_clients = Client.get_clients_with_most_projects()
# developers making the most money
top_devs = Developer.get_top_earners()
return render_template("index.html",number_of_clients=number_of_clients,
number_of_developers=number_of_developers,
top_clients = top_clients,
top_devs = top_devs)
@app.route("/dashboard")
def dashboard():
"""
The '/dashboard' route directs a user to view their dashboard.
"""
if 'username' in session:
if not session['type_of_user'] == "user":
return render_template("access_denied.html")
info = User.get_user_info(session['username'])
if (info == None):
return render_template("dashboard.html", first_name=" ")
first_name = info['first_name']
# Get notifications for this user.
unread = Notification.get_number_of_unread(session['username'])
notifications = Notification.get_notif_to_recipient(session['username'], 5)
# If the user has no projects in history, they are a new user.
user_type = User.get_user_info(session['username'])['type_of_user']
recs = {"client_rec_des": "Most Active Clients",
"dev_rec_des": "Most Active Developers",
"client_rec": Client.get_most_active_clients(),
"dev_rec": Developer.get_most_active_developers()}
if user_type == 'client':
if Client.get_info(session['username'])['num_of_completed_projects'] > 0:
recs = {"client_rec_des": "Clients with Similar Interests",
"dev_rec_des": "Developers with Similar Interests",
"client_rec": Client.get_similar_clients(session['username']),
"dev_rec": Developer.get_similar_developers(session['username'])}
elif user_type == 'developer':
if Developer.get_info(session['username'])['num_of_completed_projects'] > 0:
recs = {"client_rec_des": "Clients with Similar Interests",
"dev_rec_des": "Developers with Similar Interests",
"client_rec": Client.get_similar_clients(session['username']),
"dev_rec": Developer.get_similar_developers(session['username'])}
return render_template("dashboard.html", first_name=first_name, notifications=notifications,
recs=recs, unread=unread)
else:
return redirect(url_for('login'))
@app.route("/dashboard/projects")
def my_projects():
"""
The '/dashboard/projects' route directs a user to view their projects.
"""
if 'username' in session:
if not session['type_of_user'] == "user":
return render_template("access_denied.html")
user_type = User.get_user_info(session['username'])['type_of_user']
current = list(Demand.get_info(x) for x in Demand.get_current_projects(session['username']))
mid = []
completed = []
if user_type == "developer":
bids_by_username = Bid.get_bids_by_username(session['username'])
temp = []
for i in bids_by_username:
info = Bid.get_info(i)['demand_id']
if info not in temp:
temp.append(info)
mid = list(Demand.get_info(y) for y in temp)
completed = list(Demand.get_info(x) for x in Developer.get_past_projects(session['username']))
else:
temp = (Demand.get_info(x) for x in Demand.get_filtered_demands(None, None, session['username'], None, None, None, True))
for demand in temp:
if demand['chosen_developer_username'] is np.nan:
mid.append(demand)
completed = list(Demand.get_info(x) for x in Client.get_past_projects(session['username']))
return render_template("myProjects.html", user_type = user_type, current=current, mid=mid, completed=completed)
else:
return redirect(url_for('login'))
@app.route("/dashboard/transactions")
def view_transactions():
"""
Transaction history page for user's dashboard.
"""
if 'username' not in session:
return redirect(url_for('login'))
else:
if not session['type_of_user'] == "user":
return render_template("access_denied.html")
balance = 0
if session['type_of_user'] == "developer":
balance = Developer.get_info(session['username'])["balance"]
else:
balance = Client.get_info(session['username'])["balance"]
outgoing = Transaction.get_transactions_by_sender(session['username'])
incoming = Transaction.get_transactions_by_recipient(session['username'])
return render_template("myTransactions.html", balance=balance, outgoing=outgoing, incoming=incoming)
@app.route("/dashboard/transactions/add", methods =["GET", "POST"])
def add_funds():
"""
Allows the user to add funds to their account.
"""
if 'username' not in session:
return redirect(url_for('login'))
else:
if session['type_of_user'] == "user":
cc = str(User.get_user_info(session['username'])["credit_card"])
cc = "******" + cc[-4:]
balance = 0
if session['type_of_user'] == "developer":
balance = Developer.get_info(session['username'])["balance"]
else:
balance = Client.get_info(session['username'])["balance"]
form = AddFundsForm()
if request.method == "GET":
return render_template("addFunds.html", cc=cc,form=form, balance=balance, added=False)
elif request.method == "POST":
if form.amount.validate(form):
if session['type_of_user'] == "developer":
Developer.add_earnings(session['username'], form.amount.data)
balance = Developer.get_info(session['username'])["balance"]
else:
Client.add_to_balance(session['username'], form.amount.data)
balance = Client.get_info(session['username'])["balance"]
return render_template("addFunds.html",cc=cc, form=form, balance=balance, added=True)
else:
return render_template("addFunds.html", cc=cc, form=form, balance=balance, added=False)
else:
return render_template("access_denied.html")
@app.route("/dashboard/notifications")
def view_notifications():
"""
The '/dashboard/notifications' route directs a user to view their notifications.
"""
if 'username' in session:
if session['type_of_user'] == 'user':
unread = Notification.get_number_of_unread(session['username'])
notifications = Notification.get_all_notif_to_recipient(session['username'])
return render_template('notifications.html', notifications=notifications, unread=unread)
else:
return render_template('access_denied.html')
else:
return redirect(url_for('login'))
@app.route("/dashboard_applicant", methods=["GET", "POST"])
def dashboard_applicant():
"""
The 'dashboard_applicant' route directs an applicant to their dashboard.
They can view the status of their application here.
"""
if session['username']:
form = BecomeUserForm()
info = Applicant.get_applicant_info(session['username'])
if session['type_of_user'] == 'applicant' and request.method == 'GET':
return render_template("dashboard_applicant.html", info=info, form=form)
if session['type_of_user'] == 'applicant' and request.method == 'POST':
info = Applicant.get_applicant_info(session['username'])
if form.use_prev_credentials.data == 'yes':
User.use_old_credentials(info['user_id'],info['email'])
session['type_of_user'] = 'user'
session['role'] = info['type_of_user']
# Create a new client or developer in database depending on type of user
if info['type_of_user'] == 'client':
Client(info['user_id'])
elif info['type_of_user'] == 'developer':
Developer(info['user_id'])
return redirect(url_for('dashboard'))
elif form.validate():
User.set_credentials(form.username.data,form.password.data,info['email'])
session['username'] = form.username.data
session['type_of_user'] = 'user'
session['role'] = info['type_of_user']
# Create a new client or developer in database depending on type of user
if info['type_of_user'] == 'client':
Client(form.username.data)
elif info['type_of_user'] == 'developer':
Developer(form.username.data)
return redirect(url_for('dashboard'))
else:
flash('Login credentials are invalid. Please check that all fields are filled correctly.')
return render_template("dashboard_applicant.html", info=info, form=form)
elif session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
elif session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
else:
return render_template("index.html")
@app.route("/dashboard_superuser")
def dashboard_superuser():
"""
The 'dashboard_superuser' route directs a superuser to their dashboard.
"""
if session['username']:
if not session['type_of_user'] == "superuser":
return render_template("access_denied.html")
info = SuperUser.get_superuser_info(session['username'])
pending_applicants = Applicant.get_pending_applicants()
protests = SystemWarning.get_protests()
pending_transactions = Transaction.get_pending_transactions()
pending_delete_requests = DeleteRequest.get_pending_delete_requests()
return render_template("dashboard_superuser.html",
info=info,
pending_applicants=pending_applicants,
protests=protests,
pending_transactions=pending_transactions,
pending_delete_requests=pending_delete_requests)
else:
return render_template("index.html")
@app.route("/browse")
def browse():
"""
The '/browse' route directs anyone on the website to a page where they can browse the
demands in the system.
"""
start_date = request.args.get('start_date', default=None, type=str)
end_date = request.args.get('end_date', default=None, type=str)
client = request.args.get('client', default=None, type=str)
client_rating = None
for i in range(1,5):
if request.args.get('rating' + str(i)) == 'on':
client_rating = i
break
tags = request.args.get('tags', default=None, type=str)
min_bid = request.args.get('min_bid', default=None, type=float)
active = request.args.get('show_active', default=False)
demands = Demand.get_filtered_demands(start_date=start_date,
end_date=end_date,
client=client,
client_rating=client_rating,
tags=tags,
min_bid=min_bid,
active=active)
demands_info = []
for demand in demands:
demands_info.append(Demand.get_info(demand))
return render_template("browse.html", demands_info=demands_info)
@app.route("/user/<name>")
def user(name):
"""
The '/user/<name>' route directs a user to the profile page of the user with the username
of [name].
"""
# if User.has_user_id(name):
# get basic info
info = User.get_user_info(name)
if info['type_of_user'] == 'client':
rating = Client.get_info(name)['avg_rating']
projects = Client.get_projects_posted(name)
elif info['type_of_user'] == 'developer':
rating = Developer.get_info(name)['avg_rating']
projects = Developer.get_past_projects(name)
projects_info = []
for demand_id in projects:
projects_info.append(Demand.get_info(demand_id))
# round rating to the nearest 0.5
rating = round(0.5 * round(float(rating) / 0.5), 1)
has_half_star = rating % 1 == .5
return render_template("profile.html", info=info, rating=int(rating), half_star=has_half_star, projects=projects_info)
@app.route("/apply", methods=["GET", "POST"])
def apply():
"""
The '/apply route directs a user that is not logged in to the application page.
"""
# If the user is logged into the system, direct them to their dashboard
if 'username' in session:
if session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
if session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
form = SignupForm()
if request.method == 'POST':
if form.validate():
new_user = Applicant(form.role.data, form.first_name.data, form.last_name.data, form.email.data, form.phone.data,
form.credit_card.data, form.user_id.data, form.password.data)
session['username'] = form.user_id.data
session['type_of_user'] = 'applicant'
session['role'] = form.role.data
return redirect(url_for('dashboard_applicant'))
else:
flash('Applicant submission is invalid. Please check that all fields are filled correctly.')
return render_template('application.html', form=form)
elif request.method == 'GET':
return render_template('application.html', form=form)
@app.route("/login", methods=["GET", "POST"])
def login():
"""
The '/login' route directs the user to the login page if they are not already logged in.
"""
# If the user is logged into the system, direct them to their dashboard
if 'username' in session:
if session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
if session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
form = LoginForm()
if request.method == 'POST' and form.validate():
username = form.username.data
password = form.password.data
# Check if username exists and if password matches
if BlacklistedUser.is_blacklisted(username):
session['username'] = username
return redirect(url_for('blacklist'))
if User.check_password(username, password):
session['username'] = username
session['role'] = User.get_user_info(username)['type_of_user']
session['type_of_user'] = 'user'
if SystemWarning.should_be_blacklisted(username):
BlacklistedUser(username)
return redirect(url_for('blacklist'))
return redirect(url_for('dashboard'))
if Applicant.check_password(username, password):
session['username'] = username
session['type_of_user'] = 'applicant'
return redirect(url_for('dashboard_applicant'))
if SuperUser.check_password(username, password):
session['username'] = username
session['type_of_user'] = 'superuser'
return redirect(url_for('dashboard_superuser'))
# If username or password is invalid, notify user
else:
flash('Invalid username or password.')
return render_template('login.html', form=form)
elif request.method == 'GET':
return render_template('login.html', form=form)
return render_template('login.html', form=form)
@app.route("/logout")
def logout():
"""
The '/logout' route will remove the user from the current session if there is one.
"""
session.pop('username', None)
session.pop('role', None)
return redirect(url_for('index'))
@app.route("/editProfile", methods=['GET', 'POST'])
def editProfile():
"""
The '/editProfile' route will direct a logged in user to edit their profile
"""
if 'username' not in session:
return redirect(url_for('index'))
if session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
form = EditProfileForm()
username = session['username']
info = User.get_user_info(username)
if request.method == 'GET':
return render_template('editProfile.html', form=form, info=info)
elif request.method == 'POST':
if form.email.data != info['email'] and not Applicant.is_unique_email(form.email.data):
flash('Email is taken. Please choose another email')
return render_template('editProfile.html', form=form, info=info)
else:
User.set_email(username, form.email.data)
if form.validate():
if len(form.password.data) > 0:
User.set_password(username, form.password.data)
User.set_first_name(username, form.first_name.data)
User.set_last_name(username, form.last_name.data)
User.set_phone(username, form.phone.data)
User.set_about(username, form.about.data)
if len(form.resume.data) > 0:
User.set_resume(username, form.resume.data)
if len(form.interests.data) > 0:
User.set_interests(username, form.interests.data)
if len(form.portfolio.data) > 0:
User.set_portfolio(username, form.portfolio.data)
return redirect(url_for('user', name=username))
else:
return render_template('editProfile.html', form=form, info=info)
@app.route("/blacklist")
def blacklist():
if 'username' in session:
info = BlacklistedUser.get_info(session['username'])
return render_template('blacklist.html', info=info)
return redirect(url_for('index'))
@app.route("/warnings/<warning_id>/protest", methods=['GET', 'POST'])
def protestWarning(warning_id):
"""
The '/warnings/<warning_id>/protest' route directs the user to a page that allows them to protest
a warning
"""
# Only allow access to protest warning if regular user
warning_id = int(warning_id)
if session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
elif session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
# User can only protest warning if they are the recipient of the warning and it is an active warning
if session['username'] != SystemWarning.get_warned_user(warning_id) or SystemWarning.get_warning_status(warning_id)!='active':
return redirect(url_for('dashboard'))
form = ProtestForm()
if request.method == 'GET':
return render_template("protestWarning.html", form=form, warning_id=warning_id)
else:
if form.validate():
SystemWarning.protest_warning(warning_id,form.reason.data)
return redirect(url_for('dashboard'))
else:
return render_template('protestWarning.html', form=form, warning_id=warning_id)
@app.route("/bid/<demand_id>", methods=['GET', 'POST'])
def bidInfo(demand_id):
"""
The '/bid/<demand_id>' route directs a user to the page with complete
specifications for the demand.
"""
demand_info = Demand.get_info(demand_id)
client_info = User.get_user_info(demand_info['client_username'])
bids = Bid.get_bids_for_demand(demand_id)
bids_info = []
bidders_info = {}
if (len(bids) > 0):
lowest_bid = Bid.get_info(bids[0])['bid_amount']
else:
lowest_bid = 'None'
for bid in bids:
info = Bid.get_info(bid)
bids_info.append(info)
if info['developer_username'] not in bidders_info:
bidders_info[info['developer_username']] = User.get_user_info(info['developer_username'])
form = BidForm()
if request.method == 'POST':
if form.validate():
Bid(demand_id, session['username'], form.bid_amount.data)
return redirect(url_for('bidInfo', demand_id=demand_id))
else:
return redirect(url_for('bidInfo', demand_id=demand_id))
elif request.method == 'GET':
return render_template("bidPage.html", demand_info=demand_info, client_info=client_info, bids_info=bids_info, bidders_info=bidders_info, lowest_bid=lowest_bid, form=form, demand_id=demand_id)
@app.route('/bid/<demand_id>/choose-developer', methods=['GET', 'POST'])
def choose_developer(demand_id):
"""
The '/bid/<demand_id>/choose-developer' route directs a client to a page
where he/she can select the developer he/she wants to hire to implement the
system that was demanded.
"""
demand_info = Demand.get_info(demand_id)
bids = Bid.get_bids_for_demand(demand_id)
bids_info = []
bidders_info = {}
for bid in bids:
info = Bid.get_info(bid)
bids_info.append(info)
if info['developer_username'] not in bidders_info:
username = info['developer_username']
bidders_info[username] = User.get_user_info(username)
bidders_info[username]['lowest_bid'] = info['bid_amount']
rating = Developer.get_info(username)['avg_rating']
# round rating to the nearest 0.5
rating = round(0.5 * round(float(rating) / 0.5), 1)
bidders_info[username]['full_stars'] = int(rating)
bidders_info[username]['has_half_star'] = rating % 1 == .5
if request.method == 'POST':
chosen_developer = request.form['developer']
session['chosen_developer'] = request.form['developer']
# if the chosen developer had the lowest bid,
# update the demand's chosen developer
if chosen_developer == bids_info[0]['developer_username']:
# updates the table, notifies the developer, and also starts the transaction request
Demand.choose_developer(demand_id, chosen_developer, session['username'], bids_info[0]['bid_amount'])
return render_template("developer_chosen.html")
# if the chosen developer did not have the lowest bid,
# the client must provide a reason for choosing this developer
else:
return redirect(url_for('justify_developer_choice', demand_id=demand_id))
if request.method == 'GET':
return render_template("choose_developer.html", demand_id=demand_id, bidders_info=bidders_info)
@app.route("/bid/<demand_id>/justify-developer", methods=['GET', 'POST'])
def justify_developer_choice(demand_id):
"""
The '/bid/<demand_id>/justify-developer' route is where the client fills out a form
to explain his/her reason for choosing a developer who did not offer the lowest bid.
"""
bids = Bid.get_bids_for_demand(demand_id)
bids_info = []
bidders_info = {}
for bid in bids:
info = Bid.get_info(bid)
bids_info.append(info)
if info['developer_username'] not in bidders_info:
username = info['developer_username']
bidders_info[username] = User.get_user_info(username)
bidders_info[username]['lowest_bid'] = info['bid_amount']
form = JustifyDeveloperChoiceForm()
if request.method == 'POST':
if form.validate():
Demand.choose_developer(demand_id, session['chosen_developer'], session['username'], bidders_info[session['chosen_developer']]['lowest_bid'], form.reason.data)
return render_template("developer_chosen.html")
else:
return render_template("justify_developer_choice.html", demand_id=demand_id, form=form)
if request.method == 'GET':
return render_template("justify_developer_choice.html", demand_id=demand_id, form=form)
@app.route("/bid/<demand_id>/upload-system", methods=['GET', 'POST'])
def upload_system(demand_id):
"""
The '/bid/<demand_id>/upload-system' route is where the developer can upload the system that
they have created for a demand they have been chosen for.
"""
form = SubmitSystemForm()
demand_info = Demand.get_info(demand_id)
client = demand_info['client_username']
if request.method == 'POST':
if form.validate():
# will not actually store the file
# project is now completed
Developer.submit_system(demand_id, session['username'])
return redirect(url_for('rating', demand_id=demand_id, recipient=client))
else:
return render_template("upload_system.html", demand_id=demand_id, form=form)
if request.method == 'GET':
return render_template("upload_system.html", demand_id=demand_id, form=form)
@app.route("/bid/<demand_id>/rating/<recipient>", methods=["GET", "POST"])
def rating(demand_id, recipient):
"""
The '/bid/<demand_id>/rating/<recipient>' route is where the user can rate another user
for a demand they were involved in.
"""
if 'username' not in session:
return redirect(url_for('login'))
demand_info = Demand.get_info(demand_id)
# make sure the user is authorized to rate the recipient
if session['role'] == 'developer':
# developer rates the client, so client is recipient
if session['username'] != demand_info['chosen_developer_username']:
return render_template('access_denied.html')
elif session['role'] == 'client':
# client rates the developer, so developer is recipient
if session['username'] != demand_info['client_username']:
return render_template('access_denied.html')
if Rating.check_if_valid_rating_form(int(demand_id), recipient, session['username']):
form = RatingForm()
if request.method == "GET":
return render_template("rating.html", form=form, recipient=recipient, demand_id=demand_id)
elif request.method == "POST":
# low rating
if form.rating.data <= 2:
session['rating'+demand_id] = form.rating.data
return redirect(url_for('ratingMessage', demand_id=demand_id, recipient=recipient))
elif form.rating.data == None:
return render_template('rating.html', form=form, recipient=recipient, demand_id=demand_id)
else:
# add to form data
Rating(demand_id, recipient, session['username'], form.rating.data)
# if the client gave a good rating to a developer (<= 3)
# the remaining half of the bid amount gets transferred over to the developer
if session['role'] == 'client':
bid_amount = Demand.get_info(demand_id)['chosen_bid_amount']
Transaction(recipient, session['username'], round(bid_amount / 2, 2))
# update developer's earnings
Developer.add_earnings(recipient, bid_amount)
return render_template('ratingFinished.html', recipient=recipient)
return render_template('access_denied.html')
@app.route("/bid/<demand_id>/rating/<recipient>/message", methods=["GET", "POST"])
def ratingMessage(demand_id, recipient):
"""
The '/bid/<demand_id>/rating/<recipient>/message' route directs the user to explain their
rating if it is below a certain amount.
"""
if 'username' not in session:
return redirect(url_for('login'))
if 'username' in session and ('rating'+demand_id) in session:
form = RatingMessageForm()
if request.method == "GET":
return render_template("ratingMessage.html", form=form, demand_id=demand_id, recipient=recipient)
elif request.method == "POST":
if form.message.validate(form):
Rating(demand_id, recipient, session['username'], session['rating'+demand_id],form.message.data)
del session['rating'+demand_id]
return render_template('ratingFinished.html', recipient=recipient)
else:
return render_template("ratingMessage.html", form=form, demand_id=demand_id, recipient=recipient)
return render_template('access_denied.html')
@app.route("/createDemand", methods=['GET', 'POST'])
def createDemand():
"""
The '/createDemand' route directs a client to the form where he/she can
create and post a demand on the Turk System.
"""
if 'username' not in session:
return redirect(url_for('login'))
if session['role'] != 'client':
return render_template('access_denied.html')
form = DemandForm()
if request.method == 'POST':
if form.validate():
format = '%m-%d-%Y %I:%M %p'
dt_bid = form.bidding_deadline.data.strftime(format)
dt_submit = form.submission_deadline.data.strftime(format)
Demand(session['username'], form.title.data, form.tags.data,
form.specifications.data, dt_bid, dt_submit)
new_demand_id = Demand.get_most_recent_demand_id()
return redirect(url_for('bidInfo', demand_id=new_demand_id))
else:
return render_template('createDemand.html', form=form)
elif request.method == 'GET':
return render_template('createDemand.html', form=form)
@app.route("/applicant_approval/<applicant_id>", methods=["GET", "POST"])
def applicant_approval(applicant_id):
"""
The '/applicant_approval/<applicant_id>' route directs a superuser to approve an application with
the id of [applicant_id].
"""
if session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
form = ApplicantApprovalForm()
info = Applicant.get_applicant_info(applicant_id)
if request.method == 'GET':
return render_template("applicant_approval.html", applicant_id=applicant_id, info=info, form=form)
if request.method == 'POST':
if form.decision.data == 'approve':
Applicant.approve(applicant_id)
return redirect(url_for('dashboard_superuser'))
else:
if form.validate():
Applicant.reject(applicant_id,form.reason.data)
return redirect(url_for('dashboard_superuser'))
else:
flash('Approval form is invalid. Please make sure all fields are completed correctly')
return render_template("applicant_approval.html", applicant_id=applicant_id, info=info, form=form)
@app.route("/protest_approval/<warning_id>", methods=["GET", "POST"])
def protest_approval(warning_id):
"""
The '/protest_approval/<warning_id>' route directs a superuser to approve a protest against a warning
with the id of [warning_id].
"""
if session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
warning_id = int(warning_id)
info = SystemWarning.get_warning_info(warning_id)
username = info['warned_user']
type_of_user = User.get_user_info(username)['type_of_user']
avg_rating = 0
if type_of_user == 'client':
avg_rating = Client.get_info(username)['avg_rating']
elif type_of_user == 'developer':
avg_rating = Developer.get_info(username)['avg_rating']
form = ProtestApprovalForm()
if request.method == 'GET':
return render_template("protestApproval.html", warning_id=warning_id, info=info, form=form, avg_rating=avg_rating)
if request.method == 'POST':
if form.validate():
if form.decision.data == 'remove':
SystemWarning.remove_warning(warning_id)
Notification(username,session['username'],'Your protest for warning#'+ str(warning_id) +' was approved. Your warning has been deleted.')
else:
SystemWarning.keep_warning(warning_id)
Notification(username,session['username'],'Your protest for warning#'+ str(warning_id) +' was not approved. Your warning remains.')
return redirect(url_for('dashboard_superuser'))
else:
return render_template("protestApproval.html", warning_id=warning_id, info=info, form=form, avg_rating=avg_rating)
@app.route("/transaction_approval/<transaction_id>", methods=["GET", "POST"])
def transaction_approval(transaction_id):
"""
The '/transaction_approval/<transaction_id>' route directs a superuser to approve a transaction with
the id of [transaction_id].
"""
if 'username' not in session:
return redirect(url_for('login'))
if session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
form = TransactionApprovalForm()
info = Transaction.get_transaction_info(transaction_id)
transaction_id = int(transaction_id)
if request.method == 'GET':
enough_money = User.does_user_have_enough_money(info['sender'],int(info['amount']))
return render_template("transactionApproval.html", form=form, transaction_id=transaction_id,info=info,enough_money=enough_money)
if request.method == 'POST':
if form.validate():
if form.decision.data == 'approve':
Transaction.approve_transaction(transaction_id)
Notification(info['sender'],session['username'],'Your transaction (Transaction#'+ str(transaction_id) +') was approved.')
else:
Transaction.deny_transaction(transaction_id)
Notification(info['sender'],session['username'],'Your transaction (Transaction#'+ str(transaction_id) +') was denied.')
return redirect(url_for('dashboard_superuser'))
else:
return render_template("protestApproval.html", warning_id=warning_id, info=info, form=form, avg_rating=avg_rating)
@app.route("/warnings")
def warning():
"""
The '/warnings' route directs a user to view all their warnings.
"""
if 'username' not in session:
return redirect(url_for('login'))
if session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
username = session['username']
warnings = SystemWarning.get_user_warnings(username)
return render_template("warnings.html", warnings=warnings)
@app.route("/deleteAccount", methods=["GET", "POST"])
def deleteAccount():
"""
The '/deleteAccount' route directs a user to a form where they can request the superuser
to delete their account.
"""
if 'username' not in session:
return redirect(url_for('login'))
if session['type_of_user'] == 'superuser':
return redirect(url_for('dashboard_superuser'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
form = DeleteAccountForm()
if request.method == 'GET':
return render_template("deleteAccount.html",form=form)
elif request.method == 'POST':
if form.delete.data:
DeleteRequest(session['username'])
return redirect(url_for('dashboard'))
elif form.cancel.data:
return redirect(url_for('dashboard'))
@app.route("/delete_acount_approval/<delete_request_id>", methods=["GET", "POST"])
def delete_account_approval(delete_request_id):
"""
The '/delete_acount_approval' route directs a superuser to a form where they can approve/deny an account deletion request.
"""
if 'username' not in session:
return redirect(url_for('login'))
if session['type_of_user'] == 'user':
return redirect(url_for('dashboard'))
if session['type_of_user'] == 'applicant':
return redirect(url_for('dashboard_applicant'))
form = DeleteAccountApprovalForm()
delete_request_id = int(delete_request_id)
info = DeleteRequest.get_delete_request_info(delete_request_id)
if request.method == 'GET':
return render_template("deleteAccountApproval.html", form=form,info=info)
if request.method == 'POST':
if form.validate():
if form.decision.data == 'approve':
DeleteRequest.approve_delete_request(delete_request_id)
else:
DeleteRequest.deny_delete_request(delete_request_id)
Notification(info['username'],session['username'],'Your delete account request (ID#'+ str(info['delete_request_id']) +') was denied.')
return redirect(url_for('dashboard_superuser'))
else:
return render_template("deleteAccountApproval.html", form=form,info=info)
if __name__ == "__main__":
app.run(debug=True)