-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
251 lines (147 loc) · 6.13 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
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
"""Server for NPS Guide."""
from flask import (Flask, render_template, request, jsonify,
flash, session, redirect)
from model import User, connect_to_db
from datetime import datetime
import crud
import os
import sys
import requests
import json
from jinja2 import StrictUndefined
"""Server for nps app."""
app = Flask(__name__)
app.secret_key = "lans"
app.jinja_env.undefined = StrictUndefined
########################## USER ROUTES ###################################
@app.route('/')
def homepage():
"""Shows the homepage."""
return render_template('homepage.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
"""User login."""
if request.method == 'POST':
session.pop('user_id', None)
email = request.form['email']
password = request.form['password']
user = crud.get_user_by_email(email)
if not user:
flash('Account does not exist. Please try again.')
return redirect('/login')
elif user.password != password:
flash('Incorrect Password. Please try again.')
return redirect('/login')
elif user:
session['user'] = email
session['user_id'] = user.user_id
# flash('Successfully logged in!')
return redirect('/parks')
return render_template('login.html')
def is_logged_in():
"""Checking if there is a user logged in."""
return 'user_id' in session
def logged_in_user():
"""Get information on the user that is logged in."""
user = User.query.get(session['user_id'])
return user
@app.route('/register', methods=['GET', 'POST'])
def register():
"""User registration."""
if request.method == 'POST':
fname = request.form.get('fname')
lname = request.form.get('lname')
email = request.form.get('email')
password = request.form.get('password')
user = crud.get_user_by_email(email)
if user:
flash('Cannot create an account with that email. Please try again.')
else:
crud.create_user(fname, lname, email, password)
session['user'] = email
session['user_id'] = user
return redirect('/parks')
# else:
# return render_template('registration.html')
return render_template('registration.html')
@app.route('/logout', methods=['GET'])
def logout():
"""User logout."""
if 'user' in session:
del session['user']
flash('Logged Out.')
return redirect('/')
########################## PARK & ACTIVITY ROUTES ###################################
@app.route('/parks/<park_id>')
def show_park(park_id):
"""Show the details on a particular park."""
park = crud.get_park_by_id(park_id)
return render_template('park_details.html', park=park)
@app.route('/parks', methods=['GET'])
def all_parks():
"""Retrieve parks."""
parks = crud.get_parks()
return render_template('parks.html', parks=parks)
@app.route('/activities/<activity_id>')
def show_activity(activity_id):
"""Show the details on a particular activity."""
activity = crud.get_activity_by_id(activity_id)
return render_template('activity_details.html', activity=activity)
@app.route('/activities', methods=['GET'])
def get_activities():
"""Retrieve activities."""
activities = crud.get_activities()
return render_template('activities.html', activities=activities)
########################## BUCKETLIST ROUTES ###################################
@app.route('/profile/<user_id>')
def user_profile(user_id):
"""Shows a users profile with all their bucketlists."""
email = session['user']
user = crud.get_user_by_email(email)
# print("EMAIL", email) #shows the users email
# print("USER", user) #shows <User user_id=60 email=dan@yahoo.com>
bucketlists = crud.get_bucketlist_by_user(user.user_id)
# print("BUCKETLIST", bucketlists)
return render_template('user_profile.html', user=user, bucketlists=bucketlists)
@app.route('/bucketlists/<bucketlist_id>')
def get_specific_bucketlist(bucketlist_id):
"""Shows a user the details for a specific bucketlist."""
bucketlist = crud.get_bucketlist_by_id(bucketlist_id)
return render_template('bucketlist_details.html', bucketlist=bucketlist)
@app.route('/profile/bucketlists/<bucketlist_id>')
def select_bucketlist_from_profile(bucketlist_id):
"""A user can access a specific bucketlist from a link in their profile."""
bucketlist = crud.get_bucketlist_by_id(bucketlist_id)
return render_template('bucketlist_details.html', bucketlist=bucketlist)
#FIX THIS LATER - don't need two of the same route. should be able to delete this one
@app.route('/adding-activities', methods=['POST', 'GET'])
def adding_to_a_bucketlist():
"""Creates a new bucketlist for a user."""
email = session['user']
user = crud.get_user_by_email(email)
# user_id = session['user_id'] #returns None
park_id = request.form.get('park_id')
activity_list = request.form.getlist('activities')
if activity_list == []:
flash('Oops, no activity was selected. Please try again')
return redirect(f'parks/{park_id}')
bucketlist = crud.get_bucketlist_by_park_and_user(park_id, user.user_id)
if not bucketlist:
bucketlist = crud.create_bucketlist(user.user_id, park_id)
for activity_id in activity_list:
new_bucketlist_item = crud.create_bucketlist_item(bucketlist.bucketlist_id, activity_id, datetime.now())
return redirect(f'bucketlists/{bucketlist.bucketlist_id}')
@app.route('/saving-order', methods=['GET', 'POST'])
def saving_order():
"""Saves the date that a user enters to complete activity."""
user_id = session['user_id']
order_date = request.form.get('order-date')
item_id = request.form.get('item_id')
item = crud.update_bucketlistitem_order(item_id, order_date)
return jsonify({"status":"Successful", "date": item.order.strftime('%m/%d/%Y'), "item_id" : item.item_id})
if __name__ == '__main__':
connect_to_db(app)
app.run()
# app.run(debug=True, host='0.0.0.0')
#Post is used to send data
#Get is used to request data