-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
813 lines (710 loc) · 28.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# ----------------------------------------------------------------------------#
# Imports
# ----------------------------------------------------------------------------#
import json
import dateutil.parser
import babel
from flask import Flask, render_template, request, Response, flash, redirect, url_for
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
from sqlalchemy import func
from forms import *
from flask_migrate import Migrate
from datetime import datetime
# ----------------------------------------------------------------------------#
# App Config.
# ----------------------------------------------------------------------------#
app = Flask(__name__)
moment = Moment(app)
app.config.from_object("config")
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# DONE TODO: connect to a local postgresql database
from models import *
# DONE TODO Implement Show and Artist models, and complete all model relationships and properties, as a database migration.
# ----------------------------------------------------------------------------#
# 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.
# ----------------------------------------------------------------------------#
@app.route("/")
def index():
return render_template("pages/home.html")
# Venues
# ----------------------------------------------------------------
@app.route("/venues")
def venues():
# DONE TODO: replace with real venues data.
# num_upcoming_shows should be aggregated based on number of upcoming shows per venue.
query = Show.query.with_entities(Venue.city, Venue.state).group_by(Venue.city, Venue.state)
results = []
for city, state in query:
data = {}
venue_loc = []
data['city'] = city
data['state'] = state
data['venues'] = venue_loc
venues = Venue.query.filter_by(state=state).filter_by(city=city).all()
for venue in venues:
venue_shows = Show.query.filter_by(venue_id=venue.id).all()
show_count = 0
for show in venue_shows:
if datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') > datetime.now():
show_count += 1
venue_data = {
'id': venue.id,
'name': venue.name,
"num_upcoming_shows": show_count
}
venue_loc.append(venue_data)
results.append(data)
# data=[{
# "city": "San Francisco",
# "state": "CA",
# "venues": [{
# "id": 1,
# "name": "The Musical Hop",
# "num_upcoming_shows": 0,
# }, {
# "id": 3,
# "name": "Park Square Live Music & Coffee",
# "num_upcoming_shows": 1,
# }]
# }, {
# "city": "New York",
# "state": "NY",
# "venues": [{
# "id": 2,
# "name": "The Dueling Pianos Bar",
# "num_upcoming_shows": 0,
# }]
# }]
return render_template("pages/venues.html", areas=results)
@app.route("/venues/search", methods=["POST"])
def search_venues():
# DONE 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"
# response = {
# "count": 1,
# "data": [
# {
# "id": 2,
# "name": "The Dueling Pianos Bar",
# "num_upcoming_shows": 0,
# }
# ],
# }
search_term=request.form.get("search_term", "")
venues = []
data = Venue.query.filter(Venue.name.ilike(f"%{search_term}%")).all()
count = len(data)
for item in data:
venue_shows = Show.query.filter_by(venue_id=item.id).all()
show_count = 0
for show in venue_shows:
if datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') > datetime.now():
show_count += 1
venue = {
"id": item.id,
"name": item.name,
"num_upcoming_shows": show_count,
}
venues.append(venue)
response = {
"count": count,
"data": venues
}
return render_template(
"pages/search_venues.html",
results=response,
search_term=request.form.get("search_term", ""),
)
@app.route("/venues/<int:venue_id>")
def show_venue(venue_id):
# shows the venue page with the given venue_id
# DONE TODO: replace with real venue data from the venues table, using venue_id
# data1 = {
# "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",
# "past_shows": [
# {
# "artist_id": 4,
# "artist_name": "Guns N Petals",
# "artist_image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
# "start_time": "2019-05-21T21:30:00.000Z",
# }
# ],
# "upcoming_shows": [],
# "past_shows_count": 1,
# "upcoming_shows_count": 0,
# }
# data2 = {
# "id": 2,
# "name": "The Dueling Pianos Bar",
# "genres": ["Classical", "R&B", "Hip-Hop"],
# "address": "335 Delancey Street",
# "city": "New York",
# "state": "NY",
# "phone": "914-003-1132",
# "website": "https://www.theduelingpianos.com",
# "facebook_link": "https://www.facebook.com/theduelingpianos",
# "seeking_talent": False,
# "image_link": "https://images.unsplash.com/photo-1497032205916-ac775f0649ae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80",
# "past_shows": [],
# "upcoming_shows": [],
# "past_shows_count": 0,
# "upcoming_shows_count": 0,
# }
venue = Venue.query.get_or_404(venue_id)
shows = Show.query.filter_by(venue_id=venue_id).all()
past_shows = []
upcoming_shows = []
for show in shows:
if datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') < datetime.now():
item = {
"artist_id": show.artist.id,
"artist_name": show.artist.name,
"artist_image_link": show.artist.image_link,
"start_time": show.start_time,
}
past_shows.append(item)
elif datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') > datetime.now():
item = {
"artist_id": show.artist.id,
"artist_name": show.artist.name,
"artist_image_link": show.artist.image_link,
"start_time": show.start_time,
}
upcoming_shows.append(item)
genres = venue.genres[1:-2].split(',')
data = {
"id": venue.id,
"name": venue.name,
"genres": genres,
"address": venue.address,
"city": venue.city,
"state": venue.state,
"phone": venue.phone,
"website": venue.website,
"facebook_link": venue.facebook_link,
"seeking_talent": venue.seeking_talent,
"image_link": venue.image_link,
"past_shows": past_shows,
"upcoming_shows": upcoming_shows,
"past_shows_count": len(past_shows),
"upcoming_shows_count": len(upcoming_shows),
}
# data = list(filter(lambda d: d["id"] == venue_id, [data1, data2, data3]))[0]
return render_template("pages/show_venue.html", venue=data)
# 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():
# DONE TODO: insert form data as a new Venue record in the db, instead
# DONE TODO: modify data to be the data object returned from db insertion
form = VenueForm()
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,
website = form.website_link.data,
image_link = form.image_link.data,
seeking_talent = form.seeking_talent.data,
seeking_description = form.seeking_description.data
)
try:
db.session.add(venue)
db.session.commit()
flash("Venue " + form.name.data + " was successfully listed!")
except:
db.session.rollback()
flash('An error occurred. Venue ' + form.name.data + ' could not be listed.')
finally:
db.session.close()
# on successful db insert, flash success
# flash("Venue " + request.form["name"] + " was successfully listed!")
# DONE TODO: on unsuccessful db insert, flash an error instead.
# e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
# see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
return render_template("pages/home.html")
@app.route("/venues/<venue_id>/delete", methods=["DELETE","POST"])
def delete_venue(venue_id):
# DONE 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.
venue = Venue.query.get_or_404(venue_id)
try:
db.session.delete(venue)
db.session.commit()
except:
db.session.rollback()
finally:
db.session.close()
# 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
return redirect(url_for('index'))
# Artists
# ----------------------------------------------------------------
@app.route("/artists")
def artists():
# DONE TODO: replace with real data returned from querying the database
# data = [
# {
# "id": 4,
# "name": "Guns N Petals",
# },
# {
# "id": 5,
# "name": "Matt Quevedo",
# },
# {
# "id": 6,
# "name": "The Wild Sax Band",
# },
# ]
artists = Artist.query.all()
data = []
for artist in artists:
details = {
"id": artist.id,
"name": artist.name
}
data.append(details)
return render_template("pages/artists.html", artists=data)
@app.route("/artists/search", methods=["POST"])
def search_artists():
# DONE 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".
# response = {
# "count": 1,
# "data": [
# {
# "id": 4,
# "name": "Guns N Petals",
# "num_upcoming_shows": 0,
# }
# ],
# }
search_term=request.form.get("search_term", "")
# count = Artist.query.filter(Artist.name.ilike(f"%{search_term}%")).count(),
data = Artist.query.filter(Artist.name.ilike(f"%{search_term}%")).all()
count = len(data)
artist_list = []
for artist in data:
artist_shows = Show.query.filter(Show.artist_id==artist.id).all()
show_count = 0
for show in artist_shows:
if datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') > datetime.now():
show_count += 1
detail = {
"id": artist.id,
"name": artist.name,
"num_upcoming_shows": show_count
}
artist_list.append(detail)
response = {
"count": count,
"data": data
}
return render_template(
"pages/search_artists.html",
results=response,
search_term=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
# DONE TODO: replace with real artist data from the artist table, using artist_id
# data1 = {
# "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",
# "past_shows": [
# {
# "venue_id": 1,
# "venue_name": "The Musical Hop",
# "venue_image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60",
# "start_time": "2019-05-21T21:30:00.000Z",
# }
# ],
# "upcoming_shows": [],
# "past_shows_count": 1,
# "upcoming_shows_count": 0,
# }
# data2 = {
# "id": 5,
# "name": "Matt Quevedo",
# "genres": ["Jazz"],
# "city": "New York",
# "state": "NY",
# "phone": "300-400-5000",
# "facebook_link": "https://www.facebook.com/mattquevedo923251523",
# "seeking_venue": False,
# "image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
# "past_shows": [
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
# "start_time": "2019-06-15T23:00:00.000Z",
# }
# ],
# "upcoming_shows": [],
# "past_shows_count": 1,
# "upcoming_shows_count": 0,
# }
# data3 = {
# "id": 6,
# "name": "The Wild Sax Band",
# "genres": ["Jazz", "Classical"],
# "city": "San Francisco",
# "state": "CA",
# "phone": "432-325-5432",
# "seeking_venue": False,
# "image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
# "past_shows": [],
# "upcoming_shows": [
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
# "start_time": "2035-04-01T20:00:00.000Z",
# },
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
# "start_time": "2035-04-08T20:00:00.000Z",
# },
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
# "start_time": "2035-04-15T20:00:00.000Z",
# },
# ],
# "past_shows_count": 0,
# "upcoming_shows_count": 3,
# }
# data = list(filter(lambda d: d["id"] == artist_id, [data1, data2, data3]))[0]
artist = Artist.query.get_or_404(artist_id)
past_shows = []
upcoming_shows = []
artist_shows = Show.query.filter_by(artist_id=artist_id).all()
for show in artist_shows:
if datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') < datetime.now():
item = {
"venue_id": show.venue.id,
"venue_name": show.venue.name,
"venue_image_link": show.venue.image_link,
"start_time": show.start_time,
}
past_shows.append(item)
elif datetime.strptime(show.start_time, '%Y-%m-%d %H:%M:%S') > datetime.now():
item = {
"venue_id": show.venue.id,
"venue_name": show.venue.name,
"venue_image_link": show.venue.image_link,
"start_time": show.start_time,
}
upcoming_shows.append(item)
genres = artist.genres[1:-2].split(',')
data = {
"id": artist.id,
"name": artist.name,
"genres": genres,
"city": artist.city,
"state": artist.state,
"phone": artist.phone,
"seeking_venue": artist.seeking_venue,
"image_link": artist.image_link,
"past_shows": past_shows,
"upcoming_shows": upcoming_shows,
"past_shows_count": len(past_shows),
"upcoming_shows_count": len(upcoming_shows),
}
return render_template("pages/show_artist.html", artist=data)
# Update
# ----------------------------------------------------------------
@app.route("/artists/<int:artist_id>/edit", methods=["GET"])
def edit_artist(artist_id):
artist = Artist.query.get_or_404(artist_id)
form = ArtistForm(obj=artist)
# 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",
# }
# DONE TODO: populate form with fields from artist with ID <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):
# DONE TODO: take values from the form submitted, and update existing
# artist record with ID <artist_id> using the new attributes
form = ArtistForm()
artist = Artist.query.get_or_404(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 = form.genres.data,
artist.facebook_link = form.facebook_link.data,
artist.website = form.website_link.data,
artist.seeking_venue = form.seeking_venue.data,
artist.seeking_description = form.seeking_description.data
if form.seeking_venue.data:
artist.seeking_venue = True
else:
artist.seeking_venue = False
try:
db.session.commit()
flash("Artist " + form.name.data + " was successfully updated!")
except:
db.session.rollback()
flash('An error occurred. Artist ' + form.name.data + ' could not be updated.')
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):
# 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",
# }
# DONE TODO: populate form with values from venue with ID <venue_id>
venue = Venue.query.get_or_404(venue_id)
form = VenueForm(obj=venue)
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):
form = VenueForm()
venue = Venue.query.get_or_404(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.genres = form.genres.data,
venue.facebook_link = form.facebook_link.data,
venue.website = form.website_link.data,
venue.image_link = form.image_link.data,
venue.seeking_talent = form.seeking_talent.data,
venue.seeking_description = form.seeking_description.data
if form.seeking_talent.data:
venue.seeking_talent = True
else:
venue.seeking_talent = False
try:
db.session.commit()
flash("Venue " + form.name.data + " was successfully updated!")
except:
db.session.rollback()
flash('An error occurred. Artist ' + form.name.data + ' could not be updated.')
finally:
db.session.close()
# DONE TODO: take values from the form submitted, and update existing
# venue record with ID <venue_id> using the new attributes
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
# DONE TODO: insert form data as a new Venue record in the db, instead
# DONE TODO: modify data to be the data object returned from db insertion
form = ArtistForm()
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 = form.genres.data,
facebook_link = form.facebook_link.data,
website = form.website_link.data,
seeking_venue = form.seeking_venue.data,
seeking_description = form.seeking_description.data
)
try:
db.session.add(artist)
db.session.commit()
flash("Artist " + form.name.data + " was successfully listed!")
except:
db.session.rollback()
flash('An error occurred. Artist ' + form.name.data + ' could not be listed.')
finally:
db.session.close()
# on successful db insert, flash success
# flash("Artist " + request.form["name"] + " was successfully listed!")
# DONE TODO: on unsuccessful db insert, flash an error instead.
# e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.')
return render_template("pages/home.html")
# Shows
# ----------------------------------------------------------------
@app.route("/shows")
def shows():
# displays list of shows at /shows
# DONE TODO: replace with real venues data.
# data = [
# {
# "venue_id": 1,
# "venue_name": "The Musical Hop",
# "artist_id": 4,
# "artist_name": "Guns N Petals",
# "artist_image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
# "start_time": "2019-05-21T21:30:00.000Z",
# },
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "artist_id": 5,
# "artist_name": "Matt Quevedo",
# "artist_image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
# "start_time": "2019-06-15T23:00:00.000Z",
# },
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "artist_id": 6,
# "artist_name": "The Wild Sax Band",
# "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
# "start_time": "2035-04-01T20:00:00.000Z",
# },
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "artist_id": 6,
# "artist_name": "The Wild Sax Band",
# "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
# "start_time": "2035-04-08T20:00:00.000Z",
# },
# {
# "venue_id": 3,
# "venue_name": "Park Square Live Music & Coffee",
# "artist_id": 6,
# "artist_name": "The Wild Sax Band",
# "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
# "start_time": "2035-04-15T20:00:00.000Z",
# },
# ]
shows = Show.query.all()
return render_template("pages/shows.html", shows=shows)
@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():
# called to create new shows in the db, upon submitting new show listing form
# DONE TODO: insert form data as a new Show record in the db, instead
form = ShowForm()
show = Show(
artist_id = form.artist_id.data,
venue_id = form.venue_id.data,
start_time = form.start_time.data
)
try:
db.session.add(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()
# on successful db insert, flash success
# flash("Show was successfully listed!")
# DONE TODO: on unsuccessful db insert, flash an error instead.
# e.g., flash('An error occurred. Show could not be listed.')
# see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
return render_template("pages/home.html")
@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(debug=True)
# Or specify port manually:
"""
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
"""