-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
766 lines (657 loc) · 26.9 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
# ----------------------------------------------------------------------------#
# Imports
# ----------------------------------------------------------------------------#
from distutils.command.config import config
import json
import sys
import dateutil.parser
import babel
from flask import (
Flask,
render_template,
jsonify,
request,
Response,
flash,
redirect,
session,
url_for,
)
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
from sqlalchemy import null
from models import db, Artist, Venue, Show
from forms import *
import json
# ----------------------------------------------------------------------------#
# App Config.
# ----------------------------------------------------------------------------#
app = Flask(__name__)
moment = Moment(app)
app.config.from_object("config")
db.init_app(app)
migrate = Migrate(app, db)
# TODO: connect to a local postgresql database
# ----------------------------------------------------------------------------#
# Models.
# ----------------------------------------------------------------------------#
# TODO: implement any missing fields, as a database migration using Flask-Migrate -- 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():
# TODO: replace with real venues data. -- done
# num_upcoming_shows should be aggregated based on number of upcoming shows per venue
data = []
venues = Venue.query.distinct(Venue.city).all()
for venue in venues:
data.append({"city": venue.city, "state": venue.state, "venues": []})
all_venues = Venue.query.all()
for venue in all_venues:
for single in data:
if single["city"] == venue.city:
single["venues"].append(
{
"id": venue.id,
"name": venue.name,
}
)
upcoming_shows_list = []
all_shows_at_venues = Show.query.filter_by(venue_id=venue.id).all()
for item in all_shows_at_venues:
if item.start_time > datetime.now():
upcoming_shows_list.append(
{
"artist_id": item.artist_id,
}
)
single["venues"][-1]["num_upcoming_shows"] = len(upcoming_shows_list)
return render_template("pages/venues.html", areas=data)
@app.route("/venues/search", methods=["POST"])
def search_venues():
# TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
response = {}
search_term = request.form.get("search_term", "")
venues = Venue.query.filter(Venue.name.ilike("%" + search_term + "%")).all()
response["count"] = len(venues)
response["data"] = []
for venue in venues:
response["data"].append(
{
"id": venue.id,
"name": venue.name,
"num_upcoming_shows": len(
Show.query.filter(
Show.venue_id == venue.id and Show.start_time > datetime.now()
).all()
),
}
)
print(response)
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 -- done
try:
data = []
venue_results = Venue.query.get(venue_id)
data.append(
{
"id": venue_id,
"name": venue_results.name,
"genres": venue_results.genres,
"address": venue_results.address,
"city": venue_results.city,
"state": venue_results.state,
"phone": venue_results.phone,
"website_link": venue_results.website_link,
"facebook_link": venue_results.facebook_link,
"seeking_talent": venue_results.seeking_talent,
"seeking_description": venue_results.seeking_description,
"image_link": venue_results.image_link,
"past_shows": [],
"upcoming_shows": [],
}
)
# retrive all the shows at that venue for past and upcoming show
def past_shows_fxn(venue_id):
shows_query = (
db.session.query(Show).join(Artist).filter(Show.venue_id == venue_id)
)
for shows in shows_query:
if shows.start_time < datetime.now():
data[0]["past_shows"].append(
{
"artist_id": shows.artist_id,
"artist_name": Artist.query.get(shows.artist_id).name,
"artist_image_link": Artist.query.get(
shows.artist_id
).image_link,
"start_time": shows.start_time.strftime(
"%Y-%m-%d %H:%M:%S"
),
}
)
else:
data[0]["upcoming_shows"].append(
{
"artist_id": shows.artist_id,
"artist_name": Artist.query.get(shows.artist_id).name,
"artist_image_link": Artist.query.get(
shows.artist_id
).image_link,
"start_time": shows.start_time.strftime(
"%Y-%m-%d %H:%M:%S"
),
}
)
data[0]["upcoming_shows_count"] = len(data[0]["upcoming_shows"])
data[0]["past_shows_count"] = len(data[0]["past_shows"])
past_shows_fxn(venue_id)
except:
print(sys.exc_info())
finally:
data = list(filter(lambda d: d["id"] == venue_id, data))[0]
print(data)
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():
# 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)
if form.validate():
try:
name = request.form.get('name')
city = request.form.get('city')
state = request.form.get('state')
address = request.form.get('address')
phone = request.form.get('phone')
genres = request.form.getlist("genres")
image_link = request.form.get('image_link')
website_link = request.form.get('website_link')
facebook_link = request.form.get('facebook_link')
def seeking():
if request.form.get("seeking_talent") == "y":
return True
else:
return False
seeking_talent = seeking()
seeking_description = request.form.get("seeking_description")
venue = Venue(
name=name,
city=city,
state=state,
address=address,
phone=phone,
genres=genres,
facebook_link=facebook_link,
image_link=image_link,
website_link=website_link,
seeking_talent=seeking_talent,
seeking_description=seeking_description,
)
db.session.add(venue)
db.session.commit()
# on successful db insert, flash success
flash("Venue " + name + " was successfully listed!")
except:
name = request.form.get("name")
db.session.rollback()
print(sys.exc_info())
# TODO: on unsuccessful db insert, flash an error instead.
flash("An error occurred. Venue " + name + " could not be listed.")
finally:
db.session.close()
# 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")
else:
for field,message in form.errors.items():
flash(message)
return render_template("forms/new_venue.html", form=form)
@app.route("/venues/<venue_id>/delete", methods=["DELETE"])
def delete_venue(venue_id):
# TODO: Complete this endpoint for taking a venue_id, and using --done
# SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
try:
venue = Venue.query.with_entities(Venue.name).filter_by(id=venue_id).first()
# query shows table to find all the shows at that venue_id
show = Show.query.filter_by(venue_id=venue_id).all()
# if shows have such venue first delete them to prevent break in the app
# if no shows, delete the venue from the db.venues table and flash the success message
if len(show) == 0:
Venue.query.filter_by(id=venue_id).delete()
db.session.commit()
# on successful db insert, flash success
flash("Venue " + venue.name + " was successfully deleted!")
if len(show) > 0:
Show.query.filter_by(venue_id=venue_id).delete()
Venue.query.filter_by(id=venue_id).delete()
db.session.commit()
# flash success note to the frontend
flash("Venue " + venue.name + " was successfully deleted!")
except:
db.session.rollback()
print(sys.exc_info())
flash("An error occurred. Venue " + venue.name + " could not be deleted.")
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
# Note we gonna use fetch method in the frontend , hence the redirect to homepage is not necessary
# We will return a success message to the frontend
# return render_template('pages/home.html')
return {"success": True}
# Artists
# ----------------------------------------------------------------
@app.route("/artists")
def artists():
# TODO: replace with real data returned from querying the database --done
needed_fields = [Artist.id, Artist.name]
data = Artist.query.with_entities(*needed_fields)
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".
# --done
response = {}
try:
# get the query string from the request
query_string = request.form.get("search_term")
# get the data from the db
data = Artist.query.filter(Artist.name.ilike("%" + query_string + "%")).all()
# get the data in the needed format
response["count"] = len(data)
response["data"] = []
for d in data:
response.get("data").append({"id": d.id, "name": d.name})
# determine the number of upcoming shows for each artist
shows = Show.query.filter(Show.artist_id == d.id).all()
# if returned shows are not empty, shows for artist is obviously 0
if len(shows) == 0:
response.get("data")[-1]["num_upcoming_shows"] = 0
else:
for show in shows:
count = 0
if show.start_time > datetime.now():
count += 1
response.get("data")[-1]["num_upcoming_shows"] = count
except:
print(sys.exc_info())
finally:
db.session.close()
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
# TODO: replace with real artist data from the artist table, using artist_id --done
artist_information = Artist.query.get(artist_id)
data = {}
data["id"] = artist_information.id
data["name"] = artist_information.name
data["city"] = artist_information.city
data["state"] = artist_information.state
data["phone"] = artist_information.phone
data["genres"] = artist_information.genres
data["website_link"] = artist_information.website_link
data["facebook_link"] = artist_information.facebook_link
data["seeking_venue"] = artist_information.seeking_venue
data["seeking_description"] = artist_information.seeking_description
data["image_link"] = artist_information.image_link
data["past_shows"] = []
data["upcoming_shows"] = []
query_show_for_shows = Show.query.filter_by(artist_id=artist_id).all()
for allshows in query_show_for_shows:
if allshows.start_time > datetime.now():
data["upcoming_shows"].append(
{
"venue_id": allshows.venue_id,
"venue_name": allshows.venue.name,
"venue_image_link": allshows.venue.image_link,
"start_time": str(allshows.start_time),
}
)
else:
data["past_shows"].append(
{
"venue_id": allshows.venue_id,
"venue_name": allshows.venue.name,
"venue_image_link": allshows.venue.image_link,
"start_time": str(allshows.start_time),
}
)
data["past_shows_count"] = len(data["past_shows"])
data["upcoming_shows_count"] = len(data["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):
# TODO: populate form with fields from artist with ID <artist_id> --done
artist = Artist.query.get(artist_id)
form = ArtistForm(obj=artist)
# we gonna populate the form with the data of the artist we are editing
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)
# validate form data
if form.validate():
try:
# get the data with the artist_id we are editing
artist = Artist.query.get(artist_id)
artist.name = request.form.get("name")
artist.city = request.form.get("city")
artist.state = request.form.get("state")
artist.phone = request.form.get("phone")
artist.genres = request.form.get("genres")
artist.website_link = request.form.get("website_link")
artist.facebook_link = request.form.get("facebook_link")
artist.seeking_description = request.form.get("seeking_description")
artist.image_link = request.form.get("image_link")
def seeking_venue():
if request.form.get("seeking_venue") == "y":
return True
else:
return False
artist.seeking_venue = seeking_venue()
db.session.commit()
flash("Artist " + request.form["name"] + " was successfully updated!")
except:
db.session.rollback()
print(sys.exc_info())
flash(
"An error occurred. Artist "
+ request.form["name"]
+ " could not be updated."
)
finally:
db.session.close()
return redirect(url_for("show_artist", artist_id=artist_id))
else:
for field, message in form.errors.items():
flash(message)
return redirect(url_for("edit_artist", artist_id=artist_id))
@app.route("/venues/<int:venue_id>/edit", methods=["GET"])
def edit_venue(venue_id):
venue = Venue.query.get(venue_id)
form = VenueForm(obj=venue)
# TODO: populate form with values from venue with ID <venue_id> -- done partial
venue = Venue.query.get(venue_id)
print(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):
# TODO: take values from the form submitted, and update existing
# venue record with ID <venue_id> using the new attributes
try:
edit_current_venue = Venue.query.get(venue_id)
edit_current_venue.name = request.form.get("name")
edit_current_venue.city = request.form.get("city")
edit_current_venue.state = request.form.get("state")
edit_current_venue.address = request.form.get("address")
edit_current_venue.phone = request.form.get("phone")
edit_current_venue.genres = request.form.getlist("genres")
edit_current_venue.facebook_link = request.form["facebook_link"]
edit_current_venue.image_link = request.form["image_link"]
edit_current_venue.website_link = request.form["website_link"]
def seekvalues():
if request.form.get("seeking_talent") == "y":
return True
else:
return False
edit_current_venue.seeking_talent = seekvalues()
edit_current_venue.seeking_description = request.form["seeking_description"]
db.session.commit()
flash("Venue " + request.form["name"] + " was successfully updated!")
except:
db.session.rollback()
print(sys.exc_info())
flash(
"An error occurred. Venue "
+ request.form["name"]
+ " could not be updated."
)
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 --done
# TODO: modify data to be the data object returned from db insertion --done
form = ArtistForm(request.form)
if form.validate():
try:
name = request.form.get("name")
city = request.form.get("city")
state = request.form.get("state")
phone = request.form.get("phone")
genres = request.form.getlist("genres")
facebook_link = request.form.get("facebook_link")
image_link = request.form.get("image_link")
website_link = request.form.get("website_link")
def seeking_venue():
if request.form.get("seeking_venue") == "y":
return True
else:
return False
seeking_venue = seeking_venue()
seeking_description = request.form.get("seeking_description")
artist = Artist(
name=name,
city=city,
state=state,
phone=phone,
genres=genres,
facebook_link=facebook_link,
image_link=image_link,
website_link=website_link,
seeking_venue=seeking_venue,
seeking_description=seeking_description,
)
db.session.add(artist)
db.session.commit()
# on successful db insert, flash success
flash("Artist " + request.form["name"] + " was successfully listed!")
except:
artist_name = request.form["name"]
db.session.rollback()
print(sys.exc_info())
# TODO: on unsuccessful db insert, flash an error instead.
flash("An error occurred. Artist " + artist_name + " could not be listed.")
finally:
db.session.close()
return render_template("pages/home.html")
else:
for field,message in form.errors.items():
flash(message)
return render_template("forms/new_artist.html", form=form)
# shows the recently linked shows at the homepage
@app.route("/recently-listed-artist", methods=["GET"])
def recently_linked_shows():
# get the most recent shows from the database
data = []
recent_shows = Artist.query.order_by(Artist.id.desc()).limit(10)
for all in recent_shows:
data.append(
{
"artist_id": all.id,
"artist_name": all.name,
"artist_image_link": all.image_link,
"upcoming_shows": len(
Show.query.filter_by(artist_id=all.id)
.filter(Show.start_time > datetime.now())
.all()
),
}
)
return jsonify(data)
@app.route("/recently-listed-venue", methods=["GET"])
def recently_listed_venue():
# get the most recent venues from the database
data = []
recent_venues = Venue.query.order_by(Venue.id.desc()).limit(10)
for all in recent_venues:
data.append(
{
"venue_id": all.id,
"venue_name": all.name,
"venue_image_link": all.image_link,
"upcoming_shows": len(
Show.query.filter_by(venue_id=all.id)
.filter(Show.start_time > datetime.now())
.all()
),
}
)
return jsonify(data)
# Shows
# ----------------------------------------------------------------
@app.route("/shows")
def shows():
# displays list of shows at /shows
# TODO: replace with real venues data. -- done
allshows = Show.query.all()
data = []
for show in allshows:
# get artist_id,venue_id
current_artist_id = show.artist_id
current_venue_id = show.venue_id
# retrieve artist and venue data using their ids
artist_id_info = Artist.query.get(current_artist_id)
venue_id_info = Venue.query.get(current_venue_id)
# append the required data into the data list
data.append(
{
"venue_id": venue_id_info.id,
"venue_name": venue_id_info.name,
"artist_id": artist_id_info.id,
"artist_name": artist_id_info.name,
"artist_image_link": artist_id_info.image_link,
"start_time": show.start_time.strftime("%Y-%m-%d %H:%M:%S"),
}
)
# create a view using the data from the model
return render_template("pages/shows.html", shows=data)
@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
# TODO: insert form data as a new Show record in the db, instead --done
form = ShowForm()
try:
data = request.form
artist_id = data["artist_id"]
venue_id = data["venue_id"]
start_date = data["start_time"]
# filter vernue table if id exixt
isVenue = db.session.query(Venue).filter_by(id=venue_id).first()
isArtist = db.session.query(Artist).filter_by(id=artist_id).first()
# check if venue and artist exist
if isVenue is None:
flash("venue with id " + venue_id + " does not exist")
return render_template("pages/home.html")
if isArtist is None:
flash("artist with id " + artist_id + " does not exist")
return render_template("pages/home.html")
if isArtist is None and isVenue is None:
flash(
"venue and artist dose not exist, create an artist profile and venue profile first"
)
return render_template("pages/home.html")
# create a new show
new_show = Show(artist_id=artist_id, venue_id=venue_id, start_time=start_date)
db.session.add(new_show)
db.session.commit()
# on successful db insert, flash success
flash("Show was successfully listed!")
except:
db.session.rollback()
print(sys.exc_info())
# TODO: on unsuccessful db insert, flash an error instead. -- done
flash("An error occurred. Show could not be listed.")
finally:
db.session.close()
# 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()
# Or specify port manually:
"""
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
"""