-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
989 lines (778 loc) · 26.7 KB
/
views.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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
"""
This module contains the view functions for handling HTTP requests and rendering HTML templates for the web application.
These functions define the behavior of various routes in the application,
including user authentication, account management, department and service management,
order handling, and contact form submission.
Each function serves a specific route defined in the Flask application, processing incoming requests,
performing necessary database operations, and rendering corresponding HTML templates to provide users with a dynamic web experience.
For more details on each function, refer to their respective docstrings.
Author: Yousef Saeed
"""
from flask import (
abort,
request,
send_from_directory,
session,
render_template,
redirect,
url_for,
flash,
)
from flask_babel import _, lazy_gettext
from sqlalchemy import func
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename
from markdown import markdown
from datetime import datetime, timedelta
from humanize import naturaltime
from uuid import uuid4
from os import path
from models import *
def get_locale():
"""
Get the locale based on various sources.
Checks if the language query parameter is set and valid.
If set, it returns the language from the query parameter.
If not set via query, checks if the language is stored in the session.
If found, returns the stored language. Otherwise, it uses the browser's preferred language.
Returns:
str: The selected locale.
"""
# Check if the language query parameter is set and valid
if "lang" in request.args:
lang = request.args.get("lang")
if lang in ["en", "ar"]:
session["lang"] = lang
return session["lang"]
# If not set via query, check if we have it stored in the session
elif "lang" in session:
return session.get("lang")
# Otherwise, use the browser's preferred language
return request.accept_languages.best_match(["en", "ar"])
babel = Babel(app, locale_selector=get_locale)
@app.context_processor
def inject_babel():
return dict(_=lazy_gettext)
@app.context_processor
def inject_locale():
return {"get_locale": get_locale}
@app.context_processor
def inject_current_locale():
return {"current_locale": get_locale()}
@app.before_request
def clear_trailing():
"""
Remove trailing slashes from URL paths before processing.
If a URL path ends with a '/', this function redirects to the same URL without the trailing '/'.
Returns:
redirect: A redirect to the URL without the trailing slash.
"""
rp = request.path
if rp != "/" and rp.endswith("/"):
return redirect(rp[:-1])
@app.route("/setlang")
def setlang():
"""
Set the language for the session.
Gets the language from the query parameters and sets it as the session language.
Returns:
redirect: A redirect to the referrer page.
"""
lang = request.args.get("lang", "en")
session["lang"] = lang
return redirect(request.referrer)
@app.route("/files/<path:filename>")
def serve_file(filename):
"""
Serve files from the upload directory.
Args:
filename (str): The name of the file to be served.
Returns:
send_from_directory: Sends the file from the upload directory.
"""
return send_from_directory(app.config["UPLOAD_DIRECTORY"], filename)
@app.route("/")
def index():
"""
Render the index page with relevant data.
Returns:
render_template: Renders the index.html template with relevant data.
"""
most_ordered_services = (
db.session.query(Service)
.join(Order, Service.id == Order.service_id)
.group_by(Service.id)
.order_by(func.count(Order.id).desc())
.limit(6)
.all()
)
end_date = datetime.now()
start_date = end_date - timedelta(days=7)
total_orders = Order.query.count()
orders_last_week = Order.query.filter(
Order.start_date
>= start_date.replace(hour=0, minute=0, second=0, microsecond=0),
Order.start_date < end_date.replace(hour=0, minute=0, second=0, microsecond=0),
).count()
total_users = User.query.count()
total_services = Service.query.count()
total_department = Department.query.count()
return render_template(
"index.html",
services=most_ordered_services,
Department=Department,
total_orders=total_orders,
orders_yesterday=orders_last_week,
total_users=total_users,
total_services=total_services,
total_department=total_department,
)
@app.route("/register", methods=["GET", "POST"])
def register():
"""
Handle user registration and account creation.
Returns:
render_template: Renders the register.html template with relevant data.
"""
logout()
msg = ""
if (
request.method == "POST"
and "id" in request.form
and "password" in request.form
and "confirm-password" in request.form
and "name" in request.form
and "phone" in request.form
and "email" in request.form
and "address" in request.form
):
id = request.form["id"]
password = request.form["password"]
confirm_password = request.form["confirm-password"]
name = request.form["name"]
phone = request.form["phone"]
email = request.form["email"]
address = request.form["address"]
user = User.query.filter_by(id=id).first()
if user:
msg = _("UserAlreadyExists")
elif len(id) != 14 or not id.isdigit():
msg = _("IncorrectIDFormat")
elif len(password) < 6 or len(password) > 28:
msg = _("InvalidPasswordLength")
elif confirm_password != password:
msg = _("PasswordsDoNotMatch")
elif (
not id
or not password
or not confirm_password
or not name
or not phone
or not email
or not address
):
msg = _("EmptyFields")
else:
hashed_password = generate_password_hash(password)
new_user = User(
id=id,
password=hashed_password,
name=name,
phone=phone,
email=email,
address=address,
is_admin=0,
)
db.session.add(new_user)
db.session.commit()
user = User.query.filter_by(id=id).first()
session["loggedin"] = True
session["id"] = user.id
session["is_admin"] = user.is_admin
msg = _("AccountCreated")
return redirect(url_for("index"))
elif request.method == "POST":
msg = _("FillForm")
return render_template("register.html", msg=msg)
@app.route("/login", methods=["GET", "POST"])
def login():
"""
Handle user login and session creation.
Returns:
render_template: Renders the login.html template with relevant data.
"""
logout()
msg = ""
if request.method == "POST" and "id" in request.form and "password" in request.form:
id = request.form["id"]
password = request.form["password"]
user = User.query.filter_by(id=id).first()
if not user:
msg = _("AccountNotFound")
elif not id or not password:
msg = _("EmptyFields")
elif user:
if check_password_hash(user.password, password):
session["loggedin"] = True
session["id"] = user.id
session["is_admin"] = user.is_admin
msg = _("LoginSuccess")
return redirect(url_for("index"))
else:
msg = _("IncorrectPassword")
elif request.method == "POST":
msg = _("FillForm")
return render_template("login.html", msg=msg)
@app.route("/logout")
def logout():
"""
Log out the current user and clear their session.
Returns:
redirect: Redirects to the index page after logging out.
"""
session.pop("loggedin", None)
session.pop("id", None)
session.pop("is_admin", None)
return redirect(url_for("index"))
@app.route("/profile/<id>", methods=["GET", "POST"])
def profile(id):
"""
Render user profile page.
Args:
id (str): The ID of the user.
Returns:
render_template: Renders the profile.html template with relevant data.
"""
if "loggedin" not in session:
abort(403)
elif id == session["id"] or session["is_admin"] == True:
viewer = "current_user" if session["id"] == id else "admin"
user = User.query.filter_by(id=id).first()
if request.method == "POST" and request.form["action"] == "logout":
return logout()
return render_template("profile.html", user=user, viewer=viewer)
else:
abort(403)
@app.route("/account", methods=["GET", "POST"])
def account():
"""
Handle user account information management.
This function handles updating user information and password, as well as deleting user account.
Returns:
render_template: Renders the account.html template with relevant data.
"""
if "loggedin" not in session:
abort(401)
user = User.query.filter_by(id=session["id"]).first()
msg = ""
if (
request.method == "POST"
and "fullname" in request.form
and "phone" in request.form
and "email" in request.form
and "address" in request.form
and request.form["action"] == "update_info"
):
user.name = request.form["fullname"]
user.phone = request.form["phone"]
user.email = request.form["email"]
user.address = request.form["address"]
db.session.commit()
msg = _("AccountInfoUpdated")
elif (
request.method == "POST"
and "old_password" in request.form
and "new_password" in request.form
and "confirm_password" in request.form
and request.form["action"] == "update_password"
):
old_password = request.form["old_password"]
new_password = request.form["new_password"]
confirm_password = request.form["confirm_password"]
if check_password_hash(user.password, old_password):
if len(new_password) < 6 or len(new_password) > 28:
msg = _("PasswordLengthError")
elif confirm_password != new_password:
msg = _("PasswordsDoNotMatchError")
elif new_password == old_password:
msg = _("SamePasswordError")
else:
hashed_password = generate_password_hash(new_password)
user.password = hashed_password
db.session.commit()
msg = _("PasswordUpdated")
else:
msg = _("IncorrectPasswordError")
elif (
request.method == "POST"
and "password" in request.form
and request.form["action"] == "delete_account"
):
if check_password_hash(user.password, request.form["password"]):
db.session.delete(user)
db.session.commit()
return logout()
else:
msg = _("IncorrectPasswordError")
elif request.method == "POST":
msg = _("FillFormError")
return render_template("account.html", user=user, msg=msg)
@app.route("/new", methods=["GET", "POST"])
def new():
"""
Handle creation of new departments or services.
This function handles the creation of new departments or services by admin users.
Returns:
render_template: Renders the new.html template with relevant data.
"""
if "loggedin" not in session or session["is_admin"] == False:
abort(403)
departments = Department.query.all()
recommend = request.args.get("recommend")
msg = ""
if (
request.method == "POST"
and "title" in request.form
and "description" in request.form
and "type" in request.form
and "associated" in request.form
):
title = request.form["title"]
description = request.form["description"]
readme = request.form["readme"] if "readme" in request.form else None
type_of = request.form["type"]
associated_with = (
request.form["associated"] if request.form["associated"] != "none" else None
)
existing = (
Department.query.filter_by(title=title).first()
or Service.query.filter_by(title=title).first()
)
if existing:
msg = _("DuplicateNameError")
elif type_of == "Department":
new = Department(
title=title,
description=description,
readme=readme,
)
db.session.add(new)
db.session.commit()
flash(_("DepartmentCreated"), "success")
return redirect(url_for("department", id=new.id))
elif type_of == "Service":
department = Department.query.filter_by(id=associated_with).first()
if department:
new = Service(
title=title,
description=description,
readme=readme,
department_id=department.id,
)
else:
new = Service(
title=title,
description=description,
readme=readme,
)
db.session.add(new)
db.session.commit()
flash(_("ServiceCreated"), "success")
return redirect(url_for("service", id=new.id))
elif request.method == "POST":
msg = _("FillFormError")
return render_template(
"new.html", departments=departments, recommend=recommend, msg=msg
)
@app.route("/edit", methods=["GET", "POST"])
def edit():
"""
Handle editing of existing departments or services.
This function handles editing existing departments or services by admin users.
Returns:
render_template: Renders the edit.html template with relevant data.
"""
if "loggedin" not in session or session["is_admin"] == False:
abort(403)
item_type = request.args.get("item")
item_id = request.args.get("id")
if item_type == "service":
item = Service.query.filter_by(id=item_id).first()
elif item_type == "department":
item = Department.query.filter_by(id=item_id).first()
else:
item = None
if (
request.method == "POST"
and "title" in request.form
and "description" in request.form
and "readme" in request.form
and request.form["action"] == "update_item"
):
item.title = request.form["title"]
item.description = request.form["description"]
item.readme = request.form["readme"]
db.session.commit()
flash(_("ItemUpdatedSuccess").format(item_type.capitalize()), "success")
return redirect(url_for("edit", item=item_type, id=item_id))
elif request.method == "POST" and request.form["action"] == "delete_item":
db.session.delete(item)
db.session.commit()
return redirect(url_for("index"))
elif request.method == "POST":
flash(_("FillFormError"), "failure")
return render_template("edit.html", item_type=item_type, item=item)
@app.route("/departments")
def departments():
"""
Render the departments page with a list of all departments.
Returns:
render_template: Renders the list.html template with relevant data.
"""
departments = Department.query.all()
return render_template(
"list.html",
list_title=_("Departments"),
list_description=_("DepartmentsListDescription"),
path="department",
items=departments,
)
@app.route("/departments/<id>")
def department(id):
"""
Render the details of a specific department.
Args:
id (str): The ID of the department to be rendered.
Returns:
render_template: Renders the listing.html template with relevant data.
"""
department = Department.query.filter_by(id=id).first()
if not department:
abort(404)
all_services_url = url_for("department_services", id=department.id)
return render_template(
"listing.html",
type="department",
item=department,
markdown=markdown,
all_services_url=all_services_url,
)
@app.route("/departments/<id>/services")
def department_services(id):
"""
Render the services of a specific department.
Args:
id (str): The ID of the department.
Returns:
render_template: Renders the list.html template with relevant data.
"""
department = Department.query.filter_by(id=id).first()
if not department:
abort(404)
services = Service.query.filter_by(department_id=id).all()
return render_template(
"list.html",
list_title=(
department.title + " " + _("Services")
if get_locale() == "en"
else _("DeptServices") + " " + department.title
),
list_description=_("ServicesListDescription"),
path="service",
items=services,
)
@app.route("/services")
def services():
"""
Render the services page with a list of all services.
Returns:
render_template: Renders the list.html template with relevant data.
"""
services = Service.query.all()
return render_template(
"list.html",
list_title=_("Services"),
list_description=_("ServicesListDescription"),
item_type="all_services",
path="service",
items=services,
Department=Department,
)
@app.route("/services/<id>")
def service(id):
"""
Render the details of a specific service.
Args:
id (str): The ID of the service to be rendered.
Returns:
render_template: Renders the listing.html template with relevant data.
"""
service = Service.query.filter_by(id=id).first()
if not service:
abort(404)
return render_template(
"listing.html",
type="service",
item=service,
Department=Department,
markdown=markdown,
)
@app.route("/search")
def search():
"""
Handle search functionality for departments and services.
Returns:
render_template: Renders the search.html template with relevant data.
"""
search_query = request.args.get("query")
item = request.args.get("item") if request.args.get("item") else "all"
msg = ""
search_title = _("SearchTitle")
search_placeholder = _("SearchPlaceholder")
departments = None
services = None
if search_query:
if item == "department" or item == "service":
search_title = _("SearchTitleItem").format(item.capitalize())
search_placeholder = _("SearchPlaceholderItem").format(item.capitalize())
if item == "department" or item == "all":
departments = Department.query.filter(
Department.title.ilike(f"%{search_query}%")
).all()
if not departments and item != "all":
msg = _("NoDepartmentsFound")
if item == "service" or item == "all":
services = Service.query.filter(
Service.title.ilike(f"%{search_query}%")
).all()
if not services and item != "all":
msg = _("NoServicesFound")
if item == "all" and not departments and not services:
msg = _("NoItemsFound")
return render_template(
"search.html",
search_title=search_title,
search_placeholder=search_placeholder,
search_query=search_query,
msg=msg,
departments=departments,
services=services,
Department=Department,
)
@app.route("/new_order", methods=["GET", "POST"])
def new_order():
"""
Handle creation of a new order.
Returns:
render_template: Renders the new_order.html template with relevant data.
"""
if "loggedin" not in session:
abort(401)
if session["is_admin"] == True:
abort(403)
services = Service.query.all()
recommend = request.args.get("recommend")
msg = ""
if request.method == "POST" and "service" in request.form:
service = request.form["service"]
details = request.form["details"] if "details" in request.form else None
files = request.files.getlist("file")
file_paths = []
if not all(file.filename == "" for file in files):
for file in files:
filename = str(uuid4()) + "_" + secure_filename(file.filename)
file.save(path.join(app.config["UPLOAD_DIRECTORY"], filename))
file_paths.append(filename)
new_order = Order(
details=details,
start_date=datetime.now(),
service_id=int(service),
user_id=session["id"],
file_paths=",".join(file_paths) if file_paths else None,
is_done=False,
)
db.session.add(new_order)
db.session.commit()
flash(_("OrderCreatedSuccess"), "success")
return redirect(url_for("order", id=new_order.id))
elif request.method == "POST":
msg = _("FillFormError")
return render_template(
"new_order.html",
services=services,
recommend=recommend,
int=int,
msg=msg,
)
@app.route("/my_orders")
def my_orders():
"""
Render the orders page with a list of user's orders.
Returns:
render_template: Renders the my_orders.html template with relevant data.
"""
if "loggedin" not in session:
abort(401)
elif session["is_admin"] == True:
abort(403)
orders = Order.query.filter_by(user_id=session["id"], is_done=False).all()
done_orders = Order.query.filter_by(user_id=session["id"], is_done=True).all()
return render_template(
"my_orders.html",
orders=orders,
done_orders=done_orders,
Service=Service,
Department=Department,
naturaltime=naturaltime,
strptime=datetime.strptime,
str=str,
)
@app.route("/my_orders/<id>", methods=["GET", "POST"])
def order(id):
"""
Render the details of a specific order.
Args:
id (str): The ID of the order to be rendered.
Returns:
render_template: Renders the order.html template with relevant data.
"""
order = Order.query.filter_by(id=id).first()
if not order:
abort(404)
elif order.user_id != session["id"]:
abort(403)
if request.method == "POST" and request.form["action"] == "delete_order":
db.session.delete(order)
db.session.commit()
return redirect(url_for("my_orders"))
return render_template(
"order.html",
order=order,
Service=Service,
Department=Department,
naturaltime=naturaltime,
strptime=datetime.strptime,
str=str,
)
@app.route("/orders")
def orders():
"""
Render the orders page with a list of all active orders.
Returns:
render_template: Renders the orders.html template with relevant data.
"""
if "loggedin" not in session or session["is_admin"] == False:
abort(403)
orders = Order.query.filter_by(is_done=False).all()
return render_template(
"orders.html",
orders=orders,
Service=Service,
Department=Department,
naturaltime=naturaltime,
strptime=datetime.strptime,
str=str,
)
@app.route("/orders/<id>", methods=["GET", "POST"])
def user_order(id):
"""
Render the details of a specific user order.
Args:
id (str): The ID of the order to be rendered.
Returns:
render_template: Renders the user_order.html template with relevant data.
"""
order = Order.query.filter_by(id=id).first()
if not order:
abort(404)
elif "loggedin" not in session or session["is_admin"] == False:
abort(403)
if request.method == "POST" and request.form["action"] == "finish_order":
order.end_date = datetime.now()
order.is_done = True
db.session.commit()
flash(_("OrderFinishedSuccess"), "success")
elif request.method == "POST" and request.form["action"] == "delete_order":
db.session.delete(order)
db.session.commit()
flash(
_("OrderDeletedSuccess").format(order_id=order.id),
"success",
)
return redirect(url_for("orders"))
return render_template(
"user_order.html",
order=order,
Service=Service,
Department=Department,
naturaltime=naturaltime,
strptime=datetime.strptime,
str=str,
)
@app.route("/contact_us", methods=["GET", "POST"])
def contact():
"""
Handle the contact us page.
Returns:
render_template: Renders the contact.html template with relevant data.
"""
msg = ""
if (
request.method == "POST"
and "subject" in request.form
and "message" in request.form
):
if "loggedin" in session:
user = User.query.filter_by(id=session["id"]).first()
name = user.name
email = user.email
phone = user.phone
elif (
"loggedin" not in session
and "name" in request.form
and "email" in request.form
and "phone" in request.form
):
name = request.form["name"]
email = request.form["email"]
phone = request.form["phone"]
subject = request.form["subject"]
message = request.form["message"]
if name and email and phone and subject and message:
# Logic for sending user feedback through email to feedback staff involves
msg = _("FeedbackReceivedSuccess")
else:
msg = _("FillFormError")
elif request.method == "POST":
msg = _("FillFormError")
return render_template("contact.html", msg=msg)
@app.errorhandler(401)
def unauthorized(e):
"""
Handle an unauthorized (401) error.
Args:
e: The error object.
Returns:
redirect: Redirects to the login page for an unauthorized user.
"""
return redirect(url_for("login"))
@app.errorhandler(403)
def forbidden(e):
"""
Handle an unauthorized (403) error.
Args:
e: The error object.
Returns:
redirect: Redirects to the index page for an unauthorized user.
"""
return redirect(url_for("index"))
@app.errorhandler(404)
def page_not_found(e):
"""
Handle a 404 Page Not Found error.
Args:
e: The error object.
Returns:
render_template: Renders the 404.html template for a 404 error.
"""
return render_template("404.html"), 404