-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
596 lines (524 loc) · 20.7 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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
import json
import dateutil.parser
import babel
from babel.dates import format_date, format_datetime, format_time
from flask import Flask, render_template, request, Response, flash, redirect, url_for
from flask_migrate import Migrate
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
import sys
from forms import *
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
moment = Moment(app)
app.config.from_object('config')
db = SQLAlchemy(app)
migrate= Migrate(app, db)
# TODO: connect to a local postgresql database
#----------------------------------------------------------------------------#
# Models.
#----------------------------------------------------------------------------#
class Artist(db.Model):
__tablename__ = 'artist'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
city = db.Column(db.String(120), nullable=False)
state = db.Column(db.String(120), nullable=False)
phone = db.Column(db.String(120))
image_link = db.Column(db.String(500))
genres = db.Column(db.String(120), nullable=False)
facebook_link = db.Column(db.String(300))
website_link =db.Column(db.String(300))
seeking_venue = db.Column(db.Boolean, nullable=False, default=False)
seeking_description = db.Column(db.String())
show= db.relationship('Show', backref='artist', lazy=True, cascade= 'all, save-update, merge,delete, delete-orphan')
def __repr__(self):
return f"<Artist {self.id} {self.name} {self.city} {self.state}>"
class Venue(db.Model):
__tablename__ = 'venue'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
city = db.Column(db.String(120), nullable=False)
state = db.Column(db.String(120), nullable=False)
address = db.Column(db.String(120), nullable=False)
phone = db.Column(db.String(120))
image_link = db.Column(db.String(300))
genres = db.Column(db.String(), nullable=False)
facebook_link = db.Column(db.String(120))
website_link = db.Column(db.String(200))
seeking_talent=db.Column(db.Boolean,nullable=False, default=False)
seeking_description=db.Column(db.String(120))
show=db.relationship('Show', backref='venue', lazy=True, cascade= 'all, save-update, merge,delete, delete-orphan')
def __repr__(self):
return f"<Venue {self.id} {self.name} {self.city} {self.state}>"
class Show(db.Model):
__tablename__ = 'show'
id = db.Column(db.Integer, primary_key=True)
artist_id = db.Column(db.Integer, db.ForeignKey('artist.id'), nullable=False)
venue_id = db.Column(db.Integer, db.ForeignKey('venue.id'), nullable=False)
start_time = db.Column(db.DateTime, nullable=False)
def __repr__(self):
return f"<Show {self.id} {self.artist_id} {self.venue_id} {self.start_time}"
#----------------------------------------------------------------------------#
# Filters.
#----------------------------------------------------------------------------#
def format_datetime(value, format='medium'):
date = dateutil.parser.parse(value)
if format == 'full':
format="EEEE MMMM, d, y 'at' h:mma"
elif format == 'medium':
format="EE MM, dd, y h:mma"
return babel.dates.format_datetime(date, format, locale='en')
app.jinja_env.filters['datetime'] = format_datetime
#----------------------------------------------------------------------------#
# Controllers.
#----------------------------------------------------------------------------#
# Venues
# ----------------------------------------------------------------
# TODO: replace with real venues data.
# num_upcoming_shows should be aggregated based on number of upcoming shows per venue.
@app.route('/venues')
def venues():
venue_page = Venue.query.distinct(Venue.city, Venue.state).all()
data = []
for page in venue_page:
new = {
"city": page.city,
"state": page.state
}
venues = Venue.query.filter_by(city=page.city, state=page.state).all()
view_venues = []
for venue in venues:
view_venues.append({
"id": venue.id,
"name": venue.name,
"num_upcoming_shows": len(list(filter(lambda x: x.start_time > datetime.now(), venue.show)))
})
new["venues"] = view_venues
data.append(new)
return render_template('pages/venues.html', areas=data)
@app.route('/')
def index():
return render_template('pages/home.html')
@app.route('/venues/<int:venue_id>')
def show_venue(venue_id):
# shows the venue page with the given venue_id
# TODO: replace with real venue data from the venues table, using venue_id
# past_shows_query = list(filter(lambda show: show.start_time < current_time, venue.shows))
past_shows_query = db.session.query(Show).join(Venue).filter(Show.venue_id==venue_id).filter(Show.start_time<datetime.now()).all()
past_shows = []
for past_show in past_shows_query:
show ={}
show ['artist_id']= past_show.artist.id
show ['artist_name'] =past_show.artist.name
show ['artist_image_link']= past_show.artist.image_link,
show ['start_time']= past_show.start_time.strftime('%m/%d/%y, %H:%M:%S')
past_shows.append(show)
upcoming_shows_query = db.session.query(Show).join(Venue).filter(Show.venue_id==venue_id).filter(Show.start_time>datetime.now()).all()
venue = Venue.query.get(venue_id)
upcoming_shows = []
for upcoming_show in upcoming_shows_query:
show ={}
show ['artist_id']= upcoming_show.artist.id
show ['artist_name'] = upcoming_show.artist.name
show ['artist_image_link']= upcoming_show.artist.image_link
show ['start_time']= upcoming_show.start_time.strftime('%m/%d/%y, %H:%M:%S')
upcoming_shows.append(show)
venue.upcoming_shows= upcoming_shows
venue.upcoming_shows_count=len(upcoming_shows)
venue.past_shows= past_shows
venue.past_shows_count= len(past_shows)
return render_template('pages/show_venue.html', venue=venue)
@app.route('/venues/search', methods=['POST'])
def search_venues():
# TODO: implement search on venues with partial string search. Ensure it is case-insensitive.
# seach for Hop should return "The Musical Hop".
# search for "Music" should return "The Musical Hop" and "Park Square Live Music & Coffee"
search = request.form.get("search_term", "")
data = {}
data["count"] = len(venues)
data["data"] = []
retrieve = {}
venues = list(Venue.query.filter( Venue.name.ilike(f"%{search_term}%") | Venue.state.ilike(f"%{search_term}%") | Venue.city.ilike(f"%{search_term}%") ).all())
for venue in venues:
retrieve = {
"id": venue.id,
"name": venue.name,
"num_upcoming_shows": len(list(filter(lambda x: x.start_time > datetime.now(), venue.show)))
}
data["data"].append(retrieve)
return render_template('pages/search_venues.html', results=data, search=request.form.get('search_term'))
# 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():
# TODO: insert form data as a new Venue record in the db, instead
# TODO: modify data to be the data object returned from db insertion
form = VenueForm(request.form)
name = None
city= None
state = None
address = None
phone = None
image_link = None
website_link = None
genres = None
facebook_link = None
seeking_talent = None
seeking_description = None
#Validate form
if form.validate():
try:
new_venue= Venue(
name = form.name.data,
city= form.city.data,
state = form.state.data,
address = form.address.data,
phone = form.phone.data,
image_link = form. image_link.data,
website_link = form. website_link.data,
genres = ',' .join(form.genres.data),
facebook_link = form.facebook_link.data,
seeking_talent = form.seeking_talent.data,
seeking_description = form.seeking_description.data)
db.session.add(new_venue)
db.session.commit()
flash('Venue ' + request.form['name'] + ' was successfully listed!')
except:
db.session.rollback()
flash('An error occurred. Venue' + ' could not be listed.')
finally:
db.session.close()
return render_template('pages/home.html',
name = name,
city= city,
state = state,
address = address,
phone = phone,
image_link = image_link,
website_link = website_link,
genres = genres,
facebook_link = facebook_link,
seeking_talent = seeking_talent,
seeking_description = seeking_description)
# Delete Venue
# ----------------------------------------------------------------
@app.route('/venues/<venue_id>/delete', methods= [ 'GET'])
# TODO: Complete this endpoint for taking a venue_id, and using
# SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
# BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
# clicking that button delete it from the db then redirect the user to the homepage
def delete_venue(venue_id):
form = VenueForm()
venue = Venue.query.get(venue_id)
try:
Venue.query.filter_by(id = venue_id)
db.session.delete(venue)
db.session.commit()
flash ('Venue with id ' +venue_id+ ' was deleted successfully!' )
except:
db.session.rollback()
flash('Error, venue could not be deleted')
finally:
db.session.close()
return redirect(url_for('index'))
# Artist
# ----------------------------------------------------------------
# TODO: replace with real data returned from querying the database
@app.route('/artists')
def artists():
data = db.session.query(Artist.id, Artist.name). all()
return render_template('pages/artists.html', artists=data)
@app.route('/artists/search', methods=['POST'])
def search_artists():
# TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
# seach for "A" should return "Guns N Petals", "Matt Quevado", and "The Wild Sax Band".
# search for "band" should return "The Wild Sax Band"
search = request.form.get("search_term", "")
data = {}
data["count"] = len(venues)
data["data"] = []
retrieve = {}
artists = list(Artist.query.filter( Artist.name.ilike(f"%{search_term}%") | Artist.state.ilike(f"%{search_term}%") | Artist.city.ilike(f"%{search_term}%") ).all())
for artist in artists:
retrieve = {
"id": artist.id,
"name": artist.name,
"num_upcoming_shows": len(list(filter(lambda x: x.start_time > datetime.now(), venue.show)))
}
data["data"].append(retrieve)
return render_template('pages/search_artist.html', results=data, search=request.form.get('search_term'))
@app.route('/artists/<int:artist_id>')
def show_artist(artist_id):
# shows the artist page with the given artist_id
# TODO: replace with real artist data from the artist table, using artist_id
artist = Artist.query.get(artist_id)
past_shows = db.session.query(Show).join(Artist).filter(Show.start_time<datetime.now()).filter(Show.artist_id==artist_id).all()
portray_shows = []
for portray in past_shows:
show ={}
show ['artist_id']= portray.artist.id
show ['artist_name'] =portray.artist.name
show ['artist_image_link']= portray.artist.image_link,
show ['start_time']= portray.start_time.strftime('%m/%d/%y, %H:%M:%S')
portray_shows.append(show)
upcoming_shows_query = db.session.query(Show).join(Artist).filter(Show.artist_id==artist_id).filter(Show.start_time>datetime.now()).all()
upcoming_shows = []
for upcoming_show in upcoming_shows_query:
show ={}
show ['artist_id']= upcoming_show.artist.id
show ['artist_name'] = upcoming_show.artist.name
show ['artist_image_link']= upcoming_show.artist.image_link
show ['start_time']= upcoming_show.start_time.strftime('%m/%d/%y, %H:%M:%S')
upcoming_shows.append(show)
artist.upcoming_shows= upcoming_shows
artist.upcoming_shows_count=len(upcoming_shows)
artist.portray_shows= portray_shows
artist.portray_shows_count= len(portray_shows)
return render_template('pages/show_artist.html', artist=artist)
@app.route('/artists/<int:artist_id>/edit', methods=['GET'])
def edit_artist(artist_id):
form = ArtistForm()
artist = Artist.query.get(artist_id)
return render_template('forms/edit_artist.html', form=form, artist=artist)
@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(request.form)
name = None
city= None
state = None
address = None
phone = None
image_link = None
website_link = None
genres = None
facebook_link = None
seeking_talent = None
seeking_description = None
if form.validate():
try:
artist = Artist.query.get(artist_id)
artist.name = form.name.data
artist.city= form.city.data
artist.state= form.state.data
artist.phone= form.phone.data
artist.image_link= form.image_link.data
artist.genres= ",".join(form.genres.data)
artist.facebook_link= form.facebook_link.data
artist.website_link = form.website_link.data
artist.seeking_venue= form.seeking_venue.data
artist.seeking_description= form.seeking_description.data
db.session.add(artist)
db.session.commit()
flash("Artist " + artist.name + " was successfully edited!")
except:
db.session.rollback()
flash("Artist was not edited successfully.")
finally:
db.session.close()
return redirect(url_for('show_artist', artist_id=artist_id))
@app.route('/venues/<int:venue_id>/edit', methods=['GET'])
def edit_venue(venue_id):
form = VenueForm()
venue = Venue.query.get(venue_id)
return render_template('forms/edit_venue.html', form=form, 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(request.form)
name = None
city= None
state = None
address = None
phone = None
image_link = None
website_link = None
genres = None
facebook_link = None
seeking_talent = None
seeking_description = None
if form.validate():
try:
venue = Venue.query.get(venue_id)
venue.name=form.name.data
venue.city=form.city.data
venue.state=form.state.data
venue.address=form.address.data
venue.phone=form.phone.data
venue.image_link=form.image_link.data
venue.genres= ','.join(form.genres.data)
venue.facebook_link=form.facebook_link.data
venue.website_link=form.website_link.data
venue.seeking_talent=form.seeking_talent.data
venue.seeking_description=form.seeking_description.data
db.session.add(venue)
db.session.commit()
flash("Venue " + venue.name + " was successfully edited!")
except:
db.session.rollback()
flash('Venue was not edited successfully!')
finally:
db.session.close()
return redirect(url_for('show_venue', venue_id=venue_id))
# ----------------------------------------------------------------
# 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
# TODO: insert form data as a new Venue record in the db, instead
# TODO: modify data to be the data object returned from db insertion
form = ArtistForm(request.form)
name = None
city= None
state = None
address = None
phone = None
image_link = None
website_link = None
genres = None
facebook_link = None
seeking_talent = None
seeking_description = None
if form.validate():
try:
new_artist = Artist(
name = form.name.data,
city = form.city.data,
state = form.state.data,
phone = form.phone.data,
image_link = form.image_link.data,
genres = ','.join(form.genres.data),
facebook_link = form.facebook_link.data,
website_link = form.website_link.data,
seeking_venue = form.seeking_venue.data,
seeking_description = form.seeking_description.data
)
db.session.add(new_artist)
db.session.commit()
# on successful db insert, flash success
flash('Artist ' + request.form['name'] + ' was successfully listed!')
except:
db.session.rollback()
# TODO: on unsuccessful db insert, flash an error instead.
flash('An error occurred. Artist ' + new_artist.name + ' could not be listed.')
finally:
db.session.close()
return render_template('pages/home.html', name = name,
city= city,
state = state,
address = address,
phone = phone,
image_link = image_link,
website_link = website_link,
genres = genres,
facebook_link = facebook_link,
seeking_talent = seeking_talent,
seeking_description = seeking_description)
# Delete Artist
# ----------------------------------------------------------------
@app.route('/artists/<artist_id>/delete', methods=['GET'])
def delete_artist(artist_id):
form = ArtistForm()
artist = Artist.query.get(artist_id)
try:
Artist.query.filter_by(id = artist_id)
db.session.delete(artist)
db.session.commit()
flash("Artist ,with id " +artist_id+ " was deleted successfully!")
except:
db.session.rollback()
flash('Error, Artist has not been deleted')
finally:
db.session.close()
return redirect(url_for('index'))
# Shows
# ----------------------------------------------------------------
@app.route('/shows')
def shows():
data = []
shows = Show.query.all()
for show in shows:
portray = {}
portray["venue_id"] = show.venue.id
portray["venue_name"] = show.venue.name
portray["artist_id"] = show.artist.id
portray["artist_name"] = show.artist.name
portray["artist_image_link"] = show.artist.image_link
portray["start_time"] = show.start_time.strftime("%m/%d/%Y, %H:%M:%S")
data.append(portray)
return render_template('pages/shows.html', shows=data)
# Create Shows
# ----------------------------------------------------------------
@app.route('/shows/create')
def create_shows():
form = ShowForm()
return render_template('forms/new_show.html', form=form)
@app.route('/shows/create', methods=['POST'])
def create_show_submission():
form = ShowForm(request.form)
artist_id = None
venue_id = None
start_time = None
if form.validate():
try:
new_show = Show(
artist_id = form.artist_id.data,
venue_id = form.venue_id.data,
start_time = form.start_time.data
)
db.session.add(new_show)
db.session.commit()
flash('Show was successfully listed!')
except:
db.session.rollback()
flash('An error occurred. Show could not be listed.')
finally:
db.session.close()
return redirect (url_for('index'))
@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()
# Or specify port manually:
'''
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
'''