-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
985 lines (757 loc) · 27.2 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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
"""A musical venue and artist booking site.
Fyyur is a musical venue and artist booking site that facilitates the discovery
and bookings of shows between local performing artists and venues
Usage: flask run
"""
# ----------------------------------------------------------------------------#
# Imports
# ----------------------------------------------------------------------------#
import logging
import os
import sys
from datetime import datetime
from logging import FileHandler, Formatter
import babel
import dateutil.parser
from flask import (
abort,
flash,
jsonify,
redirect,
render_template,
request,
url_for,
)
from forms import (
ArtistForm,
MusicForm,
ShowForm,
UnavailabilityForm,
VenueForm,
)
from models import (
Area,
Artist,
Genre,
Music,
Show,
Unavailability,
Venue,
app,
db,
)
# ----------------------------------------------------------------------------#
# Filters
# ----------------------------------------------------------------------------#
def format_datetime(value, datetime_format="medium"):
"""Converts a datetime str to a format that is understood by the db.
Args:
value: A str representing a datetime
datetime_format: A str representing the desired format of the returned
datetime, accepted values are ('full', 'medium')
Returns:
A date formatted according to the given format
"""
date = dateutil.parser.parse(value)
if datetime_format == "full":
datetime_format = "EEEE MMMM, d, y 'at' h:mma"
elif datetime_format == "medium":
datetime_format = "EE MM, dd, y h:mma"
return babel.dates.format_datetime(date, datetime_format)
app.jinja_env.filters["datetime"] = format_datetime
# ----------------------------------------------------------------------------#
# Helpers
# ----------------------------------------------------------------------------#
def get_genres(genre_names):
"""Gets a list of genre objects from a list of genre names.
Args:
genre_names: A list of strs representing the names of genres
Returns:
genres: A list of genre objects corresponding the the genre names
passed in
"""
genres = []
for genre_name in genre_names:
genre = Genre.query.filter_by(name=genre_name).first()
if not genre:
genre = Genre(name=genre_name)
db.session.add(genre)
genres.append(genre)
return genres
def get_area_id(city, state):
"""Gets the area id of an area from a city and state.
Args:
city: A str representing the city of an area object
state: A str representing the state of an area object
Returns:
area_id: An int representing the id of the area object cooresponding
to a city and state
"""
area = Area.query.filter(Area.city == city, Area.state == state).first()
if not area:
area = Area(city=city, state=state)
db.session.add(area)
db.session.flush()
return area.id
# ----------------------------------------------------------------------------#
# Controllers
# ----------------------------------------------------------------------------#
@app.route("/")
def index():
"""The route handler for the homepage.
Returns:
A rendered html template for the homepage
"""
recent_venues = (
Venue.query.order_by(db.desc(Venue.created_at)).limit(9).all()
)
recent_artists = (
Artist.query.order_by(db.desc(Artist.created_at)).limit(9).all()
)
recently_listed = []
while len(recently_listed) < 9 and (
len(recent_artists) > 0 or len(recent_venues) > 0
):
if (
len(recent_venues) > 0
and recent_venues[0].created_at > recent_artists[0].created_at
or len(recent_artists) == 0
):
recent_venues[0].type = "Venue"
recently_listed.append(recent_venues.pop(0))
else:
recent_artists[0].type = "Artist"
recently_listed.append(recent_artists.pop(0))
print(recently_listed)
return render_template("pages/home.html", recently_listed=recently_listed)
# Venues
# ----------------------------------------------------------------
@app.route("/venues")
def venues():
"""Route handler for looking at all venues grouped by city, state.
Returns:
A template for all venues populated with venues grouped by city, state
"""
areas = Area.query.filter(
Area.venues != None # noqa: E711, pylint: disable=singleton-comparison
).all()
return render_template("pages/venues.html", areas=areas)
@app.route("/venues/search", methods=["POST"])
def search_venues():
"""Route handler for venue search.
Returns:
A template with the search results
"""
search_term = request.form.get("search_term", "")
areas = Area.query.filter(
Area.city.ilike(f"%{search_term}%")
| Area.state.ilike(f"%{search_term}%")
).all()
area_ids = [area.id for area in areas]
data = Venue.query.filter(
Venue.name.ilike(f"%{search_term}%") | Venue.area_id.in_(area_ids)
).all()
results = {"count": len(data), "data": data}
return render_template(
"pages/search_venues.html", results=results, search_term=search_term
)
@app.route("/venues/<int:venue_id>")
def show_venue(venue_id):
"""Route handler for showing the details for a venue.
Args:
venue_id: A str representing the id of the venue to show details for
Returns:
A template with the detail view for a venue
"""
venue = Venue.query.get(venue_id)
venue.city = venue.area.city
venue.state = venue.area.state
venue.past_shows = []
venue.upcoming_shows = []
for show in venue.shows:
show.artist_name = show.artist.name
show.artist_image_link = show.artist.image_link
if show.start_time < datetime.now():
show.start_time = str(show.start_time)
venue.past_shows.append(show)
else:
show.start_time = str(show.start_time)
venue.upcoming_shows.append(show)
venue.past_shows_count = len(venue.past_shows)
venue.upcoming_shows_count = len(venue.upcoming_shows)
return render_template("pages/show_venue.html", venue=venue)
# Update Venue
# ----------------------------------------------------------------
@app.route("/venues/<int:venue_id>/edit", methods=["GET"])
def edit_venue_form(venue_id):
"""Displays the form for editing a venue.
Args:
venue_id: A str representing the id of the venue to edit
Returns:
A html template for the venue form which has been pre-populated
"""
venue = Venue.query.get(venue_id)
venue.city = venue.area.city
venue.state = venue.area.state
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):
"""Updates an existing venue in the db from a form submission.
Args:
venue_id: A str representing the id of the venue to update
Returns:
The template for the venue's detail page
"""
form = VenueForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("edit_venue_form", venue_id=venue_id))
error = False
try:
venue_name = request.form.get("name")
city = request.form.get("city")
state = request.form.get("state")
venue = Venue.query.get(venue_id)
venue.name = venue_name
venue.genres = get_genres(request.form.getlist("genres"))
venue.address = request.form.get("address")
venue.area_id = get_area_id(city, state)
venue.phone = request.form.get("phone")
venue.website = request.form.get("website")
venue.facebook_link = request.form.get("facebook_link")
venue.seeking_talent = bool(request.form.get("seeking_talent"))
venue.seeking_description = request.form.get("seeking_description")
venue.image_link = request.form.get("image_link")
db.session.commit()
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"Venue {venue_name} was unable to be updated!", "error")
abort(500)
flash(f"Venue {venue_name} was successfully updated!")
return redirect(url_for("show_venue", venue_id=venue_id))
# Create Venue
# ----------------------------------------------------------------
@app.route("/venues/create", methods=["GET"])
def create_venue_form():
"""Displays the form for creating a venue.
Returns:
A html template for the venue form
"""
form = VenueForm()
return render_template("forms/new_venue.html", form=form)
@app.route("/venues/create", methods=["POST"])
def create_venue_submission():
"""Creates a new venue in the db from a form submission.
Returns:
The template for a list of all venues
"""
form = VenueForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("create_venue_form"))
error = False
try:
venue_name = request.form.get("name")
city = request.form.get("city")
state = request.form.get("state")
venue = Venue(
name=venue_name,
genres=get_genres(request.form.getlist("genres")),
address=request.form.get("address"),
area_id=get_area_id(city, state),
phone=request.form.get("phone"),
website=request.form.get("website"),
facebook_link=request.form.get("facebook_link"),
seeking_talent=bool(request.form.get("seeking_talent")),
seeking_description=request.form.get("seeking_description"),
image_link=request.form.get("image_link"),
)
db.session.add(venue)
db.session.commit()
venue_id = venue.id
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"Venue {venue_name} was unable to be listed!", "error")
abort(500)
flash(f"Venue {venue_name} was successfully listed!")
return redirect(url_for("show_venue", venue_id=venue_id))
# Delete Venue
# ----------------------------------------------------------------
@app.route("/venues/<venue_id>", methods=["DELETE"])
def delete_venue(venue_id):
"""Route handler to delete a venue from the db.
Args:
venue_id: A str representing the id of the venue to delete
Returns:
response: A json object signalling the deletion request was successful
"""
error = False
try:
venue = Venue.query.get(venue_id)
venue_name = venue.name
db.session.delete(venue)
db.session.commit()
response = {"success": True}
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"Venue {venue_name} was unable to be deleted!", "error")
abort(500)
flash(f"Venue {venue_name} was successfully deleted!")
return jsonify(response)
# Artists
# ----------------------------------------------------------------
@app.route("/artists")
def artists():
"""Route handler for showing a list of artists.
Returns:
A template for a list of all artists
"""
artists_all = Artist.query.all()
return render_template("pages/artists.html", artists=artists_all)
@app.route("/artists/search", methods=["POST"])
def search_artists():
"""Route handler for artist search.
Returns:
A template with the search results
"""
search_term = request.form.get("search_term", "")
areas = Area.query.filter(
Area.city.ilike(f"%{search_term}%")
| Area.state.ilike(f"%{search_term}%")
).all()
area_ids = [area.id for area in areas]
data = Artist.query.filter(
Artist.name.ilike(f"%{search_term}%") | Artist.area_id.in_(area_ids)
).all()
results = {"count": len(data), "data": data}
return render_template(
"pages/search_artists.html", results=results, search_term=search_term
)
@app.route("/artists/<int:artist_id>")
def show_artist(artist_id):
"""Route handler for showing the details for an artist.
Args:
artist_id: A str representing the id of the artist to show details for
Returns:
A template with the detail view for an artist
"""
artist = Artist.query.get(artist_id)
artist.city = artist.area.city
artist.state = artist.area.state
artist.past_shows = []
artist.upcoming_shows = []
for show in artist.shows:
show.venue_name = show.venue.name
show.venue_image_link = show.venue.image_link
if show.start_time < datetime.now():
show.start_time = str(show.start_time)
artist.past_shows.append(show)
else:
show.start_time = str(show.start_time)
artist.upcoming_shows.append(show)
artist.past_shows_count = len(artist.past_shows)
artist.upcoming_shows_count = len(artist.upcoming_shows)
music_form = MusicForm()
unavailability_form = UnavailabilityForm()
return render_template(
"pages/show_artist.html",
artist=artist,
music_form=music_form,
unavailability_form=unavailability_form,
)
# Create Music
@app.route("/artists/<int:artist_id>/music/create", methods=["POST"])
def create_music(artist_id):
"""Creates a new featured song/album for the artist from form submission.
Returns:
The detail view for the artist
"""
form = MusicForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("show_artist", artist_id=artist_id))
error = False
try:
music_type = request.form.get("type_")
music_title = request.form.get("title")
music = Music(artist_id=artist_id, type_=music_type, title=music_title)
db.session.add(music)
db.session.commit()
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"{music_type} {music_title} could not be added!", "error")
abort(500)
flash(f"{music_type} {music_title} was successfully added!")
return redirect(url_for("show_artist", artist_id=artist_id))
# Delete Music
@app.route("/artists/<int:artist_id>/music/<int:music_id>", methods=["DELETE"])
def delete_music(artist_id, music_id): # pylint: disable=unused-argument
"""Route handler to delete an piece of music for an artist from the db.
Args:
artist_id: A str representing the id of the artist who's music is being
deleted
music_id: A str representing the id of the music to delete
Returns:
response: A json object signalling the deletion request was successful
"""
error = False
try:
music = Music.query.get(music_id)
music_type = music.type_
music_title = music.title
db.session.delete(music)
db.session.commit()
response = {"success": True}
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"{music_type} {music_title} was unable to be deleted!", "error")
abort(500)
flash(f"{music_type} {music_title} was successfully deleted!")
return jsonify(response)
# Create Unavailability
@app.route("/artists/<int:artist_id>/unavailability/create", methods=["POST"])
def create_unavailability(artist_id):
"""Creates a new unavailability for the artist from a form submission.
Returns:
The detail view for the artist
"""
form = UnavailabilityForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("show_artist", artist_id=artist_id))
error = False
try:
unavailability = Unavailability(
artist_id=artist_id,
start_time=request.form.get("start_time"),
end_time=request.form.get("end_time"),
)
db.session.add(unavailability)
db.session.commit()
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash("Unavailability could not be added!", "error")
abort(500)
flash("Unavailability was successfully added!")
return redirect(url_for("show_artist", artist_id=artist_id))
# Delete Unavailability
@app.route(
"/artists/<int:artist_id>/unavailability/<int:unavailability_id>",
methods=["DELETE"],
)
def delete_unavailability(
artist_id, unavailability_id # pylint: disable=unused-argument
):
"""Route handler to delete an unavailability for an artist from the db.
Args:
artist_id: A str representing the id of the artist who's unavailability
is being deleted
unavailability_id: A str representing the id of the unavailability to
delete
Returns:
response: A json object signalling the deletion request was successful
"""
error = False
try:
unavailability = Unavailability.query.get(unavailability_id)
db.session.delete(unavailability)
db.session.commit()
response = {"success": True}
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash("Unavailability was unable to be deleted!", "error")
abort(500)
flash("Unavailability was successfully deleted!")
return jsonify(response)
# Update Artist
# ----------------------------------------------------------------
@app.route("/artists/<int:artist_id>/edit", methods=["GET"])
def edit_artist_form(artist_id):
"""Displays the form for editing an artist.
Args:
artist_id: A str representing the id of the artist to edit
Returns:
A html template for the artist form which has been pre-populated
"""
artist = Artist.query.get(artist_id)
artist.city = artist.area.city
artist.state = artist.area.state
form = ArtistForm(obj=artist)
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):
"""Updates an existing artist in the db from a form submission.
Args:
artist_id: A str representing the id of the artist to update
Returns:
The template for the artist's detail page
"""
form = ArtistForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("edit_artist_form", artist_id=artist_id))
error = False
try:
artist_name = request.form.get("name")
city = request.form.get("city")
state = request.form.get("state")
artist = Artist.query.get(artist_id)
artist.name = artist_name
artist.genres = get_genres(request.form.getlist("genres"))
artist.area_id = get_area_id(city, state)
artist.phone = request.form.get("phone")
artist.website = request.form.get("website")
artist.facebook_link = request.form.get("facebook_link")
artist.seeking_venue = bool(request.form.get("seeking_venue"))
artist.seeking_description = request.form.get("seeking_description")
artist.image_link = request.form.get("image_link")
db.session.commit()
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"Artist {artist_name} was unable to be updated!", "error")
abort(500)
flash(f"Artist {artist_name} was successfully updated!")
return redirect(url_for("show_artist", artist_id=artist_id))
# Create Artist
# ----------------------------------------------------------------
@app.route("/artists/create", methods=["GET"])
def create_artist_form():
"""Displays the form for creating an artist.
Returns:
A html template for the artist form
"""
form = ArtistForm()
return render_template("forms/new_artist.html", form=form)
@app.route("/artists/create", methods=["POST"])
def create_artist_submission():
"""Creates a new artist in the db from a form submission.
Returns:
The template for a list of all artists
"""
form = ArtistForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("create_artist_form"))
error = False
try:
artist_name = request.form.get("name")
city = request.form.get("city")
state = request.form.get("state")
artist = Artist(
name=artist_name,
genres=get_genres(request.form.getlist("genres")),
area_id=get_area_id(city, state),
phone=request.form.get("phone"),
website=request.form.get("website"),
facebook_link=request.form.get("facebook_link"),
seeking_venue=bool(request.form.get("seeking_venue")),
seeking_description=request.form.get("seeking_description"),
image_link=request.form.get("image_link"),
)
db.session.add(artist)
db.session.commit()
artist_id = artist.id
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"Artist {artist_name} was unable to be listed!", "error")
abort(500)
flash(f"Artist {artist_name} was successfully listed!")
return redirect(url_for("show_artist", artist_id=artist_id))
# Delete Artist
# ----------------------------------------------------------------
@app.route("/artists/<artist_id>", methods=["DELETE"])
def delete_artist(artist_id):
"""Route handler to delete a artist from the db.
Args:
artist_id: A str representing the id of the artist to delete
Returns:
response: A json object signalling the deletion request was successful
"""
error = False
try:
artist = Artist.query.get(artist_id)
artist_name = artist.name
db.session.delete(artist)
db.session.commit()
response = {"success": True}
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash(f"Artist {artist_name} was unable to be deleted!", "error")
abort(500)
flash(f"Artist {artist_name} was successfully deleted!")
return jsonify(response)
# Shows
# ----------------------------------------------------------------
@app.route("/shows")
def shows():
"""Route handler for showing a list of shows.
Returns:
A template for a list of all shows
"""
shows_all = Show.query.all()
for show in shows_all:
show.venue_name = show.venue.name
show.artist_name = show.artist.name
show.artist_image_link = show.artist.image_link
show.start_time = str(show.start_time)
return render_template("pages/shows.html", shows=shows_all)
# Create Show
# ----------------------------------------------------------------
@app.route("/shows/create", methods=["GET"])
def create_show_form():
"""Displays the form for creating a show.
Returns:
A html template for the show form
"""
form = ShowForm()
return render_template("forms/new_show.html", form=form)
@app.route("/shows/create", methods=["POST"])
def create_show_submission():
"""Creates a new show in the db from a form submission.
Returns:
A redirect to the shows page
"""
form = ShowForm()
if not form.validate():
flash(
list(form.errors.values())[0][0], "error",
)
return redirect(url_for("create_show_form"))
error = False
try:
venue_id = request.form.get("venue_id")
artist_id = request.form.get("artist_id")
start_time = request.form.get("start_time")
unavailabilities = Unavailability.query.filter_by(
artist_id=artist_id
).all()
for unavailability in unavailabilities:
if (
str(unavailability.start_time)
> start_time
< str(unavailability.end_time)
):
flash("Artist is unavailable at selected time")
return redirect(url_for("create_show_form"))
show = Show(
venue_id=venue_id, artist_id=artist_id, start_time=start_time
)
db.session.add(show)
db.session.commit()
except Exception: # pylint: disable=broad-except
error = True
db.session.rollback()
print(sys.exc_info())
finally:
db.session.close()
if error:
flash("Show was unable to be listed!", "error")
abort(500)
flash("Show was successfully listed!")
return redirect(url_for("shows"))
# ----------------------------------------------------------------------------#
# Error Handlers
# ----------------------------------------------------------------------------#
@app.errorhandler(404)
def not_found_error(error): # pylint: disable=unused-argument
"""Error handler for 404 not found.
Args:
error: unused
Returns:
A custom template for the error message
"""
return render_template("errors/404.html"), 404
@app.errorhandler(500)
def server_error(error): # pylint: disable=unused-argument
"""Error handler for 500 internal server error.
Args:
error: unused
Returns:
A custom template for the error message
"""
return render_template("errors/500.html"), 500
# ----------------------------------------------------------------------------#
# Debug
# ----------------------------------------------------------------------------#
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.
# ----------------------------------------------------------------------------#
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
host = os.environ.get("HOST", "127.0.0.1")
app.run(host=host, port=port)