generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_copy.py
1115 lines (921 loc) · 40.9 KB
/
app_copy.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 os
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
import re
if os.path.exists("env.py"):
import env
app = Flask(__name__)
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")
mongo = PyMongo(app)
@app.context_processor
def get_authors():
authors = list(mongo.db.users.find())
return dict(authors=authors)
@app.route("/")
@app.route("/get_recipes")
def get_recipes():
recipes = list(mongo.db.recipes.find())
this_url = request.path
referer_view = get_referer_view(request)
return render_template("recipes.html", recipes=recipes, this_url=this_url,
referer_view=referer_view)
@app.route("/recipe/<recipe_id>", methods=["GET", "POST"])
def recipe(recipe_id):
recipe = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
# these two variables are present in nealy all views and passed to the script file via hidden inputs in the base template.
# their values are the current url and the preceding. The script compares them and if they are a match it runs the code to avoid page scroll reset.
# the referer urls is worked out with the get_referer_view
this_url = request.path
referer_view = get_referer_view(request)
if not referer_view:
referer_view = this_url
# code to add tags to the recipe, if a user has pressed the Add Tag button
tagsInput = []
tags = list(mongo.db.tags.find().sort("tag", 1))
try:
tagsFetched = recipe.get('tags')
tagsInput = list(tagsFetched)
except:
tagsFetched = []
# if the recipe already has tags, remove them form the select box
try:
tags = list(mongo.db.tags.find().sort("tag", 1))
if tagsInput:
for tag in tags:
if tag['tag'] in tagsInput:
tags = [i for i in tags if i != tag]
else:
pass
except:
try:
tags = list(mongo.db.tags.find().sort("tag", 1))
except:
tags = []
# the user has hit the Add Tag button
if request.method == "POST" and 'addTagBtn' in request.args:
if 'recipe_id' in request.args:
recipe_id = request.args['recipe_id']
else:
recipe_id = recipe_id
pick_tag = request.form.get("pick_tag")
new_tag = request.form.get("new_tag")
if pick_tag:
if pick_tag not in tagsInput:
tagsInput.append(str(pick_tag))
if new_tag:
if new_tag not in tagsInput:
tagsInput.append(str(new_tag))
existingTags = list(mongo.db.tags.find().sort("tag", 1))
existingTagsItems = []
for tag in existingTags:
existingTagsItems.append(tag['tag'])
# Insert the tag in the db if not already existing
if tagsInput:
for x in tagsInput:
strX = str(x)
tagToAdd = {"tag": strX}
if not strX in existingTagsItems:
print('strX e', strX)
mongo.db.tags.insert_one(tagToAdd)
mongo.db.recipes.update({"_id": ObjectId(recipe_id)}, {
'$set': {"tags": tagsInput}})
return redirect(url_for('recipe', recipe_id=recipe_id, this_url=this_url, referer_view=referer_view))
return render_template("recipe.html", recipe=recipe, tags=tags, this_url=this_url, referer_view=referer_view)
# from Tim Nelson (CI)
@app.route("/register", methods=["GET", "POST"])
def register():
this_url = request.path
referer_view = get_referer_view(request)
recipe_id = ""
# save the recipe id if the user clicked the register link on the recipe page(for later redirection)
try:
recipe_id = session['fromRecipe_id']
except:
session['fromRecipe_id'] = ""
if 'recipe_id' in request.args:
recipe_id = request.args['recipe_id']
session['fromRecipe_id'] = recipe_id
if request.method == "POST":
# check if username already exists in db
existing_user = mongo.db.users.find_one(
{"username": request.form.get("usernameReg").lower()})
if existing_user:
flash("Username already exists")
return redirect(url_for("register"))
else:
register = {
"username": request.form.get("usernameReg").lower(),
"password": generate_password_hash(request.form.get("usernameRegPassword"))
}
mongo.db.users.insert_one(register)
# put the new user into 'session' cookie
session["user"] = request.form.get("usernameReg").lower()
flash("Registration Successful!")
# return the user to the recipe page if he clicked the register link from there
if session['fromRecipe_id']:
recipe_id = session['fromRecipe_id']
session.pop('fromRecipe_id')
return redirect(url_for('recipe', recipe_id=recipe_id, this_url=this_url, referer_view=referer_view))
else:
return redirect(url_for(
"profile", username=session["user"]))
return render_template("register.html", this_url=this_url, referer_view=referer_view)
# from Tim Nelson (CI)
@app.route("/login", methods=["GET", "POST"])
def login():
this_url = request.path
referer_view = get_referer_view(request)
recipe_id = ""
# save the recipe id if the user clicked the register link on the recipe page(for later redirection)
try:
recipe_id = session['fromRecipe_id']
except:
session['fromRecipe_id'] = ""
if 'recipe_id' in request.args:
recipe_id = request.args['recipe_id']
session['fromRecipe_id'] = recipe_id
if request.method == "POST":
# check if username exists in db
existing_user = mongo.db.users.find_one(
{"username": request.form.get("username").lower()})
if existing_user:
# ensure hashed password matches user input
if check_password_hash(
existing_user["password"], request.form.get("password")):
session["user"] = request.form.get("username").lower()
flash("Welcome, {}".format(
request.form.get("username")))
# return the user to the recipe page if he clicked the register link from there
if session['fromRecipe_id']:
recipe_id = session['fromRecipe_id']
session.pop('fromRecipe_id')
return redirect(url_for('recipe', recipe_id=recipe_id, this_url=this_url, referer_view=referer_view))
else:
return redirect(url_for(
"profile", username=session["user"]))
else:
# invalid password match
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
else:
# username doesn't exist
flash("Incorrect Username and/or Password")
return redirect(url_for("login"))
return render_template("login.html", this_url=this_url, referer_view=referer_view)
@app.route("/search", methods=["GET", "POST"])
def search():
query = request.args['search']
recipes = list(mongo.db.recipes.find({"$text": {"$search": query}}))
this_url = request.path
referer_view = get_referer_view(request)
if query == "":
queryTerm = "You haven\'t entered and search criteria"
else:
if recipes == []:
queryTerm = "Your search for \"" + query + "\" returned no results"
else:
queryTerm = 'Search results for query : \"' + query + '\"'
return render_template("recipes.html", recipes=recipes, queryTerm=queryTerm, this_url=this_url, referer_view=referer_view)
@app.route("/profile/<username>", methods=["GET", "POST"])
def profile(username):
this_url = request.path
referer_view = get_referer_view(request)
# grab the session user's username from db
username = mongo.db.users.find_one(
{"username": session["user"]})["username"]
if session["user"]:
recipes = list(mongo.db.recipes.find({"author": username}))
return render_template("profile.html", username=username, recipes=recipes, this_url=this_url, referer_view=referer_view)
@app.route("/author/<username>", methods=["GET", "POST"])
def author(username):
usernameObj = mongo.db.users.find_one({"username": username})
username = usernameObj['username']
recipes = list(mongo.db.recipes.find({"author": username}))
this_url = request.path
referer_view = get_referer_view(request)
return render_template("profile.html", username=username, recipes=recipes, this_url=this_url, referer_view=referer_view)
# from Tim Nelson (CI)
@app.route("/logout")
def logout():
# remove user from session cookie
flash("You have been logged out")
session.pop("user")
return redirect(url_for("login"))
@app.route("/add_recipe", methods=["GET", "POST"])
def add_recipe():
# check if some tags have been added already
try:
tagsInput = session['tagsInput']
except:
tagsInput = []
# get the value of the tag, if entered
if 'addTagBtn' in request.args:
pick_tag = request.form.get("pick_tag")
new_tag = request.form.get("new_tag")
# if pick_tag != None:
if pick_tag:
if pick_tag not in tagsInput:
tagsInput.append(str(pick_tag))
session['tagsInput'] = tagsInput
# if new_tag != None:
if new_tag:
if new_tag not in tagsInput:
tagsInput.append(str(new_tag))
session['tagsInput'] = tagsInput
# check if some ingridients have been added already
try:
ingridients = session['ingridients']
except:
ingridients = []
# check if the ingridients'name has been added already
try:
recipe_name = request.form.get("recipe_name")
if recipe_name is not None:
session['recipe_name'] = str(recipe_name)
else:
try:
recipe_name = str(session['recipe_name'])
except:
recipe_name = ""
except:
recipe_name = ""
# check if any method steps have been added already
try:
steps = session['steps']
except:
steps = []
# if the recipe already has tags, remove them form the select box
try:
tags = list(mongo.db.tags.find().sort("tag", 1))
if tagsInput:
for tag in tags:
if tag['tag'] in tagsInput:
tags = [i for i in tags if i != tag]
else:
pass
except:
try:
tags = list(mongo.db.tags.find().sort("tag", 1))
except:
tags = []
# remove any tag that has been clicked("X" beside the tag)
if 'delTag' in request.args:
deltag = request.args['delTag']
if tagsInput:
for tag in tagsInput:
if tag == deltag:
tagsInput.remove(tag)
# restore the removed tag to the select box
tags = list(mongo.db.tags.find().sort("tag", 1))
for tag in tags:
if tag['tag'] in tagsInput:
tags = [i for i in tags if i != tag]
else:
pass
# an hidden field, the logged user knows who he is
try:
author = request.form.get("author")
except:
author = ""
# variable to be used later, if a user has failed some required fields in the ingredient mini-form
incomplete_Ing = []
# clear the form if the user wants to start over
if 'clear' in request.args:
clear = request.args['clear']
if clear:
try:
session.pop("ingridients")
session.pop("steps")
session.pop("tagsInput")
ingridients = []
steps = []
recipe_name = ""
recipe_description = ""
tags = list(mongo.db.tags.find().sort("tag", 1))
time = ""
cook_time = ""
time_notes = ""
author = ""
tagsInput = []
incomplete_Ing = []
except:
ingridients = []
steps = []
recipe_name = ""
recipe_description = ""
tags = list(mongo.db.tags.find().sort("tag", 1))
time = ""
cook_time = ""
time_notes = ""
author = ""
tagsInput = []
incomplete_Ing = []
else:
clear = False
# determine at what point of the ingredients' section the user wants make a new entry
if 'afterIng' in request.args:
afterIng = int(request.args['afterIng'])
else:
afterIng = 0
# determine at what point of the ingredients' section the user wants make an amendment
if 'ing_to_edit' in request.args:
ing_to_edit = int(request.args['ing_to_edit'])
else:
ing_to_edit = None
# determine at what point of the method steps' section the user wants make a new entry
if 'afterStep' in request.args:
afterStep = int(request.args['afterStep'])
else:
afterStep = 0
# determine at what point of the method steps' section the user wants make an amendment
if 'step_to_edit' in request.args:
step_to_edit = int(request.args['step_to_edit'])
else:
step_to_edit = 0
# make the ingredient mini form available if the user clicked the "+" button
if 'can_add_ing' in request.args:
afterIng = int(request.args['afterIng'])
can_add_ing = True
else:
can_add_ing = False
# make the ingredient mini form available if the user clicked the edit button
if 'edit_ing' in request.args:
ing_to_edit = int(request.args['ing_to_edit'])
edit_ing = True
else:
edit_ing = False
# make the method step mini form available if the user clicked the "+" button
if 'can_add_step' in request.args:
afterStep = int(request.args['afterStep'])
can_add_step = True
else:
can_add_step = False
# make the method step mini form available if the user clicked the edit button
if 'edit_step' in request.args:
step_to_edit = int(request.args['step_to_edit'])
edit_step = True
else:
edit_step = False
# if the the user deleted an ingredient, restore its tag to the selct box, if he hadn't already done manually
# remove the ingredient so it doesn't appear in the relevant section, update the session variable
if 'del_ing' in request.args:
ing_to_del = int(request.args['ing_to_del'])
if ingridients:
tagToRemove = str(ingridients[ing_to_del][0])
if tagToRemove in tagsInput:
tagsInput.remove(tagToRemove)
ingridients.pop(ing_to_del)
session['ingridients'] = list(ingridients)
# remove the step so it doesn't appear in the relevant section, update the session variable
if 'del_step' in request.args:
step_to_del = int(request.args['step_to_del'])
steps.pop(step_to_del)
session['steps'] = list(steps)
# swap the ingredient with the one above or with the last if first, update the session variable
if 'ingridient_pos_up' in request.args:
ingridient_pos = request.args['ingridient_pos_up']
ingridients[int(ingridient_pos)], ingridients[(int(ingridient_pos)-1)
] = ingridients[(int(ingridient_pos)-1)], ingridients[int(ingridient_pos)]
session['ingridients'] = list(ingridients)
# swap the step with the one above or with the last if first, update the session variable
if 'step_pos_up' in request.args:
step_pos = request.args['step_pos_up']
steps[int(step_pos)], steps[(int(step_pos)-1)
] = steps[(int(step_pos)-1)], steps[int(step_pos)]
session['steps'] = list(steps)
# swap the ingredient with the one below or with the first if last, update the session variable
if 'ingridient_pos_down' in request.args:
ingridient_pos = int(request.args['ingridient_pos_down'])
len_ingridient_minus_1 = int(len(ingridients))-1
if ingridient_pos == len_ingridient_minus_1:
ingridients[int(ingridient_pos)], ingridients[0] = ingridients[0], ingridients[int(
ingridient_pos)]
else:
ingridients[int(ingridient_pos)], ingridients[(int(ingridient_pos)+1)
] = ingridients[(int(ingridient_pos)+1)], ingridients[int(ingridient_pos)]
session['ingridients'] = list(ingridients)
# swap the step with the one below or with the first if last, update the session variable
if 'step_pos_down' in request.args:
step_pos = int(request.args['step_pos_down'])
len_steps_minus_1 = int(len(steps))-1
if step_pos == len_steps_minus_1:
steps[int(step_pos)], steps[0] = steps[0], steps[int(
step_pos)]
else:
steps[int(step_pos)], steps[(int(step_pos)+1)
] = steps[(int(step_pos)+1)], steps[int(step_pos)]
session['steps'] = list(steps)
# process the ingredient mini form subssion,
if 'add_ingridient' in request.args:
recipe_name = str(request.form.get("recipe_name"))
session['recipe_name'] = recipe_name
recipe_description = str(request.form.get("recipe_description"))
session['recipe_description'] = recipe_description
ingredient1 = []
# ingridient's name, quantity and measure unit are enough to save.
# insert or update ingredient as appropriate, update session variables
ingredient1 = request.form.getlist('ingredient1')
if ingredient1:
if not ingredient1 in ingridients:
if ingredient1[0] and ingredient1[1] and ingredient1[2]:
if 'afterIng' in request.args:
afterIng = int(request.args['afterIng'])
ingridients.insert((afterIng+1), ingredient1)
elif 'editIng' in request.args:
editIng = int(request.args['editIng'])
tagToEdit = str(ingridients[editIng][0])
if tagToEdit in tagsInput:
tagsInput.remove(tagToEdit)
ingridients[editIng] = ingredient1
else:
ingridients.append(ingredient1)
can_add_ing = False
edit_ing = False
session['ingridients'] = ingridients
# the name of the ingridient is automatically added as tag
if ingredient1:
if not str(ingredient1[0]) in tagsInput:
tagsInput.append(str(ingredient1[0]))
session['tagsInput'] = tagsInput
# if any of the values of the first 3 fields of the ingredient are missing, re-present the mini form, with any of submitted values prefilled
else:
if 'afterIng' in request.args:
afterIng = int(request.args['afterIng'])
can_add_ing = True
incomplete_Ing = request.form.getlist('ingredient1')
else:
incomplete_Ing = request.form.getlist('ingredient1')
edit_ing = True
ing_to_edit = int(request.args['editIng'])
else:
can_add_ing = False
# process the step mini form subssion,
# insert or update step as appropriate, update session variables
if 'add_step' in request.args:
step = ""
step = request.form.get('step')
if step is not None:
step = str(request.form.get('step'))
if step:
if 'afterStep' in request.args:
afterStep = int(request.args['afterStep'])
steps.insert((afterStep+1), step)
elif 'step_to_edit' in request.args:
step_to_edit = int(request.args['step_to_edit'])
steps[step_to_edit] = step
else:
steps.append(step)
can_add_step = False
edit_step = False
session['steps'] = steps
# ifthe value is missing, re-present the mini form
if not step:
if 'afterStep' in request.args:
afterStep = int(request.args['afterStep'])
can_add_step = True
# the usual referer and current url values, for JS
this_url = request.path
referer_view = get_referer_view(request)
# the user has clicked the Add Recipe button
# all the data in the form so far gets saved in a dictionary and pymongo sends it to MongoDB
if request.method == "POST" and 'recipe' in request.args:
recipe = {
"recipe_name": request.form.get("recipe_name"),
"recipe_description": request.form.get("recipe_description"),
"ingridients": ingridients,
"steps": steps,
"tags": tagsInput,
"time": [request.form.get("time"), request.form.get("cook_time"), request.form.get("time_notes")],
"author": request.form.get("author"),
}
# if the user came up with a tag of his own, add it to the MongoDB collection for future use
existingTags = list(mongo.db.tags.find().sort("tag", 1))
existingTagsItems = []
for tag in existingTags:
existingTagsItems.append(tag['tag'])
if tagsInput:
for x in tagsInput:
strX = str(x)
tagToAdd = {"tag": strX}
if not strX in existingTagsItems:
mongo.db.tags.insert_one(tagToAdd)
# collect data about the user and the id of the recipes he posted so far.
# compare the old and new recipe lists, find the odd one out, it's the one that augmeted the list with this new recipe, redirect to this last recipe page
thisRecipeAuthor = request.form.get("author")
recipeIDsSoFar = []
UserRecipesSoFar = list(
mongo.db.recipes.find({"author": thisRecipeAuthor}))
for x in UserRecipesSoFar:
recipeIDsSoFar.append(str(ObjectId(x['_id'])))
session['ingridients'] = list(ingridients)
session['steps'] = list(steps)
mongo.db.recipes.insert_one(recipe)
NewUserRecipeIDs = []
LastID = ""
recipesWithNew = list(mongo.db.recipes.find(
{"author": thisRecipeAuthor}))
for x in recipesWithNew:
NewUserRecipeIDs.append(str(ObjectId(x['_id'])))
strLastID = ""
for x in NewUserRecipeIDs:
if not x in recipeIDsSoFar:
LastID = x
strLastID = str(LastID)
flash("Recipe Successfully Added")
session.pop("ingridients")
session.pop("steps")
session.pop("tagsInput")
return redirect(url_for('recipe', recipe_id=LastID))
return render_template("add_recipe.html", this_url=this_url, referer_view=referer_view, ingridients=ingridients, tagsInput=tagsInput, steps=steps, tags=tags, can_add_ing=can_add_ing, edit_step=edit_step, edit_ing=edit_ing, ing_to_edit=ing_to_edit, author=author, afterIng=afterIng, can_add_step=can_add_step, step_to_edit=step_to_edit, afterStep=afterStep, clear=clear, incomplete_Ing=incomplete_Ing)
@app.route("/edit_recipe", methods=["GET", "POST"])
def edit_recipe():
# prefill form with the existing recipe data
if 'recipe_id' in request.args:
time = ""
cook_time = ""
time_notes = ""
recipe_id = request.args['recipe_id']
session['recipe_id'] = recipe_id
recipeToEdit = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
tags = list(mongo.db.tags.find().sort("tag", 1))
recipe_nameFetched = recipeToEdit.get('recipe_name')
recipe_descriptionFetched = recipeToEdit.get('recipe_description')
timeFetched = recipeToEdit.get('time')
recipe_name = recipe_nameFetched
recipe_description = recipe_descriptionFetched
try:
time = timeFetched[0]
except:
pass
try:
cook_time = timeFetched[1]
except:
pass
try:
time_notes = timeFetched[2]
except:
pass
ingridientsFetched = recipeToEdit.get('ingridients')
stepsFetched = recipeToEdit.get('steps')
tagsFetched = recipeToEdit.get('tags')
session['ingridients'] = ingridientsFetched
session['steps'] = stepsFetched
session['tagsInput'] = tagsFetched
ingridients = session['ingridients']
steps = session['steps']
tagsInput = session['tagsInput']
this_url = request.path
referer_view = get_referer_view(request)
return render_template("edit_recipe.html", this_url=this_url, referer_view=referer_view, recipe=recipeToEdit, tags=tags, recipe_name=recipe_name, recipe_description=recipe_description, time=time, cook_time=cook_time, time_notes=time_notes, recipe_id=recipe_id, ingridients=ingridients, steps=steps, tagsInput=tagsInput)
# to be used later to update the collection document
try:
recipe_id = session['recipe_id']
except:
recipe_id = ""
# check if some tags have been added already
try:
tagsInput = session['tagsInput']
except:
tagsInput = []
# extra_Ing = False
if 'addTagBtn' in request.args:
pick_tag = request.form.get("pick_tag")
new_tag = request.form.get("new_tag")
# if pick_tag != None:
if pick_tag:
if pick_tag not in tagsInput:
tagsInput.append(str(pick_tag))
session['tagsInput'] = tagsInput
# if new_tag != None:
if new_tag:
if new_tag not in tagsInput:
tagsInput.append(str(new_tag))
session['tagsInput'] = tagsInput
# check if some ingridients have been added already
try:
ingridients = session['ingridients']
except:
ingridients = []
# check if the ingridients'name has been added already
try:
recipe_name = request.form.get("recipe_name")
if recipe_name is not None:
session['recipe_name'] = str(recipe_name)
else:
try:
recipe_name = str(session['recipe_name'])
except:
recipe_name = ""
except:
recipe_name = ""
# check if any method steps have been added already
try:
steps = session['steps']
except:
steps = []
# if the recipe already has tags, remove them form the select box
try:
tags = list(mongo.db.tags.find().sort("tag", 1))
if tagsInput:
for tag in tags:
if tag['tag'] in tagsInput:
tags = [i for i in tags if i != tag]
else:
pass
except:
try:
tags = list(mongo.db.tags.find().sort("tag", 1))
except:
tags = []
# remove any tag that has been clicked("X" beside the tag)
if 'delTag' in request.args:
deltag = request.args['delTag']
if tagsInput:
for tag in tagsInput:
if tag == deltag:
tagsInput.remove(tag)
tags = list(mongo.db.tags.find().sort("tag", 1))
for tag in tags:
if tag['tag'] in tagsInput:
tags = [i for i in tags if i != tag]
else:
pass
# an hidden field, the logged user knows who she is
try:
author = request.form.get("author")
except:
author = ""
# variable to be used later, if a user has failed some required fields in the ingredient mini-form
incomplete_Ing = []
# clear the form if the user wants to start over
if 'clear' in request.args:
clear = request.args['clear']
if clear:
try:
session.pop("ingridients")
session.pop("steps")
session.pop("tagsInput")
ingridients = []
steps = []
recipe_name = ""
recipe_description = ""
tags = list(mongo.db.tags.find().sort("tag", 1))
time = ""
cook_time = ""
time_notes = ""
author = ""
tagsInput = []
incomplete_Ing = []
except:
ingridients = []
steps = []
recipe_name = ""
recipe_description = ""
tags = list(mongo.db.tags.find().sort("tag", 1))
time = ""
cook_time = ""
time_notes = ""
author = ""
tagsInput = []
incomplete_Ing = []
else:
clear = False
# determine at what point of the ingredients' section the user wants make a new entry
if 'afterIng' in request.args:
afterIng = int(request.args['afterIng'])
else:
afterIng = 0
# determine at what point of the ingredients' section the user wants make an amendment
if 'ing_to_edit' in request.args:
ing_to_edit = int(request.args['ing_to_edit'])
else:
ing_to_edit = None
# determine at what point of the method steps' section the user wants to make a new entry
if 'afterStep' in request.args:
afterStep = int(request.args['afterStep'])
else:
afterStep = 0
# determine at what point of the method steps' section the user wants make an amendment
if 'step_to_edit' in request.args:
step_to_edit = int(request.args['step_to_edit'])
else:
step_to_edit = 0
# make the ingredient mini form available if the user clicked the "+" button
if 'can_add_ing' in request.args:
afterIng = int(request.args['afterIng'])
can_add_ing = True
else:
can_add_ing = False
# make the ingredient mini form available if the user clicked the edit button
if 'edit_ing' in request.args:
ing_to_edit = int(request.args['ing_to_edit'])
edit_ing = True
else:
edit_ing = False
# make the method step mini form available if the user clicked the "+" button
if 'can_add_step' in request.args:
afterStep = int(request.args['afterStep'])
can_add_step = True
else:
can_add_step = False
# make the method step mini form available if the user clicked the edit button
if 'edit_step' in request.args:
step_to_edit = int(request.args['step_to_edit'])
edit_step = True
else:
edit_step = False
# if the the user deleted an ingredient, restore its tag to the select box, if she hadn't already done manually
# remove the ingredient so it doesn't appear in the relevant section, update the session variable
if 'del_ing' in request.args:
ing_to_del = int(request.args['ing_to_del'])
if ingridients:
tagToRemove = str(ingridients[ing_to_del][0])
if tagToRemove in tagsInput:
tagsInput.remove(tagToRemove)
ingridients.pop(ing_to_del)
session['ingridients'] = list(ingridients)
# remove the step so it doesn't appear in the relevant section, update the session variable
if 'del_step' in request.args:
step_to_del = int(request.args['step_to_del'])
steps.pop(step_to_del)
session['steps'] = list(steps)
# swap the ingredient with the one above or with the last if first, update the session variable
if 'ingridient_pos_up' in request.args:
ingridient_pos = request.args['ingridient_pos_up']
ingridients[int(ingridient_pos)], ingridients[(int(ingridient_pos)-1)
] = ingridients[(int(ingridient_pos)-1)], ingridients[int(ingridient_pos)]
session['ingridients'] = list(ingridients)
# swap the step with the one above or with the last if first, update the session variable
if 'step_pos_up' in request.args:
step_pos = request.args['step_pos_up']
steps[int(step_pos)], steps[(int(step_pos)-1)
] = steps[(int(step_pos)-1)], steps[int(step_pos)]
session['steps'] = list(steps)
# swap the ingredient with the one below or with the first if last, update the session variable
if 'ingridient_pos_down' in request.args:
ingridient_pos = int(request.args['ingridient_pos_down'])
ingridient_pos_minus_1 = int(ingridient_pos)-1
len_ingridient_minus_1 = int(len(ingridients))-1
if ingridient_pos == len_ingridient_minus_1:
ingridients[int(ingridient_pos)], ingridients[0] = ingridients[0], ingridients[int(
ingridient_pos)]
else:
ingridients[int(ingridient_pos)], ingridients[(int(ingridient_pos)+1)
] = ingridients[(int(ingridient_pos)+1)], ingridients[int(ingridient_pos)]
session['ingridients'] = list(ingridients)
# swap the step with the one below or with the first if last, update the session variable
if 'step_pos_down' in request.args:
step_pos = int(request.args['step_pos_down'])
step_pos_minus_1 = int(step_pos)-1
len_steps_minus_1 = int(len(steps))-1
if step_pos == len_steps_minus_1:
steps[int(step_pos)], steps[0] = steps[0], steps[int(
step_pos)]
else:
steps[int(step_pos)], steps[(int(step_pos)+1)
] = steps[(int(step_pos)+1)], steps[int(step_pos)]
session['steps'] = list(steps)
# process the ingredient mini form subssion,
if 'add_ingridient' in request.args:
recipe_name = str(request.form.get("recipe_name"))
session['recipe_name'] = recipe_name
recipe_description = str(request.form.get("recipe_description"))
session['recipe_description'] = recipe_description
ingredient1 = []
# ingridient's name, quantity and measure unit are enough to save.
# insert or update ingredient as appropriate, update session variables
ingredient1 = request.form.getlist('ingredient1')
if ingredient1:
if not ingredient1 in ingridients:
if ingredient1[0] and ingredient1[1] and ingredient1[2]:
if 'afterIng' in request.args:
afterIng = int(request.args['afterIng'])
ingridients.insert((afterIng+1), ingredient1)
elif 'editIng' in request.args:
editIng = int(request.args['editIng'])
tagToEdit = str(ingridients[editIng][0])
if tagToEdit in tagsInput:
tagsInput.remove(tagToEdit)
ingridients[editIng] = ingredient1
else:
ingridients.append(ingredient1)
can_add_ing = False
edit_ing = False
session['ingridients'] = ingridients
# the name of the ingridient is automatically added as tag
if ingredient1:
if not str(ingredient1[0]) in tagsInput:
tagsInput.append(str(ingredient1[0]))
session['tagsInput'] = tagsInput
# if any of the values of the first 3 fields of the ingredient are missing, re-present the mini form, with any of submitted values prefilled
else:
if 'afterIng' in request.args:
afterIng = int(request.args['afterIng'])
can_add_ing = True
incomplete_Ing = request.form.getlist('ingredient1')
else:
incomplete_Ing = request.form.getlist('ingredient1')
edit_ing = True
ing_to_edit = int(request.args['editIng'])