-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
391 lines (370 loc) · 13.3 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from flask import Flask
from flask import send_from_directory
from flask import session,render_template,url_for,redirect,request
import database
import datetime
import json
import uuid
import os
import time
#left to do: populate dash board with actual data
#data need to lead to event's detail's page properly
# user needs to be able to participate in organization
app = Flask(__name__)
app.secret_key="secret key" # Since we'll be using sessions
app.config['UPLOAD_FOLDER'] = 'static/Uploads'
def validate(date_text, date_format):
try:
datetime.datetime.strptime(date_text, date_format)
return True
except ValueError:
return False
def parse_date(date_text):
dates = date_text.split('-')
return datetime.date(int(dates[0]), int(dates[1]), int(dates[2]))
def parse_time(time_text):
times = time_text.split(':')
return datetime.time(int(times[0]),int(times[1]),int(times[2]))
@app.route("/")
def index():
if not session.has_key('user'):
return redirect(url_for('identify'))
return redirect(url_for("user_dashboard"))
@app.route('/identify',methods=['GET','POST'])
def identify():
if request.method=="GET":
return render_template('identify.html')
elif request.method=="POST":
if request.form['button']=='user':
return redirect(url_for("user_login"))
elif request.form['button'] == 'organization':
return redirect(url_for("org_login"))
@app.route('/userlogin',methods=['GET','POST'])
def user_login():
if request.method=="GET":
return render_template('login.html', incorrect=False, action_name="userlogin", sign_up_redir="/usersignup")
elif request.method=="POST":
if database.user_login(request.form['login_username'], request.form['login_password']):
session["user"]=request.form['login_username']
return redirect(url_for("user_dashboard"))
else:
return render_template('login.html', incorrect=True, action_name="userlogin", sign_up_redir="/usersignup")
@app.route('/userdash', methods=['GET', 'POST'])
def user_dashboard():
user = session["user"]
name = database.find_user(user)
user_invites_header = ["event id", "event name", "organizer", "date", "location"]
invites_data = database.future_events_with_status(user, 3)
if request.method=="GET":
events_data = database.user_future_events(user)
past_events = database.user_past_events(user)
return render_template(
'dash.html',
user_first=name,
invites_table = "Received Invites",
invites_table_header=user_invites_header,
invites_table_data=invites_data,
event_redir="/user_display_event_read/",
invite_redir="/user_display_event/",
events_table = "Upcoming Events",
events_table_header= user_invites_header,
events_table_data= events_data,
search_redir="/userdash",
past_events_table = "Past Events Attended",
past_events_table_header = user_invites_header,
past_events_table_data = past_events)
if request.method=="POST":
events_data = database.user_future_events(user)
past_events = database.user_past_events(user)
query = request.form["query"]
if query != "":
try:
events_data = database.user_invites_by_category(user, request.form["query"])
print events_data
except:
print "invalid query"
return render_template(
'dash.html',
user_first=str(name[0]),
invites_table = "Received Invites",
invites_table_header=user_invites_header,
invites_table_data=invites_data,
event_redir="/user_display_event_read/",
invite_redir="/user_display_event/",
events_table = "Upcoming Events",
events_table_header= user_invites_header,
events_table_data= events_data,
search_redir="/userdash",
past_events_table = "Past Events Attended",
past_events_table_header = user_invites_header,
past_events_table_data = past_events)
@app.route('/orgdash', methods=['GET', 'POST'])
def org_dashboard():
user = session["user"]
name = database.find_organization(user)
events_header = ["id", "name", "date", "building", "room"]
invites_header = ["id", "name", "user"]
if request.method=="GET":
invites_data = database.all_org_invites(user)
events_data = database.events_created_by_org(user)
return render_template(
'dash.html',
user_first=str(name),
nav_redir="/create_event",
redir_name="Create Events",
invites_table = "Upcoming Events' Invites",
invites_table_header = invites_header,
invites_table_data = invites_data,
events_table = "Events Created by Me",
events_table_header = events_header,
events_table_data = events_data,
invite_redir="/org_display_event/",
event_redir="/org_display_event/")
if request.method=="POST":
invites_data = database.all_org_invites(user)
events_data = database.events_created_by_org(user)
query = request.form["query"]
if query != "":
try:
events_data = database.org_events_by_category(user, request.form["query"])
except:
print "invalid query"
return render_template(
'dash.html',
user_first=str(name),
nav_redir="/create_event",
redir_name="Create Events",
invites_table = "Upcoming Events' Invites",
invites_table_header = invites_header,
invites_table_data = invites_data,
events_table = "Events Created by Me",
events_table_header = events_header,
events_table_data = events_data,
invite_redir="/org_display_event/",
event_redir="/org_display_event/")
@app.route('/orglogin',methods=['GET','POST'])
def org_login():
if request.method=="GET":
return render_template('login.html', incorrect=False, action_name="orglogin", sign_up_redir="/orgsignup")
elif request.method=="POST":
if database.organization_login(request.form['login_username'], request.form['login_password']):
session["user"]=request.form['login_username']
return redirect(url_for("org_dashboard"))
else:
return render_template('login.html', incorrect=True, action_name="orglogin", sign_up_redir="/orgsignup")
@app.route('/create_event',methods=['GET','POST'])
def create_event():
user = session['user']
locations = database.get_locations()
categories = database.get_categories()
if request.method=="GET":
return render_template("event_form.html", locations=locations, categories=categories, incorrect=False, dash_redir='/orgdash')
if request.method=="POST":
try:
filePath = request.form['filePath']
name = request.form["name"]
description = request.form["description"]
date = request.form["date"]
start_time = request.form["start_time"]
end_time = request.form["end_time"]
location = request.form["location_hid"]
category = request.form["category_hid"]
building = None
room = None
if location is not None:
locations_tmp = location.split(" ")
building = locations_tmp[0]
room = locations_tmp[1]
result = database.create_event(name,
parse_date(date),
parse_time(start_time),
parse_time(end_time),
description,
filePath,
user,
building,
room)
if result != False:
database.create_event_category(result, category)
return redirect(url_for("org_display_event", eid=int(result)))
else:
return render_template("event_form.html", locations=locations, categories=categories, incorrect=True, dash_redir='/orgdash')
except:
return render_template("event_form.html", locations=locations, categories=categories, incorrect=True, dash_redir='/orgdash')
@app.route('/org_display_event/<int:eid>',methods=['GET', 'POST'])
def org_display_event(eid):
event = database.find_event(eid)[0]
category = database.get_categories_of_event(eid)
if request.method=='GET':
return render_template("event.html",
name=event[0],
filepath=event[5],
description=event[4],
date=event[1],
start_time=event[2],
end_time=event[3],
location=event[7]+" "+event[8],
category=str(category),
organizer=event[6],
event=True,
incorrect=False,
success=False,
eid=eid,
dash_redir='/orgdash')
if request.method=='POST':
username=request.form["name"]
if database.create_invitation(username, session["user"], eid):
return render_template("event.html",
name=event[0],
filepath=event[5],
description=event[4],
date=event[1],
start_time=event[2],
end_time=event[3],
location=event[7]+" "+event[8],
category=str(category),
organizer=event[6],
event=True,
incorrect=False,
success=True,
eid=eid,
dash_redir='/orgdash')
else:
return render_template("event.html",
name=event[0],
filepath=event[5],
description=event[4],
date=event[1],
start_time=event[2],
end_time=event[3],
location=event[7]+" "+event[8],
category=str(category),
organizer=event[6],
event=True,
incorrect=True,
success=False,
eid=eid,
dash_redir='/orgdash')
@app.route('/user_display_event/<int:eid>',methods=['GET', 'POST'])
def user_display_event(eid):
event = database.find_event(eid)[0]
category = database.get_categories_of_event(eid)
if request.method=='GET':
return render_template("event.html",
name=event[0],
filepath=event[5],
description=event[4],
date=event[1],
start_time=event[2],
end_time=event[3],
location=event[7]+" "+event[8],
category=str(category),
organizer=event[6],
event=False,
incorrect=False,
success=False,
eid=eid,
dash_redir='/userdash',
user_decide=True)
if request.method=='POST':
value = request.form["invite"]
status_id = 3
if value == "Accept":
status_id = 1
elif value == "Decline":
status_id = 2
if status_id == 1 or status_id == 2:
database.update_invite(session["user"], event[6], eid, status_id)
return redirect(url_for("user_dashboard"))
if value == "Participate":
org_username = request.form["name"]
success = database.participates_in_org(session["user"], org_username)
print success
return render_template("event.html",
name=event[0],
filepath=event[5],
description=event[4],
date=event[1],
start_time=event[2],
end_time=event[3],
location=event[7]+" "+event[8],
category=str(category),
organizer=event[6],
event=False,
incorrect=(not success),
success=success,
eid=eid,
dash_redir='/userdash',
user_decide=True)
@app.route('/user_display_event_read/<int:eid>', methods=['GET'])
def user_display_event_read(eid):
event = database.find_event(eid)[0]
category = database.get_categories_of_event(eid)
if request.method=='GET':
return render_template("event.html",
name=event[0],
filepath=event[5],
description=event[4],
date=event[1],
start_time=event[2],
end_time=event[3],
location=event[7]+" "+event[8],
category=str(category),
organizer=event[6],
event=False,
incorrect=False,
success=False,
eid=eid,
dash_redir='/userdash',
user_decide=False)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
print send_from_directory(app.config['UPLOAD_FOLDER'],filename)
return send_from_directory(app.config['UPLOAD_FOLDER'],filename)
@app.route('/orgsignup',methods=['GET','POST'])
def org_sign_up():
if request.method=="GET":
return render_template('signup.html', incorrect=False, user=False, action_name="orgsignup")
elif request.method=="POST":
username = request.form["username"]
password = request.form["password"]
confirm_pass = request.form["confirm"]
name = request.form["org_name"]
if password != confirm_pass or not database.create_organization(name, username, password):
return render_template('signup.html', incorrect=True, user=False, action_name="orgsignup")
else:
session["user"] = username
return redirect(url_for("org_dashboard"))
@app.route('/usersignup',methods=['GET','POST'])
def user_sign_up():
if request.method=="GET":
return render_template('signup.html', incorrect=False, user=True, action_name="usersignup")
elif request.method=="POST":
username = request.form["username"]
password = request.form["password"]
confirm_pass = request.form["confirm"]
first_name = request.form["first_name"]
last_name = request.form["last_name"]
if password != confirm_pass or not database.create_user(first_name, last_name, username, password):
return render_template('signup.html', incorrect=True, user=True, action_name="usersignup")
else:
session["user"] = username
return redirect(url_for("user_dashboard"))
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
file.save(os.path.join(app.config['UPLOAD_FOLDER'], f_name))
return json.dumps({'filename':f_name})
@app.route('/about')
def about():
return render_template('index.html', data=['log in user'])
@app.route("/logout")
def logout():
session.pop('user')
return redirect(url_for('identify'))
if __name__=="__main__":
app.debug=True
app.run()