Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #----------------------------------------------------------------------------#
- # Imports
- #----------------------------------------------------------------------------#
- import json
- from urllib.request import Request
- import dateutil.parser
- import babel
- from flask import Flask, render_template, request, Response, flash, redirect, url_for
- from flask_moment import Moment
- from flask_sqlalchemy import SQLAlchemy
- import logging
- from logging import Formatter, FileHandler
- from flask_wtf import Form
- from forms import *
- from flask_migrate import Migrate # Imports the module required to for us to carry out our database migrations
- from models import *
- #----------------------------------------------------------------------------#
- # App Config.
- #----------------------------------------------------------------------------#
- app = Flask(__name__)
- moment = Moment(app)
- app.config.from_object('config')
- db.init_app(app)
- migrate = Migrate(app, db) # Allows for migrations to be carried out
- # TODO: connect to a local postgresql database
- #----------------------------------------------------------------------------#
- # Filters.
- #----------------------------------------------------------------------------#
- def format_datetime(value, format='medium'):
- if isinstance(value, str):
- date = dateutil.parser.parse(value)
- else:
- date = 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.
- # num_upcoming_shows should be aggregated based on number of upcoming shows per venue.
- data = []
- location_query = Venue.query.distinct(Venue.state, Venue.city).all() #Gets a list of all unique states and cities
- locations = {}
- for location in location_query:
- locations = {
- "city": location.city,
- "state": location.state,
- "venues": [] # we will store all the venues with the specific location details in this list
- }
- venues = Venue.query.filter_by(state=location.state, city=location.city).all()
- for venue in venues:
- locations['venues'].append({
- "id": venue.id,
- "name": venue.name,
- "num_upcoming_shows" : Show.query.join(Venue).filter(Show.venue_id==venue.id).filter(Show.start_time>datetime.now()).count()
- })
- data.append(locations)
- # data=[{
- # "city": "San Francisco",
- # "state": "CA",
- # "venues": [{
- # "id": 1,
- # "name": "The Musical Hop",
- # "num_upcoming_shows": 0,
- # }, {
- # "id": 3,
- # "name": "Park Square Live Music & Coffee",
- # "num_upcoming_shows": 1,
- # }]
- # }, {
- # "city": "New York",
- # "state": "NY",
- # "venues": [{
- # "id": 2,
- # "name": "The Dueling Pianos Bar",
- # "num_upcoming_shows": 0,
- # }]
- # }]
- return render_template('pages/venues.html', areas=data)
- @app.route('/venues/search', methods=['POST'])
- def search_venues():
- # TODO: implement search on venues with partial string search. Ensure it is case-insensitive.
- # seach for Hop should return "The Musical Hop".
- # search for "Music" should return "The Musical Hop" and "Park Square Live Music & Coffee"
- search_term=request.form.get('search_term', '')
- search = "%{}%".format(search_term)
- response = {
- "count": Venue.query.filter(Venue.name.ilike(search)).count(),
- "data": []
- }
- search_results = Venue.query.filter(Venue.name.ilike(search)).all()
- for result in search_results:
- response["data"].append({
- "id": result.id,
- "name": result.name,
- "num_upcoming_shows": Show.query.join(Venue).filter(Show.venue_id==result.id).filter(Show.start_time>datetime.now()).count()
- })
- # response={
- # "count": 1,
- # "data": [{
- # "id": 2,
- # "name": "The Dueling Pianos Bar",
- # "num_upcoming_shows": 0,
- # }]
- # }
- 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
- # TODO: replace with real venue data from the venues table, using venue_id
- past_shows_query = Show.query.join(Venue).filter(Show.venue_id==venue_id).filter(datetime.utcnow()>Show.start_time).all()
- past_shows = []
- for show in past_shows_query:
- past_shows.append({
- "artist_id": show.artist_id,
- "artist_name": Artist.query.get(show.artist_id).name,
- "artist_image_link": Artist.query.get(show.artist_id).image_link,
- "start_time": show.start_time
- })
- upcoming_shows_query = Show.query.join(Venue).filter(Show.venue_id==venue_id).filter(datetime.utcnow()<Show.start_time).all()
- upcoming_shows = []
- for show in upcoming_shows_query:
- upcoming_shows.append({
- "artist_id": show.artist_id,
- "artist_name": Artist.query.get(show.artist_id).name,
- "artist_image_link": Artist.query.get(show.artist_id).image_link,
- "start_time": show.start_time
- })
- past_shows_count = len(past_shows)
- upcoming_shows_count = len(upcoming_shows)
- venue_information = Venue.query.get(venue_id)
- data = {
- "id": venue_information.id,
- "name": venue_information.name,
- "genres": venue_information.genres,
- "address": venue_information.address,
- "city": venue_information.city,
- "state": venue_information.state,
- "phone": venue_information.phone,
- "website": venue_information.website_link,
- "facebook_link": venue_information.facebook_link,
- "seeking_talent": venue_information.seeking_talent,
- "seeking_description": venue_information.seeking_description,
- "image_link": venue_information.image_link,
- "past_shows": past_shows,
- "upcoming_shows": upcoming_shows,
- "past_shows_count": past_shows_count,
- "upcoming_shows_count": upcoming_shows_count
- }
- # data1={
- # "id": 1,
- # "name": "The Musical Hop",
- # "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
- # "address": "1015 Folsom Street",
- # "city": "San Francisco",
- # "state": "CA",
- # "phone": "123-123-1234",
- # "website": "https://www.themusicalhop.com",
- # "facebook_link": "https://www.facebook.com/TheMusicalHop",
- # "seeking_talent": True,
- # "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
- # "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60",
- # "past_shows": [{
- # "artist_id": 4,
- # "artist_name": "Guns N Petals",
- # "artist_image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
- # "start_time": "2019-05-21T21:30:00.000Z"
- # }],
- # "upcoming_shows": [],
- # "past_shows_count": 1,
- # "upcoming_shows_count": 0,
- # }
- # data2={
- # "id": 2,
- # "name": "The Dueling Pianos Bar",
- # "genres": ["Classical", "R&B", "Hip-Hop"],
- # "address": "335 Delancey Street",
- # "city": "New York",
- # "state": "NY",
- # "phone": "914-003-1132",
- # "website": "https://www.theduelingpianos.com",
- # "facebook_link": "https://www.facebook.com/theduelingpianos",
- # "seeking_talent": False,
- # "image_link": "https://images.unsplash.com/photo-1497032205916-ac775f0649ae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80",
- # "past_shows": [],
- # "upcoming_shows": [],
- # "past_shows_count": 0,
- # "upcoming_shows_count": 0,
- # }
- # data3={
- # "id": 3,
- # "name": "Park Square Live Music & Coffee",
- # "genres": ["Rock n Roll", "Jazz", "Classical", "Folk"],
- # "address": "34 Whiskey Moore Ave",
- # "city": "San Francisco",
- # "state": "CA",
- # "phone": "415-000-1234",
- # "website": "https://www.parksquarelivemusicandcoffee.com",
- # "facebook_link": "https://www.facebook.com/ParkSquareLiveMusicAndCoffee",
- # "seeking_talent": False,
- # "image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
- # "past_shows": [{
- # "artist_id": 5,
- # "artist_name": "Matt Quevedo",
- # "artist_image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
- # "start_time": "2019-06-15T23:00:00.000Z"
- # }],
- # "upcoming_shows": [{
- # "artist_id": 6,
- # "artist_name": "The Wild Sax Band",
- # "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "start_time": "2035-04-01T20:00:00.000Z"
- # }, {
- # "artist_id": 6,
- # "artist_name": "The Wild Sax Band",
- # "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "start_time": "2035-04-08T20:00:00.000Z"
- # }, {
- # "artist_id": 6,
- # "artist_name": "The Wild Sax Band",
- # "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "start_time": "2035-04-15T20:00:00.000Z"
- # }],
- # "past_shows_count": 1,
- # "upcoming_shows_count": 1,
- # }
- # data = list(filter(lambda d: d['id'] == venue_id, [data1, data2, data3]))[0]
- return render_template('pages/show_venue.html', venue=data)
- # Create Venue
- # ----------------------------------------------------------------
- @app.route('/venues/create', methods=['GET'])
- def create_venue_form():
- form = VenueForm()
- return render_template('forms/new_venue.html', form=form)
- @app.route('/venues/create', methods=['POST'])
- def create_venue_submission():
- # 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():
- data = Venue(
- name = form.name.data,
- city = form.city.data,
- state = form.state.data,
- address = form.address.data,
- phone = form.phone.data,
- genres = ', '.join(form.genres.data),
- image_link = form.image_link.data,
- facebook_link = form.facebook_link.data,
- website_link = form.website_link.data,
- seeking_talent = form.seeking_talent.data,
- seeking_description = form.seeking_description.data)
- try:
- db.session.add(data)
- db.session.commit()
- # on successful db insert, flash success
- flash('Venue ' + request.form['name'] + ' was successfully listed!')
- # TODO: on unsuccessful db insert, flash an error instead.
- except:
- db.session.rollback()
- db.session.commit()
- flash('An error occurred. Venue ' + data.name + ' could not be listed.')
- finally:
- db.session.close()
- else:
- for field, message in form.errors.items():
- flash(f'An error occured({message}). Kindly check the following field: {field}.')
- # e.g., flash('An error occurred. Venue ' + data.name + ' could not be listed.')
- # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
- return render_template('pages/home.html')
- @app.route('/venues/<venue_id>', methods=['DELETE'])
- def delete_venue(venue_id):
- venue_to_delete = Venue.query.get(venue_id)
- try:
- db.session.delete(venue_to_delete)
- db.session.commit()
- flash(f'Venue {venue_id} deleted!!!')
- except:
- db.session.rollback()
- flash(f'An error occured. Venue {venue_id} not deleted!!!')
- finally:
- db.session.close()
- # TODO: Complete this endpoint for taking a venue_id, and using
- # SQLAlchemy ORM to delete a record. Handle cases where the session commit could fail.
- # BONUS CHALLENGE: Implement a button to delete a Venue on a Venue Page, have it so that
- # clicking that button delete it from the db then redirect the user to the homepage
- return None
- # Artists
- # ----------------------------------------------------------------
- @app.route('/artists')
- def artists():
- # TODO: replace with real data returned from querying the database
- data = []
- artists_query = Artist.query.all()
- for artist in artists_query:
- data.append({
- "id": artist.id,
- "name": artist.name
- })
- # data=[{
- # "id": 4,
- # "name": "Guns N Petals",
- # }, {
- # "id": 5,
- # "name": "Matt Quevedo",
- # }, {
- # "id": 6,
- # "name": "The Wild Sax Band",
- # }]
- 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".
- search_term=request.form.get('search_term', '')
- search = "%{}%".format(search_term)
- response = {
- "count": Artist.query.filter(Artist.name.ilike(search)).count(),
- "data": []
- }
- search_results = Artist.query.filter(Artist.name.ilike(search)).all()
- for result in search_results:
- response["data"].append({
- "id": result.id,
- "name": result.name,
- "num_upcoming_shows": Show.query.join(Artist).filter(Show.artist_id==result.id).filter(Show.start_time>datetime.now()).count()
- })
- # response={
- # "count": 1,
- # "data": [{
- # "id": 4,
- # "name": "Guns N Petals",
- # "num_upcoming_shows": 0,
- # }]
- # }
- 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
- past_shows_query = Show.query.join(Artist).filter(Show.artist_id==artist_id).filter(datetime.utcnow()>Show.start_time).all()
- past_shows = []
- for show in past_shows_query:
- past_shows.append({
- "venue_id": show.venue_id,
- "venue_name": Venue.query.get(show.venue_id).name,
- "venue_image_link": Venue.query.get(show.venue_id).image_link,
- "start_time": show.start_time
- })
- upcoming_shows_query = Show.query.join(Artist).filter(Show.artist_id==artist_id).filter(datetime.utcnow()<Show.start_time).all()
- upcoming_shows = []
- for show in upcoming_shows_query:
- upcoming_shows.append({
- "venue_id": show.venue_id,
- "venue_name": Venue.query.get(show.venue_id).name,
- "venue_image_link": Venue.query.get(show.venue_id).image_link,
- "start_time": show.start_time
- })
- past_shows_count = len(past_shows)
- upcoming_shows_count = len(upcoming_shows)
- artist_information = Artist.query.get(artist_id)
- data = {
- "id": artist_information.id,
- "name": artist_information.name,
- "genres": artist_information.genres,
- "city": artist_information.city,
- "state": artist_information.state,
- "phone": artist_information.phone,
- "website": artist_information.website_link,
- "facebook_link": artist_information.facebook_link,
- "seeking_venue": artist_information.seeking_venue,
- "seeking_description":artist_information.seeking_description,
- "image_link": artist_information.image_link,
- "past_shows": past_shows,
- "upcoming_shows": upcoming_shows,
- "past_shows_count": past_shows_count,
- "upcoming_shows_count": upcoming_shows_count
- }
- # data1={
- # "id": 4,
- # "name": "Guns N Petals",
- # "genres": ["Rock n Roll"],
- # "city": "San Francisco",
- # "state": "CA",
- # "phone": "326-123-5000",
- # "website": "https://www.gunsnpetalsband.com",
- # "facebook_link": "https://www.facebook.com/GunsNPetals",
- # "seeking_venue": True,
- # "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
- # "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
- # "past_shows": [{
- # "venue_id": 1,
- # "venue_name": "The Musical Hop",
- # "venue_image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60",
- # "start_time": "2019-05-21T21:30:00.000Z"
- # }],
- # "upcoming_shows": [],
- # "past_shows_count": 1,
- # "upcoming_shows_count": 0,
- # }
- # data2={
- # "id": 5,
- # "name": "Matt Quevedo",
- # "genres": ["Jazz"],
- # "city": "New York",
- # "state": "NY",
- # "phone": "300-400-5000",
- # "facebook_link": "https://www.facebook.com/mattquevedo923251523",
- # "seeking_venue": False,
- # "image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
- # "past_shows": [{
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
- # "start_time": "2019-06-15T23:00:00.000Z"
- # }],
- # "upcoming_shows": [],
- # "past_shows_count": 1,
- # "upcoming_shows_count": 0,
- # }
- # data3={
- # "id": 6,
- # "name": "The Wild Sax Band",
- # "genres": ["Jazz", "Classical"],
- # "city": "San Francisco",
- # "state": "CA",
- # "phone": "432-325-5432",
- # "seeking_venue": False,
- # "image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "past_shows": [],
- # "upcoming_shows": [{
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
- # "start_time": "2035-04-01T20:00:00.000Z"
- # }, {
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
- # "start_time": "2035-04-08T20:00:00.000Z"
- # }, {
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "venue_image_link": "https://images.unsplash.com/photo-1485686531765-ba63b07845a7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=747&q=80",
- # "start_time": "2035-04-15T20:00:00.000Z"
- # }],
- # "past_shows_count": 0,
- # "upcoming_shows_count": 3,
- # }
- # data = list(filter(lambda d: d['id'] == artist_id, [data1, data2, data3]))[0]
- return render_template('pages/show_artist.html', artist=data)
- # Update
- # ----------------------------------------------------------------
- @app.route('/artists/<int:artist_id>/edit', methods=['GET'])
- def edit_artist(artist_id):
- form = ArtistForm()
- artist = Artist.query.get_or_404(artist_id)
- form.name.data = artist.name
- form.genres.data = artist.genres
- form.city.data = artist.city
- form.state.data = artist.state
- form.phone.data = artist.phone
- form.website_link.data = artist.website_link
- form.facebook_link.data = artist.facebook_link
- form.seeking_venue.data = artist.seeking_venue
- form.seeking_description.data = artist.seeking_description
- form.image_link.data = artist.image_link
- # artist={
- # "id": 4,
- # "name": "Guns N Petals",
- # "genres": ["Rock n Roll"],
- # "city": "San Francisco",
- # "state": "CA",
- # "phone": "326-123-5000",
- # "website": "https://www.gunsnpetalsband.com",
- # "facebook_link": "https://www.facebook.com/GunsNPetals",
- # "seeking_venue": True,
- # "seeking_description": "Looking for shows to perform at in the San Francisco Bay Area!",
- # "image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80"
- # }
- # TODO: populate form with fields from artist with ID <artist_id>
- return render_template('forms/edit_artist.html', form=form, artist=artist)
- @app.route('/artists/<int:artist_id>/edit', methods=['POST'])
- def edit_artist_submission(artist_id):
- # TODO: take values from the form submitted, and update existing
- # artist record with ID <artist_id> using the new attributes
- form = ArtistForm(request.form)
- artist = Artist.query.get_or_404(artist_id)
- artist.name = form.name.data
- artist.genres = ",".join(form.genres.data)
- artist.city = form.city.data
- artist.state = form.state.data
- artist.phone = form.phone.data
- artist.website_link = form.website_link.data
- artist.facebook_link = form.facebook_link.data
- artist.seeking_venue = form.seeking_venue.data
- artist.seeking_description = form.seeking_description.data
- artist.image_link = form.image_link.data
- artist={
- "id": artist_id,
- "name": artist.name,
- "genres": artist.genres,
- "city": artist.city,
- "state": artist.state,
- "phone": artist.phone,
- "website": artist.website_link,
- "facebook_link": artist.facebook_link,
- "seeking_venue": artist.seeking_venue,
- "seeking_description": artist.seeking_description,
- "image_link": artist.image_link
- }
- try:
- db.session.commit()
- flash(f'Artist {artist_id} updated successfully!!!')
- except:
- db.session.rollback()
- flash(f'An error occured, artist {artist_id} not updated!!!')
- finally:
- db.session.close()
- return redirect(url_for('show_artist', artist_id=artist_id))
- @app.route('/venues/<int:venue_id>/edit', methods=['GET'])
- def edit_venue(venue_id):
- form = VenueForm()
- venue = Venue.query.get(venue_id)
- form.name.data = venue.name
- form.genres.data = venue.genres
- form.address.data = venue.address
- form.city.data = venue.city
- form.state.data = venue.state
- form.phone.data = venue.phone
- form.website_link.data = venue.website_link
- form.facebook_link.data = venue.facebook_link
- form.seeking_talent.data = venue.seeking_talent
- form.seeking_description.data = venue.seeking_description
- form.image_link.data = venue.image_link
- # venue={
- # "id": 1,
- # "name": "The Musical Hop",
- # "genres": ["Jazz", "Reggae", "Swing", "Classical", "Folk"],
- # "address": "1015 Folsom Street",
- # "city": "San Francisco",
- # "state": "CA",
- # "phone": "123-123-1234",
- # "website": "https://www.themusicalhop.com",
- # "facebook_link": "https://www.facebook.com/TheMusicalHop",
- # "seeking_talent": True,
- # "seeking_description": "We are on the lookout for a local artist to play every two weeks. Please call us.",
- # "image_link": "https://images.unsplash.com/photo-1543900694-133f37abaaa5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"
- # }
- # TODO: populate form with values from venue with ID <venue_id>
- 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
- form = VenueForm(request.form)
- venue = Venue.query.get(venue_id)
- venue.name = form.name.data
- venue.genres = ','.join(form.genres.data)
- venue.address = form.address.data
- venue.city = form.city.data
- venue.state = form.state.data
- venue.phone = form.phone.data
- venue.website_link = form.website_link.data
- venue.facebook_link = form.facebook_link.data
- venue.seeking_talent = form.seeking_talent.data
- venue.seeking_description = form.seeking_description.data
- venue.image_link = form.image_link.data
- venue={
- "id": venue_id,
- "name": venue.name,
- "genres": venue.genres,
- "address": venue.address,
- "city": venue.city,
- "state": venue.state,
- "phone": venue.phone,
- "website": venue.website_link,
- "facebook_link": venue.facebook_link,
- "seeking_talent": venue.seeking_talent,
- "seeking_description": venue.seeking_description,
- "image_link": venue.image_link
- }
- try:
- db.session.commit()
- flash(f'Venue {venue_id} updated successfully!!!')
- except:
- db.session.rollback()
- flash(f'An error occured, venue {venue_id} not 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
- # TODO: modify data to be the data object returned from db insertion
- form = ArtistForm(request.form)
- if form.validate():
- data = Artist(
- name = form.name.data,
- city = form.city.data,
- state = form.state.data,
- phone = form.phone.data,
- genres = ','.join(form.genres.data),
- image_link = form.image_link.data,
- facebook_link = form.facebook_link.data,
- website_link = form.website_link.data,
- seeking_venue = form.seeking_venue.data,
- seeking_description = form.seeking_description.data)
- try:
- db.session.add(data)
- db.session.commit()
- # on successful db insert, flash success
- flash('Artist ' + request.form['name'] + ' was successfully listed!')
- # TODO: on unsuccessful db insert, flash an error instead.
- # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.')
- except:
- db.session.rollback()
- flash('An error occurred. Artist ' + data.name + ' could not be listed.')
- finally:
- db.session.close()
- else:
- for field, message in form.errors.items():
- flash(f'An error occured({message}). Kindly check the following field: {field}.')
- return render_template('pages/home.html')
- # Shows
- # ----------------------------------------------------------------
- @app.route('/shows')
- def shows():
- # displays list of shows at /shows
- # TODO: replace with real venues data.
- data = []
- shows_query = Show.query.all()
- for show in shows_query:
- data.append({
- "venue_id": show.venue_id,
- "venue_name": Venue.query.get(show.venue_id).name,
- "artist_id": show.artist_id,
- "artist_name": Artist.query.get(show.artist_id).name,
- "artist_image_link": Artist.query.get(show.artist_id).image_link,
- "start_time": str(show.start_time)
- })
- # data = db.session.query(Show, Artist, Venue)\
- # .join(Artist, Artist.id == Show.artist_id)\
- # .join(Venue, Venue.id == Show.venue_id).all()
- # data = db.session.query(Show).join(Artist).join(Venue).all()
- # data=[{
- # "venue_id": 1,
- # "venue_name": "The Musical Hop",
- # "artist_id": 4,
- # "artist_name": "Guns N Petals",
- # "artist_image_link": "https://images.unsplash.com/photo-1549213783-8284d0336c4f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=300&q=80",
- # "start_time": "2019-05-21T21:30:00.000Z"
- # }, {
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "artist_id": 5,
- # "artist_name": "Matt Quevedo",
- # "artist_image_link": "https://images.unsplash.com/photo-1495223153807-b916f75de8c5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80",
- # "start_time": "2019-06-15T23:00:00.000Z"
- # }, {
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "artist_id": 6,
- # "artist_name": "The Wild Sax Band",
- # "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "start_time": "2035-04-01T20:00:00.000Z"
- # }, {
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "artist_id": 6,
- # "artist_name": "The Wild Sax Band",
- # "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "start_time": "2035-04-08T20:00:00.000Z"
- # }, {
- # "venue_id": 3,
- # "venue_name": "Park Square Live Music & Coffee",
- # "artist_id": 6,
- # "artist_name": "The Wild Sax Band",
- # "artist_image_link": "https://images.unsplash.com/photo-1558369981-f9ca78462e61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=794&q=80",
- # "start_time": "2035-04-15T20:00:00.000Z"
- # }]
- 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
- form = ShowForm(request.form)
- data = Show(
- venue_id = form.venue_id.data,
- artist_id = form.artist_id.data,
- start_time = str(form.start_time.data))
- # on successful db insert, flash success
- try:
- db.session.add(data)
- db.session.commit()
- flash('Show was successfully listed!')
- # TODO: on unsuccessful db insert, flash an error instead.
- # e.g., flash('An error occurred. Show could not be listed.')
- # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/
- except:
- db.session.rollback()
- flash('An error occurred. Show could not be listed.')
- finally:
- db.session.close()
- 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)
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement