generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
180 lines (133 loc) · 5.75 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
import os
from flask import Flask, render_template, redirect, request, url_for
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
app = Flask(__name__)
app.config["MONGODB_NAME"] = 'happy_cooking'
app.config["MONGO_URI"] = 'mongodb+srv://root:r00tUser@myfirstcluster-3k4ci.mongodb.net/happy_cooking?retryWrites=true&w=majority'
mongo = PyMongo(app)
#----------Add Recipe Function--------------------
@app.route('/')
@app.route('/get_recipes')
def get_recipes():
#---------------Filter Function------------------
filter = {}
suitability = request.args.get('suitability')
if suitability == 'veg':
filter['recipe_vegetarian'] = True
elif suitability == 'vegan':
filter['recipe_vegan'] = True
print(filter)
country = request.args.get('country')
if country == 'mexico':
filter['recipe_country_of_origin'] = 'Mexico'
elif country == 'italy':
filter['recipe_country_of_origin'] = 'Italy'
elif country == 'greece':
filter['recipe_country_of_origin'] = 'Greece'
elif country == 'thai':
filter['recipe_country_of_origin'] = 'Thai'
print(filter)
category = request.args.get('category')
if category == 'starters':
filter['category_name'] = 'Starters'
elif category == 'appetisers':
filter['category_name'] = 'Appetisers'
elif category == 'maincourses':
filter['category_name'] = 'Main Courses'
elif category == 'desserts':
filter['category_name'] = 'Desserts'
print(filter)
if filter:
recipes = mongo.db.recipes.find(filter)
else:
recipes = mongo.db.recipes.find()
return render_template("recipes.html", recipes=recipes)
@app.route('/add_recipe')
def add_recipe():
return render_template('addrecipe.html', categories=mongo.db.categories.find())
@app.route('/insert_recipe', methods=['POST'])
def insert_recipe():
recipes = mongo.db.recipes
# get the data from the form into a dictionary I can work with
my_user_data = request.form.to_dict()
#--------------Change the values from strings to booleans----------
if 'recipe_vegetarian' in my_user_data:
my_user_data['recipe_vegetarian'] = True
else:
my_user_data['recipe_vegetarian'] = False
my_user_data['recipe_vegetarian']
if 'recipe_vegan' in my_user_data:
my_user_data['recipe_vegan'] = True
else:
my_user_data['recipe_vegan'] = False
my_user_data['recipe_vegan']
recipes.insert_one(my_user_data)
return redirect(url_for('get_recipes'))
@app.route('/edit_recipe/<recipe_id>')
def edit_recipe(recipe_id):
the_recipe = mongo.db.recipes.find_one({"_id": ObjectId(recipe_id)})
all_categories = mongo.db.categories.find()
return render_template('editrecipe.html', recipe=the_recipe, categories=all_categories)
#---------Edit Recipe Functionality---------------------
@app.route('/update_recipe/<recipe_id>', methods=['POST'])
def update_recipe(recipe_id):
recipes = mongo.db.recipes
mongo.db.recipes.update({'_id': ObjectId(recipe_id)},
{
'recipe_name': request.form.get('recipe_name'),
'category_name': request.form.get('category_name'),
'image_url': request.form.get('image_url'),
'recipe_ingredients': request.form.get('recipe_ingredients'),
'recipe_method': request.form.get('recipe_method'),
'recipe_country_of_origin': request.form.get('recipe_country_of_origin'),
#---Change strings to booleans-----
'recipe_vegetarian': True if request.form.get('recipe_vegetarian') == 'true' else False,
'recipe_vegan': True if request.form.get('recipe_vegan') == 'true' else False,
'recipe_allergens': request.form.get('recipe_allergens'),
'recipe_nutricion': request.form.get('recipe_nutricion')
})
return redirect(url_for('get_recipes'))
#--------------View Recipe----------------
@app.route('/view_recipe/<recipe_id>')
def view_recipe(recipe_id):
recipes = mongo.db.recipes
my_recipe = recipes.find_one({"_id": ObjectId(recipe_id)})
return render_template('view_recipe.html', recipe=my_recipe)
#-----------Delete Recipe Function
@app.route('/delete_recipe/<recipe_id>')
def delete_recipe(recipe_id):
mongo.db.recipes.remove({'_id': ObjectId(recipe_id)})
return redirect(url_for('get_recipes'))
#------------Categories---------------
@app.route('/get_categories')
def get_categories():
return render_template('categories.html', categories=mongo.db.categories.find())
@app.route('/edit_category/<category_id>')
def edit_category(category_id):
return render_template('editcategory.html', category=mongo.db.categories.find_one({'_id': ObjectId(category_id)}))
@app.route('/update_category/<category_id>', methods=['POST'])
def update_category(category_id):
mongo.db.categories.update(
{'_id': ObjectId(category_id)},
{'category_name': request.form.get('category_name'),
'image_url': request.form.get('image_url')})
return redirect(url_for('get_categories'))
@app.route('/delete_category/<category_id>')
def delete_category(category_id):
mongo.db.categories.remove({'_id': ObjectId(category_id)})
return redirect(url_for('get_categories'))
@app.route('/insert_category', methods=['POST'])
def insert_category():
categories = mongo.db.categories
category_doc = {'category_name': request.form.get('category_name'),
'image_url':request.form.get('image_url')}
categories.insert_one(category_doc)
return redirect(url_for('get_categories'))
@app.route('/new_category')
def new_category():
return render_template('addcategory.html')
if __name__ == '__main__':
app.run(host='0.0.0.0',
port=int(os.environ.get('PORT')),
debug=True)