-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
1117 lines (835 loc) · 32 KB
/
app.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
990
991
992
993
994
995
996
997
998
999
1000
# import libraries & dependencies
import os
from dns.resolver import query
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for
)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from flask_paginate import Pagination, get_page_args
if os.path.exists("env.py"):
import env
app = Flask(__name__)
# mongodb config vars
app.config["MONGO_DBNAME"] = os.environ.get("MONGO_DBNAME")
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")
# global variables
mongo = PyMongo(app)
# routes
@app.route("/")
@app.route("/welcome")
def welcome():
"""welcome:
* Displays welcome page template.
\n Args:
* None.
\n Returns:
* Template displaying index.html welcome page.
"""
return render_template("index.html")
@app.route("/works")
def works():
"""works:
* Fetches every work uploaded to db.
* Displays paginated works.
\n Args:
* None.
\n Returns:
* Template displaying all works in date_submitted descending order.
"""
works = list(mongo.db.works.find().sort("date_submitted", -1))
# pagination
def get_works(offset=0, per_page=9):
return works[offset: offset + per_page]
page, per_page, offset = get_page_args(
page_parameter='page',
per_page_parameter='per_page'
)
per_page = 9
total = len(works)
pagination_works = get_works(offset=offset, per_page=per_page)
pagination = Pagination(
page=page, per_page=per_page, total=total,
css_framework='bootstrap4'
)
return render_template(
"works.html", works=pagination_works, page=page,
per_page=per_page, pagination=pagination
)
@app.route("/search", methods=["GET", "POST"])
def search():
"""search:
* Takes user input from works page search box.
* Queries db for results based on user input.
* Displays results with pagination.
\n Args:
1. query(str): user input from search box field.
\n Returns:
* Template containing any results matching query input with pagination.
* h2 containing 'No Results Found' is returned if no results.
"""
query = request.form.get("query")
works = list(mongo.db.works.find({"$text": {"$search": query}}))
# pagination
def get_works(offset=0, per_page=9):
return works[offset: offset + per_page]
page, per_page, offset = get_page_args(
page_parameter='page',
per_page_parameter='per_page'
)
per_page = 9
total = len(works)
pagination_works = get_works(offset=offset, per_page=per_page)
pagination = Pagination(
page=page, per_page=per_page, total=total,
css_framework='bootstrap4'
)
return render_template(
"works.html", works=pagination_works, page=page,
per_page=per_page, pagination=pagination
)
@app.route("/filter/<filter_type>/<direction>")
def filter(filter_type, direction):
"""filter:
* Checks for selected sort order
* Sets the order in which the data will display on main page.
* Displays data in defined order with pagination.
\n Args:
1. filter_type(str): Targeted field within db collection.
2. direction(str): Direction of db sort order (ascending or descending).
\n Returns:
* Template displaying works from db in respective sort order.
"""
if direction == 'ascending':
works = list(mongo.db.works.find().sort(filter_type, 1))
# pagination
def get_works(offset=0, per_page=9):
return works[offset: offset + per_page]
page, per_page, offset = get_page_args(
page_parameter='page',
per_page_parameter='per_page'
)
per_page = 9
total = len(works)
pagination_works = get_works(offset=offset, per_page=per_page)
pagination = Pagination(
page=page, per_page=per_page, total=total,
css_framework='bootstrap4'
)
return render_template(
"works.html", works=pagination_works, page=page,
per_page=per_page, pagination=pagination
)
works = list(mongo.db.works.find().sort(filter_type, -1))
# pagination
def get_works(offset=0, per_page=9):
return works[offset: offset + per_page]
page, per_page, offset = get_page_args(
page_parameter='page',
per_page_parameter='per_page'
)
per_page = 15
total = len(works)
pagination_works = get_works(offset=offset, per_page=per_page)
pagination = Pagination(
page=page, per_page=per_page, total=total,
css_framework='bootstrap4'
)
return render_template(
"works.html", works=pagination_works, page=page,
per_page=per_page, pagination=pagination
)
@app.route("/work/<work_id>")
def work(work_id):
"""work:
* Fetches selected work data from db collection.
* Passes data to template for work page.
\n Args:
1. work_id(str): ObjectId of selected work.
\n Returns:
* Template to diplay all content of individual object from collection.
"""
work = mongo.db.works.find_one({"_id": ObjectId(work_id)})
artists = mongo.db.artists.find()
crews = mongo.db.crews.find()
return render_template(
"work.html", work=work,
artists=artists, crews=crews
)
@app.route("/register", methods=["GET", "POST"])
def register():
"""register:
* Searches db for existing user.
* Appends new user data to db if not already existing.
* Renders profile page for user account or register page.
\n Args:
1. user inputs(str): username and password from form fields.
\n Returns:
* User profile page on successful register.
* Reloads register page with flash displaying error if unsucessful.
"""
if request.method == "POST":
# check db for existing username
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
flash("Username already exists")
return redirect(url_for("register"))
if request.form.get(
"password"
) == request.form.get(
"confirm-password"):
user = {
"username": request.form.get("username").lower(),
"password": generate_password_hash(
request.form.get("password")
)
}
mongo.db.users.insert_one(user)
# insert user into session cookie
session["user"] = request.form.get("username").lower()
flash("Registration Successful!")
return redirect(url_for("profile", username=session["user"]))
flash("Passwords Do Not Match")
return redirect(url_for("register"))
return render_template("register.html")
@app.route("/login", methods=["GET", "POST"])
def login():
"""login:
* Checks if user exists in db.
* Logs user in if credentials match.
* Appends username to session['user'] cookie.
\n Args:
* User inputs(str): username and password from form fields.
\n Returns:
* User profile template if successful.
* Login page with flash displaying error if unsuccessful.
"""
if request.method == "POST":
# check db for existing username
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
# validate password hash
if check_password_hash(
existing_user["password"], request.form.get("password")):
session["user"] = request.form.get("username").lower()
flash("You are now logged in as {}".format(
request.form.get("username")))
return redirect(url_for(
"profile", username=session["user"]))
else:
# password incorrect
flash("You have entered an incorrect password and/or username")
return redirect(url_for("login"))
else:
# username does not exist
flash("You have entered an incorrect password and/or username")
return redirect(url_for("login"))
return render_template("login.html")
@app.route("/profile/<username>")
def profile(username):
"""profile:
* Checks session cookies for 'user'.
* Returns user profile template.
* Shows all works and artists user has created in db.
\n Args:
1. username(str): Users registered username.
\n Returns:
* Template displaying associated user data.
* Login page if user does not exist.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
works = list(mongo.db.works.find({"submitted_by": username}))
artists = list(mongo.db.artists.find({"submitted_by": username}))
crews = list(mongo.db.crews.find({"submitted_by": username}))
return render_template(
"profile.html", username=username, works=works,
artists=artists, crews=crews
)
return redirect(url_for("login"))
@app.route("/logout")
def logout():
"""logout:
* Removes 'user' session cookie.
* Returns to login page.
\n Args:
* None.
\n Returns:
* Removes 'user' session cookie.
"""
flash("Sucessfully logged out")
session.pop("user")
return redirect(url_for("login"))
@app.route("/new_work", methods=["GET", "POST"])
def new_work():
"""new_work:
* Loads artist and style data from db to display in dropdown input fields.
* Requests data from user input form fields when method is POST.
* Appends data from requested fields to db.
* Returns works template with flash displaying success.
\n Args:
* None.
\n Returns:
* Creates new work object in works db collection.
"""
if 'user' in session:
if request.method == "POST":
work = {
"artist_name": request.form.get("name"),
"year_painted": request.form.get("year_painted"),
"style_name": request.form.get("style_name"),
"type_name": request.form.get("type_name"),
"image_url": request.form.get("image_url"),
"submitted_by": session["user"],
"date_submitted": datetime.now()
}
mongo.db.works.insert_one(work)
flash("Work Successfully Uploaded!")
return redirect(url_for("works"))
artists = mongo.db.artists.find().sort("artist_name", 1)
crews = mongo.db.crews.find().sort("crew_name", 1)
artiststyles = mongo.db.styles.find().sort("style_name", 1)
crewstyles = mongo.db.styles.find().sort("style_name", 1)
artisttypes = mongo.db.types.find().sort("type_name", 1)
crewtypes = mongo.db.types.find().sort("type_name", 1)
return render_template(
"new_work.html", artists=artists, crews=crews,
artiststyles=artiststyles, crewstyles=crewstyles,
artisttypes=artisttypes, crewtypes=crewtypes
)
return redirect(url_for("login"))
@app.route("/edit_work/<work_id>", methods=["GET", "POST"])
def edit_work(work_id):
"""edit_work:
* Checks for user editing authentication.
* Loads existing work data from db.
* Updates data for work in db when method is POST.
\n Args:
* work_id(str): ObjectId of selected work.
\n Returns:
* Updates selected object data in collection.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
work = mongo.db.works.find_one({"_id": ObjectId(work_id)})
if work["submitted_by"] == username or session["user"] == "admin":
if request.method == "POST":
update_work = {
"artist_name": request.form.get("artist_name"),
"year_painted": request.form.get("year_painted"),
"style_name": request.form.get("style_name"),
"image_url": request.form.get("image_url"),
"submitted_by": session["user"],
}
mongo.db.works.update_one(
{"_id": ObjectId(work_id)},
{"$set": update_work}
)
flash("Work Successfully Updated!")
artists = mongo.db.artists.find().sort("artist_name", 1)
styles = mongo.db.styles.find().sort("style_name", 1)
types = mongo.db.types.find().sort("type_name", 1)
return render_template(
"edit_work.html", work=work,
artists=artists, styles=styles,
types=types
)
return redirect(url_for("works"))
return redirect(url_for("login"))
@app.route("/delete_work/<work_id>")
def delete_work(work_id):
"""delete_work:
* Checks for user delete authentication.
* Removes work object from db collection.
* Returns works main page with flash displaying success.
\n Args:
* work_id(str): ObjectId of selected work.
\n Returns:
* Removes object from db collection.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
work = mongo.db.works.find_one({"_id": ObjectId(work_id)})
if work["submitted_by"] == username or session["user"] == "admin":
mongo.db.works.remove({"_id": ObjectId(work_id)})
flash("Work Successfully Deleted!")
return redirect(url_for("works"))
return redirect(url_for("works"))
return redirect(url_for("login"))
@app.route("/artists")
def artists():
"""artists:
* Fetches all artist names from db.
* Fetches all artist crews from db.
* Displays all artists by name and associated crews.
\n Args:
* None.
\n Returns:
* Template displaying artist names and crews.
"""
artists = mongo.db.artists.find().sort("artist_name", 1)
return render_template("artists.html", artists=artists)
@app.route("/artist/<artist_name>")
def artist(artist_name):
"""artist:
* Fetches selected artist data from db collection.
* Fetches selected artists works from db collection.
* Passes data to template for artist page.
\n Args:
1. artist_name(str): name of selected artist in db collection.
\n Returns:
* Template to diplay all content of individual object from collection.
"""
artist = mongo.db.artists.find_one({"artist_name": str(artist_name).upper()})
works = list(mongo.db.works.find({"artist_name": str(artist_name).upper()}))
return render_template("artist.html", artist=artist, works=works)
@app.route("/add_artist", methods=["GET", "POST"])
def add_artist():
"""add_artist:
* Loads crew data from db to display in dropdown input fields.
* Requests data from user input form fields when method is POST.
* Appends data from requested fields to db.
* Returns artists template with flash displaying success.
\n Args:
* None.
\n Returns:
* Creates new artist object in artists db collection.
"""
if 'user' in session:
if request.method == "POST":
artist = {
"artist_name": request.form.get("artist_name").upper(),
"artist_crews": request.form.getlist("artist_crews"),
"submitted_by": session["user"]
}
mongo.db.artists.insert_one(artist)
flash("Artist Successfully Added!")
return redirect(url_for("artists"))
crews = list(mongo.db.crews.find().sort("crew_name", 1))
return render_template("add_artist.html", crews=crews)
return redirect(url_for("login"))
@app.route("/edit_artist/<artist_name>", methods=["GET", "POST"])
def edit_artist(artist_name):
"""edit_artist:
* Checks for user edit authentication.
* Loads existing artist data from db.
* Updates data for artist in db when method is POST.
\n Args:
* artist_name(str): artist name of selected artist in db collection.
\n Returns:
* Updates selected artist object data in collection.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
artist = mongo.db.artists.find_one({"artist_name": str(artist_name).upper()})
if artist["submitted_by"] == username or session["user"] == "admin":
if request.method == "POST":
update_artist = {
"artist_name": request.form.get("artist_name").upper(),
"artist_crews": request.form.getlist("artist_crews"),
"submitted_by": session["user"]
}
mongo.db.artists.update_one(
{"artist_name": str(artist_name).upper()},
{"$set": update_artist}
)
flash("Artist Successfully Updated!")
return redirect(url_for("artists"))
crews = mongo.db.crews.find().sort("crew_name", 1)
return render_template(
"edit_artist.html",
artist=artist, crews=crews
)
return redirect(url_for("artists"))
return redirect(url_for("login"))
@app.route("/delete_artist/<artist_name>")
def delete_artist(artist_name):
"""delete_artist:
* Checks for user delete authentication.
* Removes work object from db collection.
* Returns works main page with flash displaying success.
\n Args:
* artist_name(str): artist name of selected artist in db collection.
\n Returns:
* Removes object from db collection.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
artist = mongo.db.artists.find_one({"artist_name": str(artist_name).upper()})
if artist["submitted_by"] == username or session["user"] == "admin":
mongo.db.artists.remove({"artist_name": str(artist_name).upper()})
flash("Artist Successfully Deleted!")
return redirect(url_for("artists"))
return redirect(url_for("artists"))
return redirect(url_for("login"))
@app.route("/crews")
def crews():
"""crews:
* Fetches all crews from db.
* Displays all crews by name and image.
\n Args:
* None.
\n Returns:
* Template displaying crew names and images.
"""
crews = mongo.db.crews.find().sort("crew_name", 1)
return render_template("crews.html", crews=crews)
@app.route("/crew/<crew_name>")
def crew(crew_name):
"""crew:
* Fetches selected crew data from db collection.
* Fetches selected artists associated to the crew from db collection.
* Passes data to template for crew page.
\n Args:
1. crew_name(str): name of selected crew in db collection.
\n Returns:
* Template to diplay all content of individual crew object from db.
"""
crew = mongo.db.crews.find_one({"crew_name": str(crew_name).upper()})
artists = list(mongo.db.artists.find({"artist_crews": str(crew_name).upper()}))
works = list(mongo.db.works.find({"artist_name": str(crew_name).upper()}))
return render_template(
"crew.html", crew=crew,
artists=artists, works=works
)
@app.route("/add_crew", methods=["GET", "POST"])
def add_crew():
"""add_crew:
* Requests data from user input form fields when method is POST.
* Appends data from requested fields to db.
* Returns crews template with flash displaying success.
\n Args:
* None.
\n Returns:
* Creates new crew object in crews db collection.
"""
if 'user' in session:
if request.method == "POST":
crew = {
"crew_name": request.form.get("crew_name").upper(),
"crew_image": request.form.get("crew_image"),
"submitted_by": session["user"]
}
mongo.db.crews.insert_one(crew)
flash("Crew Successfully Added!")
return redirect(url_for("crews"))
return render_template("add_crew.html")
return redirect(url_for("login"))
@app.route("/edit_crew/<crew_name>", methods=["GET", "POST"])
def edit_crew(crew_name):
"""edit_crew:
* Checks for user editing authentication.
* Loads existing crew data from db.
* Updates data for crew in db when method is POST.
\n Args:
* crew_name(str): name of selected crew in db collection.
\n Returns:
* Updates selected artist object data in collection.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
crew = mongo.db.crews.find_one({"crew_name": str(crew_name).upper()})
if crew["submitted_by"] == username or session["user"] == "admin":
if request.method == "POST":
update_crew = {
"crew_name": request.form.get("crew_name").upper(),
"crew_image": request.form.get("crew_image"),
"submitted_by": session["user"]
}
mongo.db.crews.update_one(
{"crew_name": str(crew_name).upper()},
{"$set": update_crew}
)
flash("Crew Successfully Updated!")
return redirect(url_for("crews"))
return render_template("edit_crew.html", crew=crew)
return redirect(url_for("crews"))
return redirect(url_for("login"))
@app.route("/delete_crew/<crew_name>")
def delete_crew(crew_name):
"""delete_crew:
* Checks for user deleting authentication.
* Removes crew object from db collection.
* Returns crews page with flash displaying success.
\n Args:
* crew_name(str): name of selected crew in db collection.
\n Returns:
* Removes object from db collection.
"""
if 'user' in session:
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
crew = mongo.db.crews.find_one({"crew_name": str(crew_name).upper()})
if crew["submitted_by"] == username or session["user"] == "admin":
mongo.db.crews.remove({"crew_name": str(crew_name).upper()})
flash("Crew Successfully Deleted!")
return redirect(url_for("crews"))
return redirect(url_for("crews"))
return redirect(url_for("login"))
@app.route("/styles")
def styles():
"""styles:
* Fetches all style names from db.
* Displays all styles by style name.
\n Args:
* None.
\n Returns:
* Template displaying style names in ascending order.
"""
styles = mongo.db.styles.find().sort("style_name", 1)
return render_template("styles.html", styles=styles)
@app.route("/style/<style_name>")
def style(style_name):
"""style:
* Fetches selected style data from db collection.
* Fetches works in selected style from db collection.
* Passes data to template for style page.
\n Args:
1. style_name(str): name of style in db collection.
\n Returns:
* Template to diplay all content of individual style object from db.
"""
style = mongo.db.styles.find_one({"style_name": str(style_name)})
works = list(mongo.db.works.find({"style_name": str(style_name)}))
return render_template("style.html", style=style, works=works)
@app.route("/add_style", methods=["GET", "POST"])
def add_style():
"""add_style:
* Requests data from user input form fields when method is POST.
* Appends data from requested fields to db.
* Returns styles template with flash displaying success.
\n Args:
* None.
\n Returns:
* Creates new style object in styles db collection.
"""
if 'user' in session:
if session["user"] == "admin":
if request.method == "POST":
style = {
"style_name": request.form.get("style_name"),
"style_image": request.form.get("image_url")
}
mongo.db.styles.insert_one(style)
flash("Style Successfully Added!")
return redirect(url_for("styles"))
return render_template("add_style.html")
return redirect(url_for("styles"))
return redirect(url_for("login"))
@app.route("/edit_style/<style_name>", methods=["GET", "POST"])
def edit_style(style_name):
"""edit_style:
* Loads existing style data from db.
* Updates data for style in db when method is POST.
\n Args:
* style_name(str): name of style in db collection.
\n Returns:
* Updates selected style object data in collection.
"""
if 'user' in session:
if session["user"] == "admin":
if request.method == "POST":
update_style = {
"style_name": request.form.get("style_name"),
"style_image": request.form.get("image_url")
}
mongo.db.styles.update_one(
{"style_name": str(style_name)},
{"$set": update_style}
)
flash("Style Successfully Updated!")
return redirect(url_for("styles"))
style = mongo.db.styles.find_one({"style_name": str(style_name)})
return render_template("edit_style.html", style=style)
return redirect(url_for("styles"))
return redirect(url_for("login"))
@app.route("/delete_style/<style_name>")
def delete_style(style_name):
"""delete_style:
* Removes style object from db collection.
* Returns styles page with flash displaying success.
\n Args:
* style_name(str): name of style in db collection.
\n Returns:
* Removes object from db collection.
"""
if 'user' in session:
if session["user"] == "admin":
mongo.db.styles.remove({"style_name": str(style_name)})
flash("Style Successfully Deleted!")
return redirect(url_for("styles"))
return redirect(url_for("styles"))
return redirect(url_for("login"))
@app.route("/types")
def types():
"""types:
* Fetches all type names from db.
* Displays all types by type name.
\n Args:
* None.
\n Returns:
* Template displaying types in name ascending order.
"""
types = mongo.db.types.find().sort("type_name", 1)
return render_template("types.html", types=types)
@app.route("/type/<type_name>")
def type(type_name):
"""type:
* Fetches selected type data from db collection.
* Fetches works of selected type from db collection.
* Passes data to template for type page.
\n Args:
1. type_name(str): name of type in db collection.
\n Returns:
* Template to diplay all content of individual type object from db.
"""
type = mongo.db.types.find_one({"type_name": str(type_name)})
works = list(mongo.db.works.find({"type_name": str(type_name)}))
return render_template("type.html", type=type, works=works)
@app.route("/add_type", methods=["GET", "POST"])
def add_type():
"""add_type:
* Checks for admin user authentication.
* Requests data from user input form fields when method is POST.
* Appends data from requested fields to db.
* Returns types template with flash displaying success.
\n Args:
* None.
\n Returns:
* Creates new type object in types db collection.
"""
if 'user' in session:
if session["user"] == "admin":
if request.method == "POST":
style = {
"type_name": request.form.get("type_name"),
"type_image": request.form.get("image_url")
}
mongo.db.types.insert_one(style)
flash("Style Successfully Added!")
return redirect(url_for("types"))
return render_template("add_type.html")
return redirect(url_for("types"))
return redirect(url_for("login"))
@app.route("/edit_type/<type_name>", methods=["GET", "POST"])
def edit_type(type_name):
"""edit_type:
* Checks for admin user authentication.
* Loads existing type data from db.
* Updates data for type in db when method is POST.
\n Args:
1. type_name(str): name of type in db collection.
\n Returns:
* Updates selected style object data in collection.
"""
if 'user' in session:
if session["user"] == "admin":
if request.method == "POST":
update_type = {
"type_name": request.form.get("type_name"),
"type_image": request.form.get("image_url")
}
mongo.db.types.update_one(
{"type_name": str(type_name)},
{"$set": update_type}
)
flash("Type Successfully Updated!")
return redirect(url_for("types"))
type = mongo.db.types.find_one({"type_name": str(type_name)})
return render_template("edit_type.html", type=type)
return redirect(url_for("types"))
return redirect(url_for("login"))
@app.route("/delete_type/<type_name>")