-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
115 lines (91 loc) · 3.18 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
from flask import Flask
from flask import flash, render_template, request, url_for, session, redirect
from auth import Auth
from recipes import Recipes
app = Flask(__name__)
app.secret_key = b'aj(>,m87hJn9+-alkjns*jkj90($'
# Behövs inte röras.
@app.route("/login/", methods=["POST", "GET"])
def login():
if request.method == 'GET':
return render_template('login.html')
username = request.form.get('username', None)
password = request.form.get('password', None)
auth = Auth()
if auth.login(username, password):
return redirect(url_for('show_recipes'))
flash('Could not log you in')
return render_template('login.html')
@app.route("/sign-up/", methods=["POST", "GET"])
def sign_up():
if request.method == 'GET':
return render_template('sign-up.html')
username = request.form.get('username', None)
password = request.form.get('password', None)
password2 = request.form.get('password2', None)
auth = Auth()
error = None
if auth.has_user(username):
error = 'There is already a user with that name'
elif password != password2:
error = 'Passwords must match'
if error:
flash(error)
return redirect(url_for('sign_up'))
auth.create_user(username, password)
auth.login(username, password)
return redirect(url_for('show_recipes'))
@app.route("/logout/")
def logout():
auth = Auth()
auth.logout()
return redirect(url_for('login'))
#---- Jag Jobbade Här men är Klar nu ----------
@app.route("/")
@app.route("/recipes/")
def show_recipes():
'''If logged in, show recipes for the current user.
Otherwise, redirect to the Login page.'''
# Hämtar mallen Recipies.
recipes = Recipes()
# Hämtar mallen Auth.
auth = Auth()
# Kollar om User är online.
if auth.is_logged_in() == True:
# I dont understand this.... user_id
user_id = auth.get_current_user()['user_id']
# Test skriver ut user_id i terminalen.
print(user_id)
# Hämtar funktionen get_recipes. Med user_id.
recipes = recipes.get_recipes(user_id)
return render_template('recipes.html', recipes=recipes)
else:
return redirect(url_for('login'))
#-------------------------------------------------------------
@app.route("/recipes/new/", methods=['GET', 'POST'])
def add_recipe():
'''If request method is GET, load the 'add recipe' page.
If request method is POST:
1. Get all posted form data
2. Get ID of currently logged-in user
3. Call the relevant function of the Recipes class to add/create
a new record in the recipes table. Pass in the relevant
information as arguments to the function.
4. Redirect to the Recipes (listing) page.'''
if request.method == "GET":
return render_template('add_recipe.html')
if request.method == "POST":
recipe = Recipes()
auth = Auth()
description = request.form.get('description', None)
ingredients = request.form.get('ingredients', None)
user_id = auth.get_current_user()[0]
recipe.add_recipe( description, ingredients, user_id)
return redirect(url_for('show_recipes'))
#------------------------------------------------------
@app.route("/recipes/<int:recipe_id>/")
def show_recipe(recipe_id):
'''Show the recipe that matches the given recipe ID.'''
recipe = Recipes()
recipe = recipe.get_recipe(recipe_id)
return render_template('recipe.html', recipe = recipe)