-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
530 lines (453 loc) · 16.7 KB
/
app.py
File metadata and controls
530 lines (453 loc) · 16.7 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from distutils.command.config import config
from itertools import count
import json
from pyexpat import model
from unittest import result
from flask_moment import Moment
from flask import Flask, render_template, request, Response, flash, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
from flask_migrate import Migrate
import datetime
from forms import *
from models import *
from config import *
from filters import *
#----------------------------------------------------------------------------#
# Configuration
#----------------------------------------------------------------------------#
app = Flask(__name__)
moment = Moment(app)
app.config.from_object('config')
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config ['PYTHONUNBUFFERED'] = ""
app.config ['DEBUG'] = True
db = SQLAlchemy(app)
migrate = Migrate(app, db, render_as_batch=True)
#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#
@app.route('/')
def index():
return render_template('pages/home.html')
#----------------------------------------------------------------------------#
# Venues.
#----------------------------------------------------------------------------#
@app.route('/venues')
def venues(venue_id):
num_upcoming_shows = db.session.query(Shows, Venue).select_from(Shows)\
.join(Venue).filter(Shows.venue_id==venue_id).filter(Shows.start_time>datetime.now()).all()
venues = []
result = Venue.query.all()
flash(result)
for row in result:
venue={
'city' : {row.city},
'state' : {row.state},
"id" : {row.id},
"name" : {row.name},
"num_upcoming_shows" : {row.num_upcoming_shows}
}
venues.append(venue)
return render_template('pages/venues.html', venue=venue, num_upcoming_shows=num_upcoming_shows);
#-- SEARCH
@app.route('/venues/search', methods=['GET', 'POST'])
def search_venues():
venues = request.args.get("venues")
all_venues = Venue.query.filter(Venue.name.ilike("%"+ venues + "%")).all()
return render_template('pages/search_venues.html', venues=venues, all_venues=all_venues)
#-- SHOW VENUES
@app.route('/venues/<int:venue_id>')
def show_venues(venue_id):
upcoming_shows = db.session.query(Shows, Venue).select_from(Shows)\
.join(Venue).filter(Shows.venue_id==venue_id).filter(Shows.start_time>datetime.now()).all()
past_shows = db.session.query(Shows, Venue).select_from(Shows)\
.join(Venue).filter(Shows.venue_id==venue_id).filter(Shows.start_time<datetime.now()).all()
past_shows_count = count(past_shows)
upcoming_shows_count = count(upcoming_shows)
show_venues = []
result = Venue.query.get(venue_id)
flash(result)
for row in result:
show_venue={
'city' : {row.city},
'state' : {row.state},
"id" : {row.id},
"name" : {row.name},
"genres": {row.genres},
"address": {row.address},
"phone": {row.phone},
"website": {row.website},
"facebook_link": {row.facebook_link},
"seeking_talent": {row.seeking_talent},
"seeking_description": {row.seeking_description},
"image_link": {row.image_link},
"past_shows": [{
"artist_id": {row.show.artist_id},
"artist_name":{row.show.artist_name},
"artist_image_link":{row.show.artist_image_link},
"start_time":{row.show.start_time}
}],
"upcoming_shows":{row.upcoming_shows},
"past_shows_count":{row.past_shows_count},
"upcoming_shows_count":{row.upcoming_shows_count}
}
show_venues.append(show_venue)
return render_template('pages/show_venue.html', show_venue=show_venue, upcoming_shows=upcoming_shows,
upcoming_shows_count=upcoming_shows_count, past_shows=past_shows, past_shows_count=past_shows_count)
#-- CREATE VENUE
@app.route('/venues/create', methods=['GET'])
def create_venue_form():
form = VenueForm()
return render_template('forms/new_venue.html', form=form)
@app.route('/venues/create', methods=['POST'])
def create_venue_submission():
error = False
try:
form = VenueForm(request.form)
venue = Venue(
name = form.name.data,
city = form.city.data,
state = form.state.data,
address = form.address.data,
phone = form.phone.data,
genres = form.genres.data,
facebook_link = form.facebook_link.data,
image_link = form.image_link.data,
website_link = form.website_link.data,
seeking_talent = form.seeking_talent.data,
seeking_description = form.seeking_description.data,
)
db.session.add(venue)
db.session.commit()
flash('Venue: {0} created successfully'.format(venue.name))
except Exception as err:
flash('An error occurred creating the Venue: {0}. Error: {1}'.format(venue.name, err))
db.session.rollback()
finally:
db.session.close()
if not error:
return render_template('pages/home.html', venue=Venue.query.all())
@app.route('/venues/<venue_id>', methods=['DELETE'])
def delete_venue(venue_id):
venue_id = venue_id
delete_venue = Venue.query.filter_by(venue_id = venue_id).delete()
db.session.commit()
return render_template('pages/home.html', delete_venue=delete_venue)
# Artists
# ----------------------------------------------------------------
@app.route('/artists')
def artists():
artists = []
result = Artist.query.all()
flash(result)
for row in result:
artist={
"id" : {row.id},
"name" : {row.name}
}
artists.append(artist)
return render_template('pages/artist.html', artists=artist)
#-- SEARCH
@app.route('/artists/search', methods=['POST'])
def search_artists():
artist = request.args.get("artist")
all_artist = Venue.query.filter(Venue.name.ilike("%"+ artist + "%")).all()
return render_template('pages/search_artist.html', artist=artist, all_artist=all_artist)
#-- SHOW ARTIST
@app.route('/artists/<int:artist_id>')
def show_artist(artist_id):
upcoming_shows = db.session.query(Shows, Artist).select_from(Shows)\
.join(Artist).filter(Shows.artist_id==artist_id).filter(Shows.start_time>datetime.now()).all()
past_shows = db.session.query(Shows, Artist).select_from(Shows)\
.join(Artist).filter(Shows.artist_id==artist_id).filter(Shows.start_time<datetime.now()).all()
past_shows_count = count(past_shows)
upcoming_shows_count = count(upcoming_shows)
show_artists = []
result = Artist.query.get(artist_id)
flash(result)
for row in result:
show_artist={
'city' : {row.city},
'state' : {row.state},
"id" : {row.id},
"name" : {row.name},
"genres": {row.genres},
"address": {row.address},
"phone": {row.phone},
"website": {row.website},
"facebook_link": {row.facebook_link},
"seeking_venue": {row.seeking_venue},
"seeking_description": {row.seeking_description},
"image_link": {row.image_link},
"past_shows": [{
"artist_id": {row.show.artist_id},
"artist_name":{row.show.artist_name},
"artist_image_link":{row.show.artist_image_link},
"start_time":{row.show.start_time}
}],
"upcoming_shows":{row.upcoming_shows},
"past_shows_count":{row.past_shows_count},
"upcoming_shows_count":{row.upcoming_shows_count}
}
show_artists.append(show_artist)
return render_template('pages/show_artist.html', show_artist=show_artist, upcoming_shows=upcoming_shows,
upcoming_shows_count=upcoming_shows_count, past_shows=past_shows, past_shows_count=past_shows_count)
# Update
# ----------------------------------------------------------------
@app.route('/artists/<int:artist_id>/edit', methods=['GET'])
def edit_artist(artist_id = None):
form = ArtistForm()
form_action = url_for('artist.edit_artist')
artist= Artist.query.get(artist_id)
artist={
"id": 4,
"name": "Guns N Petals",
"genres": ["Rock n Roll"],
"city": "San Francisco",
"state": "CA",
"phone": "326-123-5000",
"website": "https://www.gunsnpetalsband.com",
"facebook_link": "https://www.facebook.com/GunsNPetals",
"seeking_venue": True,
"seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
"image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80"
}
db.session.add(artist)
db.session.commit()
# TODO: populate form with fields from artist with ID <artist_id>
return render_template('forms/edit_artist.html', form=form, artist=artist, form_action=form_action, title="Artist Update")
@app.route('/artists/<int:artist_id>/edit', methods=['POST'])
def edit_artist_submission(artist_id):
# TODO: take values from the form submitted, and update existing
# artist record with ID <artist_id> using the new attributes
form = ArtistForm()
form_action = url_for('artist.edit_artist')
artist= Artist.get(artist_id)
if request.method == 'GET':
form.name = artist.name
form.city = artist.city
form.facebook_link = artist.facebook_link
form.phone = artist.phone
form.seeking_description = artist.seeking_description
form.state = artist.state
form.website_link = artist.website_link
form.genres = artist.genres
form.image_link = artist.image_link
if form.validate_on_submit():
form.name = artist.name
form.city = artist.city
form.facebook_link = artist.facebook_link
form.phone = artist.phone
form.seeking_description = artist.seeking_description
form.state = artist.state
form.website_link = artist.website_link
form.genres = artist.genres
form.image_link = artist.image_link
if form.id == artist_id:
query = ArtistForm(
form.name,
form.city,
form.facebook_link,
form.phone,
form.seeking_description,
form.state,
form.website_link,
form.genres,
form.image_link,
)
print (query)
db.session.add(query)
db.session.commit()
flash ('Arist Updated')
print ("added")
return (url_for('artist.edit_artist'))
return redirect(url_for('show_artist', artist= artist, artist_id=artist_id, form_action=form_action, title="Artist Update"))
@app.route('/venues/<int:venue_id>/edit', methods=['GET'])
def edit_venue(venue_id):
form = VenueForm()
form_action = url_for('venue.edit_venue')
venue = Venue.query.get(venue_id)
venue={
"id": 1,
"name": "The Musical Hop",
"genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
"address": "1015 Folsom Street",
"city": "San Francisco",
"state": "CA",
"phone": "123-123-1234",
"website": "https://www.themusicalhop.com",
"facebook_link": "https://www.facebook.com/TheMusicalHop",
"seeking_talent": True,
"seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
"image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
}
db.session.add(venue)
db.session.commit()
# TODO: populate form with values from venue with ID <venue_id>
return render_template('forms/edit_venue.html', form=form, form_action=form_action, venue=venue)
@app.route('/venues/<int:venue_id>/edit', methods=['POST'])
def edit_venue_submission(venue_id):
# TODO: take values from the form submitted, and update existing
# venue record with ID <venue_id> using the new attributes
form = VenueForm()
form_action = url_for('venue.edit_venue')
venue = Venue.query.get(venue_id)
if request.method == 'GET':
form.name = venue.name
form.address = venue.address
form.city = venue.city
form.facebook_link = venue.facebook_link
form.phone = venue.phone
form.seeking_description = venue.seeking_description
form.seeking_talent = venue.seeking_talent
form.state = venue.state
form.website_link = venue.website_link
form.genres = venue.genres
form.image_link = venue.image_link
if form.validate_on_submit():
form.name = venue.name
form.address = venue.address
form.city = venue.city
form.facebook_link = venue.facebook_link
form.phone = venue.phone
form.seeking_description = venue.seeking_description
form.seeking_talent = venue.seeking_talent
form.state = venue.state
form.website_link = venue.website_link
form.genres = venue.genres
form.image_link = venue.image_link
if form.id == venue_id:
query = VenueForm(
form.name,
form.address,
form.city,
form.facebook_link,
form.phone,
form.seeking_description,
form.seeking_talent,
form.state,
form.website_link,
form.genres,
form.image_link,
)
print (query)
db.session.add(query)
db.session.commit()
flash ('Venue Updated')
print ("added")
return (url_for('venue.edit_venue'))
return redirect(url_for('show_venue', venue=venue, venue_id=venue_id, form_action=form_action, title="Venue Update"))
# Create Artist
# ----------------------------------------------------------------
@app.route('/artists/create', methods=['GET'])
def create_artist_form():
form = ArtistForm()
return render_template('forms/new_artist.html', form=form)
@app.route('/artists/create', methods=['POST'])
def create_artist_submission():
# called upon submitting the new artist listing form
error = False
try:
form = ArtistForm(request.form)
artist = Artist(
name = form.name.data,
city = form.city.data,
state = form.state.data,
address = form.address.data,
phone = form.phone.data,
genres = form.genres.data,
facebook_link = form.facebook_link.data,
image_link = form.image_link.data,
website_link = form.website_link.data,
seeking_venue = form.seeking_venue.data,
seeking_description = form.seeking_description.data,
)
db.session.add(artist)
db.session.commit()
flash('Artist: {0} created successfully'.format(artist.name))
except Exception as err:
flash('An error occurred creating the Artist: {0}. Error: {1}'.format(artist.name, err))
db.session.rollback()
finally:
db.session.close()
if not error:
return render_template('pages/home.html', artist=Artist.query.all())
# Shows
# ----------------------------------------------------------------
@app.route('/shows')
def shows():
# displays list of shows at /shows
shows = []
result = Shows.query.all()
flash(result)
for row in result:
show={
'venue_id' : {row.id},
'venue_name': {row.venue.name},
'artist_id' : {row.artist_id},
'artist_name' : {row.artist.name},
"artist_image_link" : {row.artist.image_link},
"start_time" : {row.start_time}
}
shows.append(show)
return render_template('pages/shows.html', shows=shows)
# CREATE SHOW
@app.route('/shows/create')
def create_shows():
# renders form. do not touch.
form = ShowForm()
return render_template('forms/new_show.html', form=form)
@app.route('/shows/create', methods=['POST'])
def create_show_submission():
error = False
try:
form = ShowForm(request.form)
show = Shows(
show_artist = form.artist_name.data,
show_venue = form.venue_name.data,
artist_image_link = form.artist_image_link.data,
start_time = form.start_time.data
)
db.session.add(show)
db.session.commit()
flash('Show: {0} was successfully listed'.format(show.name))
except Exception as err:
flash('An error occurred creating the Show: {0}. Error: {1}'.format(show.name, err))
db.session.rollback()
finally:
db.session.close()
if not error:
return render_template('pages/home.html', show=Shows.query.all())
@app.errorhandler(404)
def not_found_error(error):
return render_template('errors/404.html'), 404
@app.errorhandler(500)
def server_error(error):
return render_template('errors/500.html'), 500
if not app.debug:
file_handler = FileHandler('error.log')
file_handler.setFormatter(
Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
)
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('errors')
#----------------------------------------------------------------------------#
# Launch.
#----------------------------------------------------------------------------#
# Default port:
if __name__ == '__main__':
app.run(host="0.0.0.0")
# Or specify port manually:
'''
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
'''