-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
163 lines (109 loc) · 4.17 KB
/
server.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
from jinja2 import StrictUndefined
from flask import Flask, render_template, request, flash, redirect, session, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from model import *
app = Flask(__name__)
# Required to use Flask sessions and the debug toolbar
app.secret_key = "super-secret"
# raise an error in Jinja2 when using an undefined variable
app.jinja_env.undefined = StrictUndefined
debug = '\n' * 3
@app.route('/')
def index():
"""Defaults user view to today's log entries"""
return render_template('home.html')
@app.route('/register', methods=['GET'])
def register_form():
"""Show form for user signup"""
return render_template('login.html')
@app.route('/register', methods=['POST'])
def register_process():
"""Create new user in the database"""
register_form = request.form
if User.query.filter(User.email == register_form['email']).first():
flash('Email already exists within our userbase')
return redirect('/register')
elif register_form['password'] != register_form['confirm_password']:
flash('Passwords do not match')
return redirect('/register')
else:
new_user = User(email=register_form['email'],
password=register_form['password'])
db.session.add(new_user)
db.session.commit()
flash('Added new user')
session['user_id'] = new_user.id
flash('Successfully logged in')
return redirect('/')
@app.route('/login', methods=['POST'])
def process_login():
"""Log a user in if the user is in the database and provides correct password"""
login_attempt = request.form
user = User.query.filter(User.email == login_attempt['email']).first()
# check if a user exists in the database
if not user:
flash('Incorrect email or password')
return redirect("/login")
# check if their password matches
elif user.password != login_attempt['password']:
flash('Incorrect password or email')
return redirect("/login")
# if yes to both above, add user_id to session data
else:
session['user_id'] = user.id
flash('Successfully logged in')
return redirect('/')
@app.route('/logout')
def process_logout():
"""Log a user out by deleting their session variable"""
del session['user_id']
flash('Successfully logged out')
return redirect("/register")
@app.route('/api/goals', methods=['GET'])
def read_goals():
"""Returns a list of goals for the logged in user"""
user_id = session['user_id']
goal_query = Goal.query.filter(Goal.user_id == user_id) \
.order_by(Goal.id) \
.all()
goals = []
for goal in goal_query:
goals.append({'goal_id': goal.id, 'description': goal.description})
print('\n\n\n\n')
print('GOALS', goals)
return jsonify(goals)
@app.route('/api/goals', methods=['POST'])
def add_goal():
"""Adds a new goal to the database for the logged in user"""
user_id = session['user_id']
description = request.form.get('goal_description')
new_goal = Goal(user_id=user_id, description=description)
db.session.add(new_goal)
db.session.commit()
return "New goal has been added!"
@app.route('/api/goals/update', methods=['POST'])
def update_goal():
"""Updates an existing goal to the database for the logged in user"""
user_id = session['user_id']
description = request.form.get('goal_description')
goal_id = request.form.get('goal_id')
existing_goal = Goal.query.get(goal_id)
existing_goal.description = description
db.session.commit()
return "Goal has been updated!"
@app.route('/api/goals/delete', methods=['POST'])
def delete_goal():
"""Deletes an existing goal to the database for the logged in user"""
user_id = session['user_id']
goal_id = request.form.get('goal_id')
existing_goal = Goal.query.get(goal_id)
db.session.delete(existing_goal)
db.session.commit()
return "Goal has been deleted!"
if __name__ == '__main__':
# Remove debug for demo
app.debug = True
connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
app.run(host='0.0.0.0')