-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
312 lines (204 loc) · 7.77 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""Covid19 Hackathon"""
from jinja2 import StrictUndefined
from flask import Flask, render_template, request, flash, redirect, session, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from model import connect_to_db, db, User, Relational, Trip, Wishlist
from queryuser import add_wishlist, get_wishlist, update_status, get_wishlists
from query_volunteer import vol_update_status
app = Flask(__name__)
app.jinja_env.undefined = StrictUndefined
app.secret_key= "ABC"
@app.route("/")
def homepage():
"""Show the homepage."""
return render_template("index.html")
@app.route("/user")
def display_user_type():
"""Show a transit page for user to choose to be volunteer or create wishlist."""
return render_template("user.html")
@app.route("/login", methods=["POST"])
def login_page():
"""Log in user and return to homepage."""
email = request.form.get("email")
print(f'\n\nemail: {email}')
password = request.form.get("password")
user = User.query.filter_by(email=email).first()
if user:
if user.check_password(password):
session["user_id"] = user.user_id
flash("Successfully logged in!")
return redirect("/user")
else:
flash("Incorrect password, please try again.")
return redirect("/")
else:
flash("No user found with that username. Please register for an account.")
return redirect("/register")
@app.route("/register")
def show_registration_page():
"""Show page for use to register for an account."""
return render_template("register.html")
@app.route("/register", methods=["POST"])
def register_user():
"""Create account for user by adding them to database."""
email = request.form.get("email")
username = request.form.get("username")
password = request.form.get("password")
zipcode = request.form.get("zipcode")
if not User.query.filter_by(user_full_name=username).all():
new_user = User(email=email,
user_full_name=username,
uzipcode=zipcode)
new_user.set_password(password)
db.session.add(new_user)
db.session.commit()
session["user_id"] = new_user.user_id
flash("Successfully created an account!")
return redirect("/user")
else:
flash("User already exists! Try logging in instead.")
return redirect("/login")
@app.route("/logout")
def logout_user():
"""Log user out of current session."""
if session.get("user_id") != None:
del session["user_id"]
flash("Successfully logged out!")
return redirect("/")
else:
flash("You're not currently logged in!")
return redirect("/")
@app.route("/asker-homepage")
def show_asker_homepage():
"""Show homepage for asker."""
return render_template("asker.html")
@app.route("/create", methods=["POST"])
def create_wishlist():
"""Get asker's wishlist and zipcode to save in database."""
new_wishlist = request.args.get('wishlist')
zipcode = request.args.get('zipcode')
asker = session.get("user_id")
status = "incomplete"
# if not Trip.query.filter_by(trip=username).all():
new_trip = Trip(user_id=asker,
wishlist=new_wishlist,
trip_zipcode=zipcode,
item_progress=status)
db.session.add(new_trip)
db.session.commit()
session["trip_id"] = new_trip.trip_id
flash("Successfully created a wishlist!")
print("200")
return redirect("/asker-homepage")
@app.route("/incomplete")
def asker_view_wishlist():
"""Display wishlist."""
asker = session.get("user_id")
print(asker)
incomplete_order = get_wishlists(asker)
print(incomplete_order)
return jsonify(incomplete_order)
@app.route("/inprogress")
def status_in_progress():
"""Update wishlist status to in progress."""
asker = session.get("user_id")
new_status = update_status(asker)
return new_status
@app.route("/completed")
def status_completed():
"""Update wishlist status to completed."""
asker = session.get("user_id")
new_status = update_status(asker)
return new_status
# return render_template("volunteer.html")
@app.route("/volunteer-homepage")
def show_volunteer_homepage():
"""Show homepage for volunteer and displays active orders."""
return render_template("volunteer.html")
@app.route('/trips.json')
def trip_info():
"""Retrieving status of asker's wishlist"""
volunteer = session.get('user_id')
trip = db.session.query(Trip.trip_id, Trip.item_progress)\
.join(Relational, Trip.trip_id == Relational.r_trip_id)\
.filter(Relational.r_vol_id == volunteer).all()
print("\n\n")
print(f"trip{trip}")
tripList = []
if trip:
tripList.append({
'trip_id': trip[0][0],
'trip_progress': trip[0][1]
})
print(tripList)
return jsonify(tripList)
else:
tripList.append({
'trip_id': None,
'trip_progress': None
})
return jsonify(tripList)
@app.route('/volunteer-all-wishlist')
def display_asker_wishlists():
"""Displays all avaliable wishlists"""
return render_template("wishlists.html")
@app.route('/wishlistInfo.json')
def viewwishlists():
""" """
user_id = session.get('user_id')
print(user_id)
zipcode = db.session.query(User.uzipcode).filter(User.user_id==user_id).first()
print(zipcode)
trips = Trip.query.filter_by(trip_zipcode=zipcode).all()
print(trips)
wishlistList = []
for trip in trips:
print(trip.trip_id)
wishlistList.append({
'wishlist_id': trip.trip_id,
'wishlist_progress': trip.item_progress,
'wishlist': trip.wishlist
# shows number of items on list before clicking on wishlist
})
return jsonify(wishlistList)
@app.route('/volunteer-wishlist/<int:trip_id>')
def display_asker_single_wishlist(trip_id):
"""Displays selected wishlist (single)"""
trip = Trip.query.filter_by(trip_id=trip_id).first()
session['wishlist_id'] = trip.trip_id
trip_info = {
'wishlist_id': trip.trip_id,
'wishlist_progress': trip.item_progress,
'wishlist': trip.wishlist
}
data = trip_info
return render_template("single_wishlist.html", data=data)
@app.route("/inprogress", methods=["POST"])
def vol_status_in_progress():
"""Update wishlist status to in progress."""
volunteer = session.get("wishlist_id")
current_status = 'In Progress'
trip = Trip.query.filter_by(trip_id=volunteer).first()
trip.item_progress = current_status
new_status = vol_update_status(volunteer)
db.session.commit()
return new_status
@app.route("/completed", methods=["POST"])
def vol_status_completed():
"""Update wishlist status to completed."""
volunteer = session.get("wishlist_id")
current_status = 'Completed!'
trip = Trip.query.filter_by(trip_id=volunteer).first()
trip.item_progress = current_status
new_status = vol_update_status(volunteer)
db.session.commit()
return new_status
@app.route("/about")
def about():
"""about page"""
return render_template("about.html")
if __name__ == "__main__":
app.debug = False #pragma: no cover
connect_to_db(app) #pragma: no cover
DebugToolbarExtension(app) #pragma: no cover
app.run(host="0.0.0.0") #pragma: no cover