-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
131 lines (106 loc) · 4.57 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
import os
from flask import Flask, render_template, redirect, request, session, url_for, Blueprint, flash
from flask_pymongo import PyMongo
from pymongo import ReturnDocument
from bson.objectid import ObjectId
from decouple import config
from flask_login import LoginManager
from forms import signupForm
from forms import sortfield
import bcrypt
if os.path.exists("env.py"):
import env
app = Flask(__name__)
app.config["MONGO_DBNAME"] = "book"
app.config["MONGO_URI"] = os.environ.get('MONGO_URI', 'mongodb://localhost')
app.secret_key = os.environ.get('SECRET_KEY')
mongo = PyMongo(app)
@app.route('/')
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == 'POST':
users = mongo.db.users
loginuser = users.find_one({'username' : request.form['username']})
if loginuser:
if bcrypt.hashpw(request.form['password'].encode('utf-8'), loginuser['password']) == loginuser['password']:
session['username'] = request.form['username']
return redirect(url_for('home'))
else: flash('Invalid username/password combination')
if session.get('username'):
return redirect(url_for('home'))
return render_template("login.html")
@app.route('/signup', methods=['POST', 'GET'])
def signup():
signupform = signupForm()
if request.method == 'POST':
users = mongo.db.users
existingUser = users.find_one({'username' : request.form['username']})
if existingUser is None:
hashpass = bcrypt.hashpw(request.form['password'].encode('utf-8'), bcrypt.gensalt())
users.insert({'username' : request.form['username'], 'password' : hashpass})
session['username'] = request.form['username']
return redirect(url_for('home'))
flash('That username already exists')
return render_template('signup.html', form = signupform)
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))
@app.route('/home', methods=['POST', 'GET'])
def home():
if request.method == 'POST':
sortby = request.form.get('sorting')
mystring = str(sortby)
return render_template("home.html", sortby = sortby,
recipes=mongo.db.recipe.find().sort(sortby, 1))
return render_template("home.html",
recipes=mongo.db.recipe.find())
@app.route('/addrecipe')
def addrecipe():
return render_template("add_recipe.html")
@app.route('/insert_recipe', methods=['POST'])
def insert_recipe():
recipes = mongo.db.recipe
dict_values = request.form.to_dict()
dict_values['upvotes'] = 1
recipes.insert_one(dict_values)
return redirect(url_for('home'))
@app.route('/recipedetail/<recipe_id>')
def recipedetail(recipe_id):
the_recipe = mongo.db.recipe.find_one({"_id": ObjectId(recipe_id)})
return render_template("recipedetail.html", recipe=the_recipe)
@app.route('/upvote/<recipe_id>', methods=['POST'])
def upvote(recipe_id):
mongo.db.recipe.update({"_id": ObjectId(recipe_id)},{'$inc':{'upvotes': 1}})
return redirect(url_for('recipedetail',recipe_id=recipe_id))
@app.route('/delete_recipe/<recipe_id>')
def delete_recipe(recipe_id):
mongo.db.recipe.remove({'_id': ObjectId(recipe_id)})
return redirect(url_for('home'))
@app.route('/editrecipe/<recipe_id>')
def editrecipe(recipe_id):
edit_recipe = mongo.db.recipe.find_one({"_id": ObjectId(recipe_id)})
return render_template('editrecipe.html', recipe = edit_recipe)
@app.route('/updaterecipe/<recipe_id>', methods=['POST'])
def updaterecipe(recipe_id):
if request.method == 'POST':
updateRecipe = mongo.db.recipe
updateRecipe.update(
{"_id": ObjectId(recipe_id)},
{'name':request.form.get('name'),
'img':request.form.get('img'),
'author': request.form.get('author'),
'cuisine': request.form.get('cuisine'),
'date_added':request.form.get('date_added'),
'time_taken':request.form.get('time_taken'),
'desc':request.form.get('desc'),
'ingredients':request.form.get('ingredients'),
'instructions':request.form.get('instruction'),
'allergens':request.form.get('allergens'),
'upvotes':request.form.get('upvotes')}
)
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(host=os.environ.get('IP'),
port=int(os.environ.get('PORT', 4444)),
debug=True)