Advertisement
brandonmunda1

FyyurAppBeforeEditing

Aug 21st, 2022
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 31.67 KB | None | 0 0
  1. #----------------------------------------------------------------------------#
  2. # Imports
  3. #----------------------------------------------------------------------------#
  4.  
  5. import json
  6. from urllib.request import Request
  7. import dateutil.parser
  8. import babel
  9. from flask import Flask, render_template, request, Response, flash, redirect, url_for
  10. from flask_moment import Moment
  11. from flask_sqlalchemy import SQLAlchemy
  12. import logging
  13. from logging import Formatter, FileHandler
  14. from flask_wtf import Form
  15. from forms import *
  16. from flask_migrate import Migrate # Imports the module required to for us to carry out our database migrations
  17. from models import *
  18. #----------------------------------------------------------------------------#
  19. # App Config.
  20. #----------------------------------------------------------------------------#
  21.  
  22. app = Flask(__name__)
  23. moment = Moment(app)
  24. app.config.from_object('config')
  25. db.init_app(app)
  26. migrate = Migrate(app, db) # Allows for migrations to be carried out
  27.  
  28. # TODO: connect to a local postgresql database
  29.  
  30.  
  31. #----------------------------------------------------------------------------#
  32. # Filters.
  33. #----------------------------------------------------------------------------#
  34.  
  35. def format_datetime(value, format='medium'):
  36.   if isinstance(value, str):
  37.     date = dateutil.parser.parse(value)
  38.   else:
  39.     date = value
  40.  
  41.   if format == 'full':
  42.       format="EEEE MMMM, d, y 'at' h:mma"
  43.   elif format == 'medium':
  44.       format="EE MM, dd, y h:mma"
  45.   return babel.dates.format_datetime(date, format, locale='en')
  46.  
  47. app.jinja_env.filters['datetime'] = format_datetime
  48.  
  49. #----------------------------------------------------------------------------#
  50. # Controllers.
  51. #----------------------------------------------------------------------------#
  52.  
  53. @app.route('/')
  54. def index():
  55.   return render_template('pages/home.html')
  56.  
  57.  
  58. #  Venues
  59. #  ----------------------------------------------------------------
  60.  
  61. @app.route('/venues')
  62. def venues():
  63.   # TODO: replace with real venues data.
  64.   #       num_upcoming_shows should be aggregated based on number of upcoming shows per venue.
  65.  
  66.   data = []
  67.  
  68.   location_query = Venue.query.distinct(Venue.state, Venue.city).all() #Gets a list of all unique states and cities
  69.  
  70.   locations = {}
  71.   for location in location_query:
  72.     locations = {
  73.       "city": location.city,
  74.       "state": location.state,
  75.       "venues": [] # we will store all the venues with the specific location details in this list
  76.     }
  77.  
  78.     venues = Venue.query.filter_by(state=location.state, city=location.city).all()
  79.     for venue in venues:
  80.       locations['venues'].append({
  81.         "id": venue.id,
  82.         "name": venue.name,
  83.         "num_upcoming_shows" : Show.query.join(Venue).filter(Show.venue_id==venue.id).filter(Show.start_time>datetime.now()).count()
  84.       })
  85.  
  86.       data.append(locations)
  87.  
  88.  
  89.   # data=[{
  90.   #   "city": "San Francisco",
  91.   #   "state": "CA",
  92.   #   "venues": [{
  93.   #     "id": 1,
  94.   #     "name": "The Musical Hop",
  95.   #     "num_upcoming_shows": 0,
  96.   #   }, {
  97.   #     "id": 3,
  98.   #     "name": "Park Square Live Music & Coffee",
  99.   #     "num_upcoming_shows": 1,
  100.   #   }]
  101.   # }, {
  102.   #   "city": "New York",
  103.   #   "state": "NY",
  104.   #   "venues": [{
  105.   #     "id": 2,
  106.   #     "name": "The Dueling Pianos Bar",
  107.   #     "num_upcoming_shows": 0,
  108.   #   }]
  109.   # }]
  110.   return render_template('pages/venues.html', areas=data)
  111.  
  112. @app.route('/venues/search', methods=['POST'])
  113. def search_venues():
  114.   # TODO: implement search on venues with partial string search. Ensure it is case-insensitive.
  115.   # seach for Hop should return "The Musical Hop".
  116.   # search for "Music" should return "The Musical Hop" and "Park Square Live Music & Coffee"
  117.  
  118.   search_term=request.form.get('search_term', '')
  119.   search = "%{}%".format(search_term)
  120.   response = {
  121.     "count": Venue.query.filter(Venue.name.ilike(search)).count(),
  122.     "data": []
  123.   }
  124.  
  125.   search_results = Venue.query.filter(Venue.name.ilike(search)).all()
  126.  
  127.   for result in search_results:
  128.     response["data"].append({
  129.       "id": result.id,
  130.       "name": result.name,
  131.       "num_upcoming_shows": Show.query.join(Venue).filter(Show.venue_id==result.id).filter(Show.start_time>datetime.now()).count()
  132.     })
  133.  
  134.   # response={
  135.   #   "count": 1,
  136.   #   "data": [{
  137.   #     "id": 2,
  138.   #     "name": "The Dueling Pianos Bar",
  139.   #     "num_upcoming_shows": 0,
  140.   #   }]
  141.   # }
  142.   return render_template('pages/search_venues.html', results=response, search_term=request.form.get('search_term', ''))
  143.  
  144. @app.route('/venues/<int:venue_id>')
  145. def show_venue(venue_id):
  146.   # shows the venue page with the given venue_id
  147.   # TODO: replace with real venue data from the venues table, using venue_id
  148.  
  149.   past_shows_query = Show.query.join(Venue).filter(Show.venue_id==venue_id).filter(datetime.utcnow()>Show.start_time).all()
  150.   past_shows = []
  151.   for show in past_shows_query:
  152.     past_shows.append({
  153.       "artist_id": show.artist_id,
  154.       "artist_name": Artist.query.get(show.artist_id).name,
  155.       "artist_image_link": Artist.query.get(show.artist_id).image_link,
  156.       "start_time": show.start_time
  157.  
  158.     })
  159.  
  160.   upcoming_shows_query = Show.query.join(Venue).filter(Show.venue_id==venue_id).filter(datetime.utcnow()<Show.start_time).all()
  161.   upcoming_shows = []
  162.   for show in upcoming_shows_query:
  163.     upcoming_shows.append({
  164.       "artist_id": show.artist_id,
  165.       "artist_name": Artist.query.get(show.artist_id).name,
  166.       "artist_image_link": Artist.query.get(show.artist_id).image_link,
  167.       "start_time": show.start_time
  168.  
  169.     })
  170.  
  171.   past_shows_count = len(past_shows)
  172.   upcoming_shows_count = len(upcoming_shows)
  173.  
  174.   venue_information = Venue.query.get(venue_id)
  175.  
  176.   data = {
  177.     "id": venue_information.id,
  178.     "name": venue_information.name,
  179.     "genres": venue_information.genres,
  180.     "address": venue_information.address,
  181.     "city": venue_information.city,
  182.     "state": venue_information.state,
  183.     "phone": venue_information.phone,
  184.     "website": venue_information.website_link,
  185.     "facebook_link": venue_information.facebook_link,
  186.     "seeking_talent": venue_information.seeking_talent,
  187.     "seeking_description": venue_information.seeking_description,
  188.     "image_link": venue_information.image_link,
  189.     "past_shows": past_shows,
  190.     "upcoming_shows": upcoming_shows,
  191.     "past_shows_count": past_shows_count,
  192.     "upcoming_shows_count": upcoming_shows_count
  193.   }
  194.  
  195.   # data1={
  196.   #   "id": 1,
  197.   #   "name": "The Musical Hop",
  198.   #   "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
  199.   #   "address": "1015 Folsom Street",
  200.   #   "city": "San Francisco",
  201.   #   "state": "CA",
  202.   #   "phone": "123-123-1234",
  203.   #   "website": "https://www.themusicalhop.com",
  204.   #   "facebook_link": "https://www.facebook.com/TheMusicalHop",
  205.   #   "seeking_talent": True,
  206.   #   "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
  207.   #   "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60",
  208.   #   "past_shows": [{
  209.   #     "artist_id": 4,
  210.   #     "artist_name": "Guns N Petals",
  211.   #     "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",
  212.   #     "start_time": "2019-05-21T21:30:00.000Z"
  213.   #   }],
  214.   #   "upcoming_shows": [],
  215.   #   "past_shows_count": 1,
  216.   #   "upcoming_shows_count": 0,
  217.   # }
  218.   # data2={
  219.   #   "id": 2,
  220.   #   "name": "The Dueling Pianos Bar",
  221.   #   "genres": ["Classical", "R&B", "Hip-Hop"],
  222.   #   "address": "335 Delancey Street",
  223.   #   "city": "New York",
  224.   #   "state": "NY",
  225.   #   "phone": "914-003-1132",
  226.   #   "website": "https://www.theduelingpianos.com",
  227.   #   "facebook_link": "https://www.facebook.com/theduelingpianos",
  228.   #   "seeking_talent": False,
  229.   #   "image_link": "https://images.unsplash.com/photo-1497032205916-ac775f0649ae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80",
  230.   #   "past_shows": [],
  231.   #   "upcoming_shows": [],
  232.   #   "past_shows_count": 0,
  233.   #   "upcoming_shows_count": 0,
  234.   # }
  235.   # data3={
  236.   #   "id": 3,
  237.   #   "name": "Park Square Live Music & Coffee",
  238.   #   "genres": ["Rock n Roll", "Jazz", "Classical", "Folk"],
  239.   #   "address": "34 Whiskey Moore Ave",
  240.   #   "city": "San Francisco",
  241.   #   "state": "CA",
  242.   #   "phone": "415-000-1234",
  243.   #   "website": "https://www.parksquarelivemusicandcoffee.com",
  244.   #   "facebook_link": "https://www.facebook.com/ParkSquareLiveMusicAndCoffee",
  245.   #   "seeking_talent": False,
  246.   #   "image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
  247.   #   "past_shows": [{
  248.   #     "artist_id": 5,
  249.   #     "artist_name": "Matt Quevedo",
  250.   #     "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",
  251.   #     "start_time": "2019-06-15T23:00:00.000Z"
  252.   #   }],
  253.   #   "upcoming_shows": [{
  254.   #     "artist_id": 6,
  255.   #     "artist_name": "The Wild Sax Band",
  256.   #     "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",
  257.   #     "start_time": "2035-04-01T20:00:00.000Z"
  258.   #   }, {
  259.   #     "artist_id": 6,
  260.   #     "artist_name": "The Wild Sax Band",
  261.   #     "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",
  262.   #     "start_time": "2035-04-08T20:00:00.000Z"
  263.   #   }, {
  264.   #     "artist_id": 6,
  265.   #     "artist_name": "The Wild Sax Band",
  266.   #     "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",
  267.   #     "start_time": "2035-04-15T20:00:00.000Z"
  268.   #   }],
  269.   #   "past_shows_count": 1,
  270.   #   "upcoming_shows_count": 1,
  271.   # }
  272.   # data = list(filter(lambda d: d['id'] == venue_id, [data1, data2, data3]))[0]
  273.   return render_template('pages/show_venue.html', venue=data)
  274.  
  275. #  Create Venue
  276. #  ----------------------------------------------------------------
  277.  
  278. @app.route('/venues/create', methods=['GET'])
  279. def create_venue_form():
  280.   form = VenueForm()
  281.   return render_template('forms/new_venue.html', form=form)
  282.  
  283. @app.route('/venues/create', methods=['POST'])
  284. def create_venue_submission():
  285.   # TODO: insert form data as a new Venue record in the db, instead
  286.   # TODO: modify data to be the data object returned from db insertion
  287.  
  288.   form = VenueForm(request.form)
  289.  
  290.   if form.validate():
  291.  
  292.     data = Venue(
  293.       name = form.name.data,
  294.       city = form.city.data,
  295.       state = form.state.data,
  296.       address = form.address.data,
  297.       phone = form.phone.data,
  298.       #genres = ', '.join(form.genres.data),
  299.       genres = form.genres.data,
  300.       #genres = request.form.getlist('genres'),
  301.       image_link = form.image_link.data,
  302.       facebook_link = form.facebook_link.data,
  303.       website_link = form.website_link.data,
  304.       seeking_talent = form.seeking_talent.data,
  305.       seeking_description = form.seeking_description.data)
  306.  
  307.     try:
  308.       db.session.add(data)
  309.       db.session.commit()
  310.  
  311.       # on successful db insert, flash success
  312.       flash('Venue ' + request.form['name'] + ' was successfully listed!')
  313.     # TODO: on unsuccessful db insert, flash an error instead.
  314.  
  315.     except:
  316.       db.session.rollback()
  317.       db.session.commit()
  318.       flash('An error occurred. Venue ' + data.name + ' could not be listed.')
  319.  
  320.     finally:
  321.       db.session.close()
  322.  
  323.   else:
  324.     for field, message in form.errors.items():
  325.       flash(f'An error occured({message}). Kindly check the following field: {field}.')
  326.  
  327.  
  328.  
  329.  
  330.   # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
  331.   # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
  332.   return render_template('pages/home.html')
  333.  
  334. @app.route('/venues/<venue_id>', methods=['DELETE'])
  335. def delete_venue(venue_id):
  336.   venue_to_delete = Venue.query.get(venue_id)
  337.  
  338.   try:
  339.     db.session.delete(venue_to_delete)
  340.     db.session.commit()
  341.     flash(f'Venue {venue_id} deleted!!!')
  342.  
  343.   except:
  344.     db.session.rollback()    
  345.     flash(f'An error occured. Venue {venue_id} not deleted!!!')
  346.  
  347.   finally:
  348.     db.session.close()
  349.   # TODO: Complete this endpoint for taking a venue_id, and using
  350.   # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
  351.  
  352.   # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
  353.   # clicking that button delete it from the db then redirect the user to the homepage
  354.   return None
  355.  
  356. #  Artists
  357. #  ----------------------------------------------------------------
  358. @app.route('/artists')
  359. def artists():
  360.   # TODO: replace with real data returned from querying the database
  361.  
  362.   data = []
  363.  
  364.   artists_query = Artist.query.all()
  365.  
  366.   for artist in artists_query:
  367.     data.append({
  368.       "id": artist.id,
  369.       "name": artist.name
  370.     })
  371.  
  372.   # data=[{
  373.   #   "id": 4,
  374.   #   "name": "Guns N Petals",
  375.   # }, {
  376.   #   "id": 5,
  377.   #   "name": "Matt Quevedo",
  378.   # }, {
  379.   #   "id": 6,
  380.   #   "name": "The Wild Sax Band",
  381.   # }]
  382.   return render_template('pages/artists.html', artists=data)
  383.  
  384. @app.route('/artists/search', methods=['POST'])
  385. def search_artists():
  386.   # TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
  387.   # seach for "A" should return "Guns N Petals", "Matt Quevado", and "The Wild Sax Band".
  388.   # search for "band" should return "The Wild Sax Band".
  389.   search_term=request.form.get('search_term', '')
  390.   search = "%{}%".format(search_term)
  391.   response = {
  392.     "count": Artist.query.filter(Artist.name.ilike(search)).count(),
  393.     "data": []
  394.   }
  395.  
  396.   search_results = Artist.query.filter(Artist.name.ilike(search)).all()
  397.  
  398.   for result in search_results:
  399.     response["data"].append({
  400.       "id": result.id,
  401.       "name": result.name,
  402.       "num_upcoming_shows": Show.query.join(Artist).filter(Show.artist_id==result.id).filter(Show.start_time>datetime.now()).count()
  403.     })
  404.  
  405.   # response={
  406.   #   "count": 1,
  407.   #   "data": [{
  408.   #     "id": 4,
  409.   #     "name": "Guns N Petals",
  410.   #     "num_upcoming_shows": 0,
  411.   #   }]
  412.   # }
  413.   return render_template('pages/search_artists.html', results=response, search_term=request.form.get('search_term', ''))
  414.  
  415. @app.route('/artists/<int:artist_id>')
  416. def show_artist(artist_id):
  417.   # shows the artist page with the given artist_id
  418.   # TODO: replace with real artist data from the artist table, using artist_id
  419.  
  420.  
  421.   past_shows_query = Show.query.join(Artist).filter(Show.artist_id==artist_id).filter(datetime.utcnow()>Show.start_time).all()
  422.   past_shows = []
  423.   for show in past_shows_query:
  424.     past_shows.append({
  425.       "venue_id": show.venue_id,
  426.       "venue_name": Venue.query.get(show.venue_id).name,
  427.       "venue_image_link": Venue.query.get(show.venue_id).image_link,
  428.       "start_time": show.start_time
  429.  
  430.     })
  431.  
  432.   upcoming_shows_query = Show.query.join(Artist).filter(Show.artist_id==artist_id).filter(datetime.utcnow()<Show.start_time).all()
  433.   upcoming_shows = []
  434.   for show in upcoming_shows_query:
  435.     upcoming_shows.append({
  436.       "venue_id": show.venue_id,
  437.       "venue_name": Venue.query.get(show.venue_id).name,
  438.       "venue_image_link": Venue.query.get(show.venue_id).image_link,
  439.       "start_time": show.start_time
  440.  
  441.     })
  442.  
  443.   past_shows_count = len(past_shows)
  444.   upcoming_shows_count = len(upcoming_shows)
  445.  
  446.   artist_information = Artist.query.get(artist_id)
  447.  
  448.   data = {
  449.     "id": artist_information.id,
  450.     "name": artist_information.name,
  451.     "genres": artist_information.genres,
  452.     "city": artist_information.city,
  453.     "state": artist_information.state,
  454.     "phone": artist_information.phone,
  455.     "website": artist_information.website_link,
  456.     "facebook_link": artist_information.facebook_link,
  457.     "seeking_venue": artist_information.seeking_venue,
  458.     "seeking_description":artist_information.seeking_description,
  459.     "image_link": artist_information.image_link,
  460.     "past_shows": past_shows,
  461.     "upcoming_shows": upcoming_shows,
  462.     "past_shows_count": past_shows_count,
  463.     "upcoming_shows_count": upcoming_shows_count
  464.   }
  465.  
  466.  
  467.  
  468.  
  469.   # data1={
  470.   #   "id": 4,
  471.   #   "name": "Guns N Petals",
  472.   #   "genres": ["Rock n Roll"],
  473.   #   "city": "San Francisco",
  474.   #   "state": "CA",
  475.   #   "phone": "326-123-5000",
  476.   #   "website": "https://www.gunsnpetalsband.com",
  477.   #   "facebook_link": "https://www.facebook.com/GunsNPetals",
  478.   #   "seeking_venue": True,
  479.   #   "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
  480.   #   "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
  481.   #   "past_shows": [{
  482.   #     "venue_id": 1,
  483.   #     "venue_name": "The Musical Hop",
  484.   #     "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",
  485.   #     "start_time": "2019-05-21T21:30:00.000Z"
  486.   #   }],
  487.   #   "upcoming_shows": [],
  488.   #   "past_shows_count": 1,
  489.   #   "upcoming_shows_count": 0,
  490.   # }
  491.   # data2={
  492.   #   "id": 5,
  493.   #   "name": "Matt Quevedo",
  494.   #   "genres": ["Jazz"],
  495.   #   "city": "New York",
  496.   #   "state": "NY",
  497.   #   "phone": "300-400-5000",
  498.   #   "facebook_link": "https://www.facebook.com/mattquevedo923251523",
  499.   #   "seeking_venue": False,
  500.   #   "image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
  501.   #   "past_shows": [{
  502.   #     "venue_id": 3,
  503.   #     "venue_name": "Park Square Live Music & Coffee",
  504.   #     "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",
  505.   #     "start_time": "2019-06-15T23:00:00.000Z"
  506.   #   }],
  507.   #   "upcoming_shows": [],
  508.   #   "past_shows_count": 1,
  509.   #   "upcoming_shows_count": 0,
  510.   # }
  511.   # data3={
  512.   #   "id": 6,
  513.   #   "name": "The Wild Sax Band",
  514.   #   "genres": ["Jazz", "Classical"],
  515.   #   "city": "San Francisco",
  516.   #   "state": "CA",
  517.   #   "phone": "432-325-5432",
  518.   #   "seeking_venue": False,
  519.   #   "image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
  520.   #   "past_shows": [],
  521.   #   "upcoming_shows": [{
  522.   #     "venue_id": 3,
  523.   #     "venue_name": "Park Square Live Music & Coffee",
  524.   #     "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",
  525.   #     "start_time": "2035-04-01T20:00:00.000Z"
  526.   #   }, {
  527.   #     "venue_id": 3,
  528.   #     "venue_name": "Park Square Live Music & Coffee",
  529.   #     "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",
  530.   #     "start_time": "2035-04-08T20:00:00.000Z"
  531.   #   }, {
  532.   #     "venue_id": 3,
  533.   #     "venue_name": "Park Square Live Music & Coffee",
  534.   #     "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",
  535.   #     "start_time": "2035-04-15T20:00:00.000Z"
  536.   #   }],
  537.   #   "past_shows_count": 0,
  538.   #   "upcoming_shows_count": 3,
  539.   # }
  540.   # data = list(filter(lambda d: d['id'] == artist_id, [data1, data2, data3]))[0]
  541.   return render_template('pages/show_artist.html', artist=data)
  542.  
  543. #  Update
  544. #  ----------------------------------------------------------------
  545. @app.route('/artists/<int:artist_id>/edit', methods=['GET'])
  546. def edit_artist(artist_id):
  547.   form = ArtistForm()
  548.   artist = Artist.query.get_or_404(artist_id)
  549.  
  550.   form.name.data = artist.name
  551.   form.genres.data = artist.genres
  552.   form.city.data = artist.city
  553.   form.state.data = artist.state
  554.   form.phone.data = artist.phone
  555.   form.website_link.data = artist.website_link
  556.   form.facebook_link.data = artist.facebook_link
  557.   form.seeking_venue.data = artist.seeking_venue
  558.   form.seeking_description.data = artist.seeking_description
  559.   form.image_link.data = artist.image_link
  560.  
  561.  
  562.   # artist={
  563.   #   "id": 4,
  564.   #   "name": "Guns N Petals",
  565.   #   "genres": ["Rock n Roll"],
  566.   #   "city": "San Francisco",
  567.   #   "state": "CA",
  568.   #   "phone": "326-123-5000",
  569.   #   "website": "https://www.gunsnpetalsband.com",
  570.   #   "facebook_link": "https://www.facebook.com/GunsNPetals",
  571.   #   "seeking_venue": True,
  572.   #   "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
  573.   #   "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80"
  574.   # }
  575.   # TODO: populate form with fields from artist with ID <artist_id>
  576.   return render_template('forms/edit_artist.html', form=form, artist=artist)
  577.  
  578. @app.route('/artists/<int:artist_id>/edit', methods=['POST'])
  579. def edit_artist_submission(artist_id):
  580.   # TODO: take values from the form submitted, and update existing
  581.   # artist record with ID <artist_id> using the new attributes
  582.   form = ArtistForm(request.form)
  583.   artist = Artist.query.get_or_404(artist_id)
  584.  
  585.   artist.name = form.name.data
  586.   #artist.genres = ",".join(form.genres.data)
  587.   artist.genres = form.genres.data
  588.   artist.city = form.city.data
  589.   artist.state = form.state.data
  590.   artist.phone = form.phone.data
  591.   artist.website_link = form.website_link.data
  592.   artist.facebook_link = form.facebook_link.data
  593.   artist.seeking_venue =  form.seeking_venue.data
  594.   artist.seeking_description = form.seeking_description.data
  595.   artist.image_link = form.image_link.data
  596.  
  597.   artist={
  598.     "id": artist_id,
  599.     "name": artist.name,
  600.     "genres": artist.genres,
  601.     "city": artist.city,
  602.     "state": artist.state,
  603.     "phone": artist.phone,
  604.     "website": artist.website_link,
  605.     "facebook_link": artist.facebook_link,
  606.     "seeking_venue": artist.seeking_venue,
  607.     "seeking_description": artist.seeking_description,
  608.     "image_link": artist.image_link
  609.   }
  610.  
  611.   try:
  612.     db.session.commit()
  613.     flash(f'Artist {artist_id} updated successfully!!!')
  614.  
  615.   except:
  616.     db.session.rollback()
  617.     flash(f'An error occured, artist {artist_id} not updated!!!')
  618.  
  619.   finally:
  620.     db.session.close()
  621.  
  622.   return redirect(url_for('show_artist', artist_id=artist_id))
  623.  
  624. @app.route('/venues/<int:venue_id>/edit', methods=['GET'])
  625. def edit_venue(venue_id):
  626.   form = VenueForm()
  627.   venue = Venue.query.get(venue_id)
  628.  
  629.   form.name.data = venue.name
  630.   form.genres.data = venue.genres
  631.   form.address.data = venue.address
  632.   form.city.data = venue.city
  633.   form.state.data = venue.state
  634.   form.phone.data = venue.phone
  635.   form.website_link.data = venue.website_link
  636.   form.facebook_link.data = venue.facebook_link
  637.   form.seeking_talent.data = venue.seeking_talent
  638.   form.seeking_description.data = venue.seeking_description
  639.   form.image_link.data = venue.image_link  
  640.  
  641.   # venue={
  642.   #   "id": 1,
  643.   #   "name": "The Musical Hop",
  644.   #   "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
  645.   #   "address": "1015 Folsom Street",
  646.   #   "city": "San Francisco",
  647.   #   "state": "CA",
  648.   #   "phone": "123-123-1234",
  649.   #   "website": "https://www.themusicalhop.com",
  650.   #   "facebook_link": "https://www.facebook.com/TheMusicalHop",
  651.   #   "seeking_talent": True,
  652.   #   "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
  653.   #   "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
  654.   # }
  655.   # TODO: populate form with values from venue with ID <venue_id>
  656.   return render_template('forms/edit_venue.html', form=form, venue=venue)
  657.  
  658. @app.route('/venues/<int:venue_id>/edit', methods=['POST'])
  659. def edit_venue_submission(venue_id):
  660.   # TODO: take values from the form submitted, and update existing
  661.   # venue record with ID <venue_id> using the new attributes
  662.  
  663.   form = VenueForm(request.form)
  664.   venue = Venue.query.get(venue_id)
  665.  
  666.   venue.name = form.name.data
  667.   #venue.genres = ','.join(form.genres.data)
  668.   venue.genres = form.genres.data
  669.   venue.address = form.address.data
  670.   venue.city = form.city.data
  671.   venue.state = form.state.data
  672.   venue.phone = form.phone.data
  673.   venue.website_link = form.website_link.data
  674.   venue.facebook_link = form.facebook_link.data
  675.   venue.seeking_talent =  form.seeking_talent.data
  676.   venue.seeking_description = form.seeking_description.data
  677.   venue.image_link = form.image_link.data
  678.  
  679.   venue={
  680.     "id": venue_id,
  681.     "name": venue.name,
  682.     "genres": venue.genres,
  683.     "address": venue.address,
  684.     "city": venue.city,
  685.     "state": venue.state,
  686.     "phone": venue.phone,
  687.     "website": venue.website_link,
  688.     "facebook_link": venue.facebook_link,
  689.     "seeking_talent": venue.seeking_talent,
  690.     "seeking_description": venue.seeking_description,
  691.     "image_link": venue.image_link
  692.   }
  693.  
  694.   try:
  695.     db.session.commit()
  696.     flash(f'Venue {venue_id} updated successfully!!!')
  697.  
  698.   except:
  699.     db.session.rollback()
  700.     flash(f'An error occured, venue {venue_id} not updated!!!')
  701.  
  702.   finally:
  703.     db.session.close()
  704.  
  705.   return redirect(url_for('show_venue', venue_id=venue_id))
  706.  
  707. #  Create Artist
  708. #  ----------------------------------------------------------------
  709.  
  710. @app.route('/artists/create', methods=['GET'])
  711. def create_artist_form():
  712.   form = ArtistForm()
  713.   return render_template('forms/new_artist.html', form=form)
  714.  
  715. @app.route('/artists/create', methods=['POST'])
  716. def create_artist_submission():
  717.   # called upon submitting the new artist listing form
  718.   # TODO: insert form data as a new Venue record in the db, instead
  719.   # TODO: modify data to be the data object returned from db insertion
  720.  
  721.   form = ArtistForm(request.form)
  722.  
  723.   if form.validate():
  724.  
  725.     data = Artist(
  726.       name = form.name.data,
  727.       city = form.city.data,
  728.       state = form.state.data,
  729.       phone = form.phone.data,
  730.       #genres = ','.join(form.genres.data),
  731.       genres = form.genres.data,
  732.       image_link = form.image_link.data,
  733.       facebook_link = form.facebook_link.data,
  734.       website_link = form.website_link.data,
  735.       seeking_venue = form.seeking_venue.data,
  736.       seeking_description = form.seeking_description.data)
  737.  
  738.     try:
  739.       db.session.add(data)
  740.       db.session.commit()
  741.  
  742.       # on successful db insert, flash success
  743.       flash('Artist ' + request.form['name'] + ' was successfully listed!')
  744.       # TODO: on unsuccessful db insert, flash an error instead.
  745.       # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.')
  746.     except:
  747.       db.session.rollback()
  748.       flash('An error occurred. Artist ' + data.name + ' could not be listed.')
  749.    
  750.     finally:
  751.       db.session.close()
  752.  
  753.   else:
  754.     for field, message in form.errors.items():
  755.       flash(f'An error occured({message}). Kindly check the following field: {field}.')
  756.  
  757.   return render_template('pages/home.html')
  758.  
  759.  
  760. #  Shows
  761. #  ----------------------------------------------------------------
  762.  
  763. @app.route('/shows')
  764. def shows():
  765.   # displays list of shows at /shows
  766.   # TODO: replace with real venues data.
  767.  
  768.   data = []
  769.  
  770.   shows_query = Show.query.all()
  771.  
  772.   for show in shows_query:
  773.     data.append({
  774.       "venue_id": show.venue_id,
  775.       "venue_name": Venue.query.get(show.venue_id).name,
  776.       "artist_id": show.artist_id,
  777.       "artist_name": Artist.query.get(show.artist_id).name,
  778.       "artist_image_link": Artist.query.get(show.artist_id).image_link,
  779.       "start_time": str(show.start_time)
  780.     })
  781.  
  782.   # data = db.session.query(Show, Artist, Venue)\
  783.   #   .join(Artist, Artist.id == Show.artist_id)\
  784.   #     .join(Venue, Venue.id == Show.venue_id).all()
  785.  
  786.   # data = db.session.query(Show).join(Artist).join(Venue).all()
  787.  
  788.   # data=[{
  789.   #   "venue_id": 1,
  790.   #   "venue_name": "The Musical Hop",
  791.   #   "artist_id": 4,
  792.   #   "artist_name": "Guns N Petals",
  793.   #   "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",
  794.   #   "start_time": "2019-05-21T21:30:00.000Z"
  795.   # }, {
  796.   #   "venue_id": 3,
  797.   #   "venue_name": "Park Square Live Music & Coffee",
  798.   #   "artist_id": 5,
  799.   #   "artist_name": "Matt Quevedo",
  800.   #   "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",
  801.   #   "start_time": "2019-06-15T23:00:00.000Z"
  802.   # }, {
  803.   #   "venue_id": 3,
  804.   #   "venue_name": "Park Square Live Music & Coffee",
  805.   #   "artist_id": 6,
  806.   #   "artist_name": "The Wild Sax Band",
  807.   #   "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",
  808.   #   "start_time": "2035-04-01T20:00:00.000Z"
  809.   # }, {
  810.   #   "venue_id": 3,
  811.   #   "venue_name": "Park Square Live Music & Coffee",
  812.   #   "artist_id": 6,
  813.   #   "artist_name": "The Wild Sax Band",
  814.   #   "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",
  815.   #   "start_time": "2035-04-08T20:00:00.000Z"
  816.   # }, {
  817.   #   "venue_id": 3,
  818.   #   "venue_name": "Park Square Live Music & Coffee",
  819.   #   "artist_id": 6,
  820.   #   "artist_name": "The Wild Sax Band",
  821.   #   "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",
  822.   #   "start_time": "2035-04-15T20:00:00.000Z"
  823.   # }]
  824.   return render_template('pages/shows.html', shows=data)
  825.  
  826. @app.route('/shows/create')
  827. def create_shows():
  828.   # renders form. do not touch.
  829.   form = ShowForm()
  830.   return render_template('forms/new_show.html', form=form)
  831.  
  832. @app.route('/shows/create', methods=['POST'])
  833. def create_show_submission():
  834.   # called to create new shows in the db, upon submitting new show listing form
  835.   # TODO: insert form data as a new Show record in the db, instead
  836.  
  837.   form = ShowForm(request.form)
  838.  
  839.   data = Show(  
  840.   venue_id = form.venue_id.data,
  841.   artist_id = form.artist_id.data,
  842.   start_time = str(form.start_time.data))
  843.  
  844.   # on successful db insert, flash success
  845.   try:
  846.     db.session.add(data)
  847.     db.session.commit()
  848.     flash('Show was successfully listed!')
  849.     # TODO: on unsuccessful db insert, flash an error instead.
  850.     # e.g., flash('An error occurred. Show could not be listed.')
  851.     # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
  852.   except:
  853.     db.session.rollback()
  854.     flash('An error occurred. Show could not be listed.')
  855.  
  856.   finally:
  857.     db.session.close()
  858.  
  859.  
  860.   return render_template('pages/home.html')
  861.  
  862. @app.errorhandler(404)
  863. def not_found_error(error):
  864.     return render_template('errors/404.html'), 404
  865.  
  866. @app.errorhandler(500)
  867. def server_error(error):
  868.     return render_template('errors/500.html'), 500
  869.  
  870.  
  871. if not app.debug:
  872.     file_handler = FileHandler('error.log')
  873.     file_handler.setFormatter(
  874.         Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
  875.     )
  876.     app.logger.setLevel(logging.INFO)
  877.     file_handler.setLevel(logging.INFO)
  878.     app.logger.addHandler(file_handler)
  879.     app.logger.info('errors')
  880.  
  881. #----------------------------------------------------------------------------#
  882. # Launch.
  883. #----------------------------------------------------------------------------#
  884.  
  885. # Default port:
  886. if __name__ == '__main__':
  887.     app.run()
  888.  
  889. # Or specify port manually:
  890. '''
  891. if __name__ == '__main__':
  892.    port = int(os.environ.get('PORT', 5000))
  893.    app.run(host='0.0.0.0', port=port)
  894. '''
  895.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement