-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
450 lines (330 loc) · 12 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import os
import io
import qrcode
import base64
from datetime import datetime, timedelta
from flask import Flask, redirect, render_template, request
from flask_login import LoginManager, login_required, login_user, logout_user, current_user
from flask_restful import Api
from api import ShowAPI, VenueAPI
from models import User, db, Show, Venue, Booking, ScheduledAt, ShowTags
cwd = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
api = Api(app, prefix='/api')
login_manager = LoginManager(app)
app.secret_key = '9mwY7f-lEiSyHTXRX3nXMgyT98t5HRDnKxJaxTORbOo'
app.config['SECRET_KEY'] = '9mwY7f-lEiSyHTXRX3nXMgyT98t5HRDnKxJaxTORbOo'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
os.path.join(cwd, 'database.sqlite3')
db.init_app(app)
app.app_context().push()
db.create_all()
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(username):
return User.query.filter(User.username == username).first()
@app.template_filter('formatdate')
def date_format(d: datetime):
return d.date().strftime('%d %b, %Y')
@app.template_filter('formattime')
def time_format(d: datetime):
return d.time().strftime('%I:%M %p')
@app.template_filter('formatdatetime')
def datetime_format(d: datetime):
return d.strftime('%d %b, %Y %I:%M %p')
@app.get('/')
def index():
shows = db.session.query(Show)\
.join(ScheduledAt)\
.join(ShowTags, isouter=True)\
.order_by(ScheduledAt.show_date_time)\
.all()
venues = Venue.query.all()
return render_template('index.html', shows=shows, venues=venues)
@app.get('/shows')
def shows():
upcoming_shows = db.session.query(Show)\
.join(ShowTags, isouter=True)\
.join(ScheduledAt, isouter=True)\
.join(Venue, isouter=True)\
.filter(ScheduledAt.upcoming)\
.order_by(ScheduledAt.show_date_time)
all_shows = db.session.query(Show)\
.join(ShowTags, isouter=True)\
.join(ScheduledAt, isouter=True)\
.join(Venue, isouter=True)
role = current_user.role if current_user.is_authenticated else None
shows = upcoming_shows if role == 'user' else all_shows
args = request.args
if len(args) > 0:
venue = args.get('venue')
if venue != '':
shows = shows.filter(Venue.name == venue)
tag = args.get('tag')
if tag != '':
shows = shows.filter(ShowTags.tag == tag)
rating = args.get('rating')
shows = shows.filter(Venue.rating >= rating)
date_from = args.get('date_from')
if date_from:
shows = shows\
.filter(ScheduledAt.show_date_time >= datetime.fromisoformat(date_from))
date_to = args.get('date_to')
if date_to:
shows = shows\
.filter(ScheduledAt.show_date_time <= datetime.fromisoformat(date_to))
shows = shows.all()
venues = sorted(set([v.name for v in Venue.query.all()]))
tags = sorted(set([t.tag for t in ShowTags.query.all()]))
return render_template('shows.html', shows=shows, venues=venues, tags=tags)
@app.get('/venues')
def venues():
venues = db.session.query(Venue)
args = request.args
if len(args) > 0:
location = args.get('location')
if location != '':
venues = venues.filter(Venue.location == location)
rating = args.get('rating')
venues = venues.filter(Venue.rating >= rating)
venues = venues.all()
locations = sorted(set([v.location for v in Venue.query.all()]))
return render_template('venues.html', venues=venues, locations=locations)
@app.get('/show/<int:id>')
def show(id):
show = db.session.query(Show).get(id)
upcoming = any([x.upcoming for x in show.scheduled_at])
return render_template('show.html', show=show, upcoming=upcoming)
@app.get('/venue/<int:id>')
def venue(id):
venue = db.session.query(Venue).get(id)
upcoming = any([x.upcoming for x in venue.scheduled_shows])
return render_template('venue.html', venue=venue, upcoming=upcoming)
@app.get('/show/edit/<int:id>')
@login_required
def edit_show_form(id):
if (current_user.role != 'admin'):
return redirect('/')
show = db.session.query(Show).get(id)
venues = Venue.query\
.order_by(Venue.name)\
.distinct()
return render_template('edit_show.html', mode='edit', show=show, venues=venues)
@app.post('/show/edit/<int:id>')
@login_required
def edit_show(id):
if (current_user.role != 'admin'):
return redirect('/')
show = db.session.query(Show).get(id)
args = request.json
ds = args.get('show')
show.name = ds['name']
show.description = ds['description']
show.category = ds['category']
show.rating = ds['rating']
show.ticket_price = ds['ticket_price']
etag = [x.tag for x in show.tags]
for tag in ds['tags']:
if tag not in etag:
db.session.add(ShowTags(show_id=id, tag=tag))
for x in args.get('schedule_remove'):
s = db.session.query(ScheduledAt).get(int(x))
db.session.delete(s)
for x in args.get('schedule_add'):
show_date_time = datetime\
.strptime(x['show_date_time'], '%Y-%m-%dT%H:%M:%S.%fZ') \
+ timedelta(hours=5, minutes=30)
db.session.add(ScheduledAt(show_id=id,
venue_id=x['venue_id'],
show_date_time=show_date_time))
db.session.commit()
return {'id': show.id}
@app.get('/show/add')
@login_required
def add_show_form():
if (current_user.role != 'admin'):
return redirect('/')
venues = Venue.query\
.order_by(Venue.name)\
.distinct()
return render_template('edit_show.html', mode='add', show={}, venues=venues)
@app.post('/show/add')
@login_required
def add_show():
if (current_user.role != 'admin'):
return redirect('/')
show = Show()
args = request.json
ds = args.get('show')
show.name = ds['name']
show.description = ds['description']
show.category = ds['category']
show.rating = ds['rating']
show.ticket_price = ds['ticket_price']
db.session.add(show)
db.session.commit()
for tag in ds['tags']:
db.session.add(ShowTags(show_id=show.id, tag=tag))
for x in args.get('schedule_add'):
show_date_time = datetime\
.strptime(x['show_date_time'], '%Y-%m-%dT%H:%M:%S.%fZ') \
+ timedelta(hours=5, minutes=30)
db.session.add(ScheduledAt(show_id=show.id,
venue_id=x['venue_id'],
show_date_time=show_date_time))
db.session.commit()
return {'id': show.id}
@app.get('/venue/edit/<int:id>')
@login_required
def edit_venue_form(id):
if (current_user.role != 'admin'):
return redirect('/')
venue = db.session.query(Venue).get(id)
return render_template('edit_venue.html', venue=venue, mode='edit')
@app.post('/venue/edit/<int:id>')
@login_required
def edit_venue(id):
if (current_user.role != 'admin'):
return redirect('/')
venue = db.session.query(Venue).get(id)
args = request.form
venue.name = args['name']
venue.description = args['description']
venue.location = args['location']
venue.rating = args['rating']
venue.capacity = args['capacity']
db.session.commit()
return redirect(f'/venue/{id}')
@app.get('/venue/add')
@login_required
def add_venue_form():
if (current_user.role != 'admin'):
return redirect('/')
return render_template('edit_venue.html', venue={}, mode='add')
@app.post('/venue/add')
@login_required
def add_venue():
if (current_user.role != 'admin'):
return redirect('/')
venue = Venue()
args = request.form
venue.name = args['name']
venue.description = args['description']
venue.location = args['location']
venue.rating = args['rating']
venue.capacity = args['capacity']
db.session.add(venue)
db.session.commit()
return redirect(f'/venue/{venue.id}')
@app.get('/profile')
@login_required
def profile():
return render_template('profile.html')
@app.get('/profile/admin')
@login_required
def admin():
if (current_user.role == 'admin'):
return render_template('admin.html', shows=Show.query.all(), venues=Venue.query.all())
else:
return redirect('/profile')
@app.get('/tickets')
@login_required
def tickets():
tickets = db.session\
.query(Booking)\
.filter(Booking.user_id == current_user.id)\
.all()
buffers = [io.BytesIO() for _ in tickets]
qrs = [qrcode.make(f'{x.ticket.show.name} at {x.ticket.venue.name} on {x.ticket.show_date_time}')
for x in tickets]
for qr, buffer in zip(qrs, buffers):
qr.save(buffer, "JPEG")
encoded_qrs = [base64
.b64encode(x.getvalue())
.decode('utf-8')
for x in buffers]
return render_template('tickets.html', tickets=list(zip(tickets, encoded_qrs)))
@app.get('/book/<int:id>')
@login_required
def booking_form(id):
return render_template('book_ticket.html', scheduled_show=db.session.query(ScheduledAt).get(id))
@app.post('/book/<int:id>')
@login_required
def book_ticket(id):
n_tickets = request.form.get('no_of_tickets')
ticket = Booking(user_id=current_user.id,
svd_id=id, no_of_tickets=n_tickets)
db.session.add(ticket)
db.session.commit()
return redirect('/tickets')
@app.post('/schedule')
def schedule_template():
args = request.json
venue = db.session.query(Venue).get(args.get('venue_id'))
show_date_time = datetime\
.strptime(args.get('show_date_time'), '%Y-%m-%dT%H:%M:%S.%fZ') \
+ timedelta(hours=5, minutes=30)
shows = db.session.query(ScheduledAt)\
.filter(ScheduledAt.venue_id == int(args.get('venue_id')))
show_id = args.get('show_id')
if show_id != '':
shows = shows.filter(ScheduledAt.show_id == int(show_id))
shows = shows.all()
if any([x.show_date_time == show_date_time for x in shows]):
return '', 400
return render_template('scheduled_show.html', venue=venue, show_date_time=show_date_time)
@app.get('/signup')
def signup_form():
return render_template('signup.html')
@app.post('/signup')
def signup():
args = request.form
if (db.session.query(User).filter(User.username == args.get('username')).first()):
return render_template('signup.html', error='The entered username is taken.')
if (db.session.query(User).filter(User.email == args.get('email')).first()):
return render_template('signup.html', error='The entered email is associated with another account.')
if (args.get('password') != args.get('password-confirm')):
return render_template('signup.html', error='Please enter matching passwords!')
user = User(
first_name=args.get('first_name'),
last_name=args.get('last_name'),
username=args.get('username'),
email=args.get('email'),
password=args.get('password'),
role='user',
authenticated=False
)
db.session.add(user)
db.session.commit()
return render_template('login.html')
@app.get('/login')
def login_form():
return render_template('login.html')
@app.post('/login')
def login():
if not request.form:
return {'message': 'send credentials to authenticate'}, 406
info = request.form
username = info.get('username', 'guest')
password = info.get('password', '')
user = User.query\
.filter(User.username == username)\
.filter(User.password == password)\
.first()
if user:
user.authenticated = True
login_user(user)
return redirect('/')
else:
return render_template('login.html', error=True), 401
@app.route("/logout")
@login_required
def logout():
logout_user()
current_user.authenticated = False
return redirect('/')
api.add_resource(ShowAPI, '/shows', '/shows/<int:id>', endpoint='api-shows')
api.add_resource(VenueAPI, '/venues', '/venues/<int:id>',
endpoint='api-venues')
if __name__ == "__main__":
app.run(debug=True)