Advertisement
brandonmunda1

brandonFyyurApp

Aug 13th, 2022 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 31.48 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.       image_link = form.image_link.data,
  300.       facebook_link = form.facebook_link.data,
  301.       website_link = form.website_link.data,
  302.       seeking_talent = form.seeking_talent.data,
  303.       seeking_description = form.seeking_description.data)
  304.  
  305.     try:
  306.       db.session.add(data)
  307.       db.session.commit()
  308.  
  309.       # on successful db insert, flash success
  310.       flash('Venue ' + request.form['name'] + ' was successfully listed!')
  311.     # TODO: on unsuccessful db insert, flash an error instead.
  312.  
  313.     except:
  314.       db.session.rollback()
  315.       db.session.commit()
  316.       flash('An error occurred. Venue ' + data.name + ' could not be listed.')
  317.  
  318.     finally:
  319.       db.session.close()
  320.  
  321.   else:
  322.     for field, message in form.errors.items():
  323.       flash(f'An error occured({message}). Kindly check the following field: {field}.')
  324.  
  325.  
  326.  
  327.  
  328.   # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
  329.   # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
  330.   return render_template('pages/home.html')
  331.  
  332. @app.route('/venues/<venue_id>', methods=['DELETE'])
  333. def delete_venue(venue_id):
  334.   venue_to_delete = Venue.query.get(venue_id)
  335.  
  336.   try:
  337.     db.session.delete(venue_to_delete)
  338.     db.session.commit()
  339.     flash(f'Venue {venue_id} deleted!!!')
  340.  
  341.   except:
  342.     db.session.rollback()    
  343.     flash(f'An error occured. Venue {venue_id} not deleted!!!')
  344.  
  345.   finally:
  346.     db.session.close()
  347.   # TODO: Complete this endpoint for taking a venue_id, and using
  348.   # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
  349.  
  350.   # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
  351.   # clicking that button delete it from the db then redirect the user to the homepage
  352.   return None
  353.  
  354. #  Artists
  355. #  ----------------------------------------------------------------
  356. @app.route('/artists')
  357. def artists():
  358.   # TODO: replace with real data returned from querying the database
  359.  
  360.   data = []
  361.  
  362.   artists_query = Artist.query.all()
  363.  
  364.   for artist in artists_query:
  365.     data.append({
  366.       "id": artist.id,
  367.       "name": artist.name
  368.     })
  369.  
  370.   # data=[{
  371.   #   "id": 4,
  372.   #   "name": "Guns N Petals",
  373.   # }, {
  374.   #   "id": 5,
  375.   #   "name": "Matt Quevedo",
  376.   # }, {
  377.   #   "id": 6,
  378.   #   "name": "The Wild Sax Band",
  379.   # }]
  380.   return render_template('pages/artists.html', artists=data)
  381.  
  382. @app.route('/artists/search', methods=['POST'])
  383. def search_artists():
  384.   # TODO: implement search on artists with partial string search. Ensure it is case-insensitive.
  385.   # seach for "A" should return "Guns N Petals", "Matt Quevado", and "The Wild Sax Band".
  386.   # search for "band" should return "The Wild Sax Band".
  387.   search_term=request.form.get('search_term', '')
  388.   search = "%{}%".format(search_term)
  389.   response = {
  390.     "count": Artist.query.filter(Artist.name.ilike(search)).count(),
  391.     "data": []
  392.   }
  393.  
  394.   search_results = Artist.query.filter(Artist.name.ilike(search)).all()
  395.  
  396.   for result in search_results:
  397.     response["data"].append({
  398.       "id": result.id,
  399.       "name": result.name,
  400.       "num_upcoming_shows": Show.query.join(Artist).filter(Show.artist_id==result.id).filter(Show.start_time>datetime.now()).count()
  401.     })
  402.  
  403.   # response={
  404.   #   "count": 1,
  405.   #   "data": [{
  406.   #     "id": 4,
  407.   #     "name": "Guns N Petals",
  408.   #     "num_upcoming_shows": 0,
  409.   #   }]
  410.   # }
  411.   return render_template('pages/search_artists.html', results=response, search_term=request.form.get('search_term', ''))
  412.  
  413. @app.route('/artists/<int:artist_id>')
  414. def show_artist(artist_id):
  415.   # shows the artist page with the given artist_id
  416.   # TODO: replace with real artist data from the artist table, using artist_id
  417.  
  418.  
  419.   past_shows_query = Show.query.join(Artist).filter(Show.artist_id==artist_id).filter(datetime.utcnow()>Show.start_time).all()
  420.   past_shows = []
  421.   for show in past_shows_query:
  422.     past_shows.append({
  423.       "venue_id": show.venue_id,
  424.       "venue_name": Venue.query.get(show.venue_id).name,
  425.       "venue_image_link": Venue.query.get(show.venue_id).image_link,
  426.       "start_time": show.start_time
  427.  
  428.     })
  429.  
  430.   upcoming_shows_query = Show.query.join(Artist).filter(Show.artist_id==artist_id).filter(datetime.utcnow()<Show.start_time).all()
  431.   upcoming_shows = []
  432.   for show in upcoming_shows_query:
  433.     upcoming_shows.append({
  434.       "venue_id": show.venue_id,
  435.       "venue_name": Venue.query.get(show.venue_id).name,
  436.       "venue_image_link": Venue.query.get(show.venue_id).image_link,
  437.       "start_time": show.start_time
  438.  
  439.     })
  440.  
  441.   past_shows_count = len(past_shows)
  442.   upcoming_shows_count = len(upcoming_shows)
  443.  
  444.   artist_information = Artist.query.get(artist_id)
  445.  
  446.   data = {
  447.     "id": artist_information.id,
  448.     "name": artist_information.name,
  449.     "genres": artist_information.genres,
  450.     "city": artist_information.city,
  451.     "state": artist_information.state,
  452.     "phone": artist_information.phone,
  453.     "website": artist_information.website_link,
  454.     "facebook_link": artist_information.facebook_link,
  455.     "seeking_venue": artist_information.seeking_venue,
  456.     "seeking_description":artist_information.seeking_description,
  457.     "image_link": artist_information.image_link,
  458.     "past_shows": past_shows,
  459.     "upcoming_shows": upcoming_shows,
  460.     "past_shows_count": past_shows_count,
  461.     "upcoming_shows_count": upcoming_shows_count
  462.   }
  463.  
  464.  
  465.  
  466.  
  467.   # data1={
  468.   #   "id": 4,
  469.   #   "name": "Guns N Petals",
  470.   #   "genres": ["Rock n Roll"],
  471.   #   "city": "San Francisco",
  472.   #   "state": "CA",
  473.   #   "phone": "326-123-5000",
  474.   #   "website": "https://www.gunsnpetalsband.com",
  475.   #   "facebook_link": "https://www.facebook.com/GunsNPetals",
  476.   #   "seeking_venue": True,
  477.   #   "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
  478.   #   "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
  479.   #   "past_shows": [{
  480.   #     "venue_id": 1,
  481.   #     "venue_name": "The Musical Hop",
  482.   #     "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",
  483.   #     "start_time": "2019-05-21T21:30:00.000Z"
  484.   #   }],
  485.   #   "upcoming_shows": [],
  486.   #   "past_shows_count": 1,
  487.   #   "upcoming_shows_count": 0,
  488.   # }
  489.   # data2={
  490.   #   "id": 5,
  491.   #   "name": "Matt Quevedo",
  492.   #   "genres": ["Jazz"],
  493.   #   "city": "New York",
  494.   #   "state": "NY",
  495.   #   "phone": "300-400-5000",
  496.   #   "facebook_link": "https://www.facebook.com/mattquevedo923251523",
  497.   #   "seeking_venue": False,
  498.   #   "image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
  499.   #   "past_shows": [{
  500.   #     "venue_id": 3,
  501.   #     "venue_name": "Park Square Live Music & Coffee",
  502.   #     "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",
  503.   #     "start_time": "2019-06-15T23:00:00.000Z"
  504.   #   }],
  505.   #   "upcoming_shows": [],
  506.   #   "past_shows_count": 1,
  507.   #   "upcoming_shows_count": 0,
  508.   # }
  509.   # data3={
  510.   #   "id": 6,
  511.   #   "name": "The Wild Sax Band",
  512.   #   "genres": ["Jazz", "Classical"],
  513.   #   "city": "San Francisco",
  514.   #   "state": "CA",
  515.   #   "phone": "432-325-5432",
  516.   #   "seeking_venue": False,
  517.   #   "image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
  518.   #   "past_shows": [],
  519.   #   "upcoming_shows": [{
  520.   #     "venue_id": 3,
  521.   #     "venue_name": "Park Square Live Music & Coffee",
  522.   #     "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",
  523.   #     "start_time": "2035-04-01T20:00:00.000Z"
  524.   #   }, {
  525.   #     "venue_id": 3,
  526.   #     "venue_name": "Park Square Live Music & Coffee",
  527.   #     "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",
  528.   #     "start_time": "2035-04-08T20:00:00.000Z"
  529.   #   }, {
  530.   #     "venue_id": 3,
  531.   #     "venue_name": "Park Square Live Music & Coffee",
  532.   #     "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",
  533.   #     "start_time": "2035-04-15T20:00:00.000Z"
  534.   #   }],
  535.   #   "past_shows_count": 0,
  536.   #   "upcoming_shows_count": 3,
  537.   # }
  538.   # data = list(filter(lambda d: d['id'] == artist_id, [data1, data2, data3]))[0]
  539.   return render_template('pages/show_artist.html', artist=data)
  540.  
  541. #  Update
  542. #  ----------------------------------------------------------------
  543. @app.route('/artists/<int:artist_id>/edit', methods=['GET'])
  544. def edit_artist(artist_id):
  545.   form = ArtistForm()
  546.   artist = Artist.query.get_or_404(artist_id)
  547.  
  548.   form.name.data = artist.name
  549.   form.genres.data = artist.genres
  550.   form.city.data = artist.city
  551.   form.state.data = artist.state
  552.   form.phone.data = artist.phone
  553.   form.website_link.data = artist.website_link
  554.   form.facebook_link.data = artist.facebook_link
  555.   form.seeking_venue.data = artist.seeking_venue
  556.   form.seeking_description.data = artist.seeking_description
  557.   form.image_link.data = artist.image_link
  558.  
  559.  
  560.   # artist={
  561.   #   "id": 4,
  562.   #   "name": "Guns N Petals",
  563.   #   "genres": ["Rock n Roll"],
  564.   #   "city": "San Francisco",
  565.   #   "state": "CA",
  566.   #   "phone": "326-123-5000",
  567.   #   "website": "https://www.gunsnpetalsband.com",
  568.   #   "facebook_link": "https://www.facebook.com/GunsNPetals",
  569.   #   "seeking_venue": True,
  570.   #   "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
  571.   #   "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80"
  572.   # }
  573.   # TODO: populate form with fields from artist with ID <artist_id>
  574.   return render_template('forms/edit_artist.html', form=form, artist=artist)
  575.  
  576. @app.route('/artists/<int:artist_id>/edit', methods=['POST'])
  577. def edit_artist_submission(artist_id):
  578.   # TODO: take values from the form submitted, and update existing
  579.   # artist record with ID <artist_id> using the new attributes
  580.   form = ArtistForm(request.form)
  581.   artist = Artist.query.get_or_404(artist_id)
  582.  
  583.   artist.name = form.name.data
  584.   artist.genres = ",".join(form.genres.data)
  585.   artist.city = form.city.data
  586.   artist.state = form.state.data
  587.   artist.phone = form.phone.data
  588.   artist.website_link = form.website_link.data
  589.   artist.facebook_link = form.facebook_link.data
  590.   artist.seeking_venue =  form.seeking_venue.data
  591.   artist.seeking_description = form.seeking_description.data
  592.   artist.image_link = form.image_link.data
  593.  
  594.   artist={
  595.     "id": artist_id,
  596.     "name": artist.name,
  597.     "genres": artist.genres,
  598.     "city": artist.city,
  599.     "state": artist.state,
  600.     "phone": artist.phone,
  601.     "website": artist.website_link,
  602.     "facebook_link": artist.facebook_link,
  603.     "seeking_venue": artist.seeking_venue,
  604.     "seeking_description": artist.seeking_description,
  605.     "image_link": artist.image_link
  606.   }
  607.  
  608.   try:
  609.     db.session.commit()
  610.     flash(f'Artist {artist_id} updated successfully!!!')
  611.  
  612.   except:
  613.     db.session.rollback()
  614.     flash(f'An error occured, artist {artist_id} not updated!!!')
  615.  
  616.   finally:
  617.     db.session.close()
  618.  
  619.   return redirect(url_for('show_artist', artist_id=artist_id))
  620.  
  621. @app.route('/venues/<int:venue_id>/edit', methods=['GET'])
  622. def edit_venue(venue_id):
  623.   form = VenueForm()
  624.   venue = Venue.query.get(venue_id)
  625.  
  626.   form.name.data = venue.name
  627.   form.genres.data = venue.genres
  628.   form.address.data = venue.address
  629.   form.city.data = venue.city
  630.   form.state.data = venue.state
  631.   form.phone.data = venue.phone
  632.   form.website_link.data = venue.website_link
  633.   form.facebook_link.data = venue.facebook_link
  634.   form.seeking_talent.data = venue.seeking_talent
  635.   form.seeking_description.data = venue.seeking_description
  636.   form.image_link.data = venue.image_link  
  637.  
  638.   # venue={
  639.   #   "id": 1,
  640.   #   "name": "The Musical Hop",
  641.   #   "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
  642.   #   "address": "1015 Folsom Street",
  643.   #   "city": "San Francisco",
  644.   #   "state": "CA",
  645.   #   "phone": "123-123-1234",
  646.   #   "website": "https://www.themusicalhop.com",
  647.   #   "facebook_link": "https://www.facebook.com/TheMusicalHop",
  648.   #   "seeking_talent": True,
  649.   #   "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
  650.   #   "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
  651.   # }
  652.   # TODO: populate form with values from venue with ID <venue_id>
  653.   return render_template('forms/edit_venue.html', form=form, venue=venue)
  654.  
  655. @app.route('/venues/<int:venue_id>/edit', methods=['POST'])
  656. def edit_venue_submission(venue_id):
  657.   # TODO: take values from the form submitted, and update existing
  658.   # venue record with ID <venue_id> using the new attributes
  659.  
  660.   form = VenueForm(request.form)
  661.   venue = Venue.query.get(venue_id)
  662.  
  663.   venue.name = form.name.data
  664.   venue.genres = ','.join(form.genres.data)
  665.   venue.address = form.address.data
  666.   venue.city = form.city.data
  667.   venue.state = form.state.data
  668.   venue.phone = form.phone.data
  669.   venue.website_link = form.website_link.data
  670.   venue.facebook_link = form.facebook_link.data
  671.   venue.seeking_talent =  form.seeking_talent.data
  672.   venue.seeking_description = form.seeking_description.data
  673.   venue.image_link = form.image_link.data
  674.  
  675.   venue={
  676.     "id": venue_id,
  677.     "name": venue.name,
  678.     "genres": venue.genres,
  679.     "address": venue.address,
  680.     "city": venue.city,
  681.     "state": venue.state,
  682.     "phone": venue.phone,
  683.     "website": venue.website_link,
  684.     "facebook_link": venue.facebook_link,
  685.     "seeking_talent": venue.seeking_talent,
  686.     "seeking_description": venue.seeking_description,
  687.     "image_link": venue.image_link
  688.   }
  689.  
  690.   try:
  691.     db.session.commit()
  692.     flash(f'Venue {venue_id} updated successfully!!!')
  693.  
  694.   except:
  695.     db.session.rollback()
  696.     flash(f'An error occured, venue {venue_id} not updated!!!')
  697.  
  698.   finally:
  699.     db.session.close()
  700.  
  701.   return redirect(url_for('show_venue', venue_id=venue_id))
  702.  
  703. #  Create Artist
  704. #  ----------------------------------------------------------------
  705.  
  706. @app.route('/artists/create', methods=['GET'])
  707. def create_artist_form():
  708.   form = ArtistForm()
  709.   return render_template('forms/new_artist.html', form=form)
  710.  
  711. @app.route('/artists/create', methods=['POST'])
  712. def create_artist_submission():
  713.   # called upon submitting the new artist listing form
  714.   # TODO: insert form data as a new Venue record in the db, instead
  715.   # TODO: modify data to be the data object returned from db insertion
  716.  
  717.   form = ArtistForm(request.form)
  718.  
  719.   if form.validate():
  720.  
  721.     data = Artist(
  722.       name = form.name.data,
  723.       city = form.city.data,
  724.       state = form.state.data,
  725.       phone = form.phone.data,
  726.       genres = ','.join(form.genres.data),
  727.       image_link = form.image_link.data,
  728.       facebook_link = form.facebook_link.data,
  729.       website_link = form.website_link.data,
  730.       seeking_venue = form.seeking_venue.data,
  731.       seeking_description = form.seeking_description.data)
  732.  
  733.     try:
  734.       db.session.add(data)
  735.       db.session.commit()
  736.  
  737.       # on successful db insert, flash success
  738.       flash('Artist ' + request.form['name'] + ' was successfully listed!')
  739.       # TODO: on unsuccessful db insert, flash an error instead.
  740.       # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.')
  741.     except:
  742.       db.session.rollback()
  743.       flash('An error occurred. Artist ' + data.name + ' could not be listed.')
  744.    
  745.     finally:
  746.       db.session.close()
  747.  
  748.   else:
  749.     for field, message in form.errors.items():
  750.       flash(f'An error occured({message}). Kindly check the following field: {field}.')
  751.  
  752.   return render_template('pages/home.html')
  753.  
  754.  
  755. #  Shows
  756. #  ----------------------------------------------------------------
  757.  
  758. @app.route('/shows')
  759. def shows():
  760.   # displays list of shows at /shows
  761.   # TODO: replace with real venues data.
  762.  
  763.   data = []
  764.  
  765.   shows_query = Show.query.all()
  766.  
  767.   for show in shows_query:
  768.     data.append({
  769.       "venue_id": show.venue_id,
  770.       "venue_name": Venue.query.get(show.venue_id).name,
  771.       "artist_id": show.artist_id,
  772.       "artist_name": Artist.query.get(show.artist_id).name,
  773.       "artist_image_link": Artist.query.get(show.artist_id).image_link,
  774.       "start_time": str(show.start_time)
  775.     })
  776.  
  777.   # data = db.session.query(Show, Artist, Venue)\
  778.   #   .join(Artist, Artist.id == Show.artist_id)\
  779.   #     .join(Venue, Venue.id == Show.venue_id).all()
  780.  
  781.   # data = db.session.query(Show).join(Artist).join(Venue).all()
  782.  
  783.   # data=[{
  784.   #   "venue_id": 1,
  785.   #   "venue_name": "The Musical Hop",
  786.   #   "artist_id": 4,
  787.   #   "artist_name": "Guns N Petals",
  788.   #   "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",
  789.   #   "start_time": "2019-05-21T21:30:00.000Z"
  790.   # }, {
  791.   #   "venue_id": 3,
  792.   #   "venue_name": "Park Square Live Music & Coffee",
  793.   #   "artist_id": 5,
  794.   #   "artist_name": "Matt Quevedo",
  795.   #   "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",
  796.   #   "start_time": "2019-06-15T23:00:00.000Z"
  797.   # }, {
  798.   #   "venue_id": 3,
  799.   #   "venue_name": "Park Square Live Music & Coffee",
  800.   #   "artist_id": 6,
  801.   #   "artist_name": "The Wild Sax Band",
  802.   #   "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",
  803.   #   "start_time": "2035-04-01T20:00:00.000Z"
  804.   # }, {
  805.   #   "venue_id": 3,
  806.   #   "venue_name": "Park Square Live Music & Coffee",
  807.   #   "artist_id": 6,
  808.   #   "artist_name": "The Wild Sax Band",
  809.   #   "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",
  810.   #   "start_time": "2035-04-08T20:00:00.000Z"
  811.   # }, {
  812.   #   "venue_id": 3,
  813.   #   "venue_name": "Park Square Live Music & Coffee",
  814.   #   "artist_id": 6,
  815.   #   "artist_name": "The Wild Sax Band",
  816.   #   "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",
  817.   #   "start_time": "2035-04-15T20:00:00.000Z"
  818.   # }]
  819.   return render_template('pages/shows.html', shows=data)
  820.  
  821. @app.route('/shows/create')
  822. def create_shows():
  823.   # renders form. do not touch.
  824.   form = ShowForm()
  825.   return render_template('forms/new_show.html', form=form)
  826.  
  827. @app.route('/shows/create', methods=['POST'])
  828. def create_show_submission():
  829.   # called to create new shows in the db, upon submitting new show listing form
  830.   # TODO: insert form data as a new Show record in the db, instead
  831.  
  832.   form = ShowForm(request.form)
  833.  
  834.   data = Show(  
  835.   venue_id = form.venue_id.data,
  836.   artist_id = form.artist_id.data,
  837.   start_time = str(form.start_time.data))
  838.  
  839.   # on successful db insert, flash success
  840.   try:
  841.     db.session.add(data)
  842.     db.session.commit()
  843.     flash('Show was successfully listed!')
  844.     # TODO: on unsuccessful db insert, flash an error instead.
  845.     # e.g., flash('An error occurred. Show could not be listed.')
  846.     # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
  847.   except:
  848.     db.session.rollback()
  849.     flash('An error occurred. Show could not be listed.')
  850.  
  851.   finally:
  852.     db.session.close()
  853.  
  854.  
  855.   return render_template('pages/home.html')
  856.  
  857. @app.errorhandler(404)
  858. def not_found_error(error):
  859.     return render_template('errors/404.html'), 404
  860.  
  861. @app.errorhandler(500)
  862. def server_error(error):
  863.     return render_template('errors/500.html'), 500
  864.  
  865.  
  866. if not app.debug:
  867.     file_handler = FileHandler('error.log')
  868.     file_handler.setFormatter(
  869.         Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
  870.     )
  871.     app.logger.setLevel(logging.INFO)
  872.     file_handler.setLevel(logging.INFO)
  873.     app.logger.addHandler(file_handler)
  874.     app.logger.info('errors')
  875.  
  876. #----------------------------------------------------------------------------#
  877. # Launch.
  878. #----------------------------------------------------------------------------#
  879.  
  880. # Default port:
  881. if __name__ == '__main__':
  882.     app.run()
  883.  
  884. # Or specify port manually:
  885. '''
  886. if __name__ == '__main__':
  887.    port = int(os.environ.get('PORT', 5000))
  888.    app.run(host='0.0.0.0', port=port)
  889. '''
  890.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement