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) try: form.validate() show = Show( start_time=form.start_time.data, venue_id=form.venue_id.data, artist_id=form.artist_id.data, ) db.session.add(show) db.session.commit() # on successful db insert, flash success flash('Show was successfully listed!') except Exception as e: print(e) # 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/ db.session.rollback() flash('An error occurred. Show could not be listed.') finally: db.session.close() return render_template('pages/home.html')
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 # 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/ form = ShowForm(request.form, meta = {'csrf': False}) if form.validate(): try: show = Show() form.populate_obj(show) db.session.add(show) db.session.commit() flash('Show was successfully listed!') except ValueError as e: print(e) flash('Error in listing show. Please try again.') db.session.rollback() finally: db.session.close() else: err_msg = [] for field, err in form.errors.items(): err_msg.append(field + '-' + '|'.join(err)) flash('Errors: ' + str(err_msg)) return render_template('pages/home.html')
def create_show_submission(): # validate form form = ShowForm(request.form) if not form.validate(): return render_template('forms/new_show.html', form=form) # create show error = False try: show = Show() show.venue_id = request.form.get('venue_id', '') show.artist_id = request.form.get('artist_id', '') show.start_time = request.form.get('start_time', '') db.session.add(show) db.session.commit() except: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash('Show could not be listed.') else: flash('Show was successfully listed!') return render_template('pages/home.html')
def create_show_submission(): """Creates a new show in the db from a form submission. Returns: A redirect to the shows page """ form = ShowForm() if not form.validate(): flash( list(form.errors.values())[0][0], "error", ) return redirect(url_for("create_show_form")) error = False try: venue_id = request.form.get("venue_id") artist_id = request.form.get("artist_id") start_time = request.form.get("start_time") unavailabilities = Unavailability.query.filter_by( artist_id=artist_id).all() for unavailability in unavailabilities: if (str(unavailability.start_time) > start_time < str( unavailability.end_time)): flash("Artist is unavailable at selected time") return redirect(url_for("create_show_form")) show = Show(venue_id=venue_id, artist_id=artist_id, start_time=start_time) db.session.add(show) db.session.commit() except Exception: # pylint: disable=broad-except error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash("Show was unable to be listed!", "error") abort(500) flash("Show was successfully listed!") return redirect(url_for("shows"))
def create_show_submission(): """ Received POSTed form data for a new show and saves those details in the database Returns user to index.html """ # 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() if form.validate(): artist_id = form.artist_id.data venue_id = form.venue_id.data start_time = form.start_time.data # Isolate hour from desired show time start_time_only = str(start_time).split(' ')[1] start_time_hour = int(start_time_only[0] + start_time_only[1]) # Get start and end hours for artist availability artist = Artist.query.get(artist_id) available_from = 0 available_to = 23 available_hours = artist.available_hours if available_hours: available_from = int(available_hours.split('-')[0]) available_to = int(available_hours.split('-')[1]) # If show time falls in Artist available hours, or no available hours were specified for the artist, try to list the show if (start_time_hour >= available_from) and (start_time_hour <= available_to): try: show = Show(artist_id=artist_id, venue_id=venue_id, start_time=start_time) db.session.add(show) db.session.commit() flash('Show was successfully listed!') # 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 except Exception as e: db.session.rollback() flash('ERROR: Show not created!') finally: db.session.close() else: flash("Artist not avaiable at that time") # 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/ return redirect(url_for('index'))
def create_show_submission(): error_msg = '' try: f = request.form sf = ShowForm(f) if not sf.validate(): for key, val in sf.errors.items(): error_msg += key + ": " + ';'.join(val) + " \n" raise AssertionError # check accessibility at = Artist.query.get(int(f["artist_id"])) vn = Venue.query.get(int(f["venue_id"])) if at is None or vn is None: error_msg += " Invalid artist or venue ID! " raise AssertionError if not at.seeking_venue or not vn.seeking_talent: error_msg += " The artist or venue is not available. " raise AssertionError # create show sh = Show(artist_id=int(f["artist_id"]), venue_id=int(f["venue_id"]), start_time=dateutil.parser.parse(f["start_time"])) db.session.add(sh) db.session.commit() flash('The show is successfully added!') except AssertionError: db.session.rollback() print(sys.exc_info()) flash('The form is invalid.' + error_msg) except SQLAlchemyError: db.session.rollback() print(sys.exc_info()) flash('A database error occurred. This show could not be listed.') finally: db.session.close() return render_template('pages/home.html')
def create_show_submission(): """ Received POSTed form data for a new show and saves those details in the database Returns user to index.html """ # 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() if form.validate(): artist_id = form.artist_id.data venue_id = form.venue_id.data start_time = form.start_time.data try: show = Show(artist_id=artist_id, venue_id=venue_id, start_time=start_time) db.session.add(show) db.session.commit() flash('Show was successfully listed!') # 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 except Exception as e: db.session.rollback() flash('An error occurred. Show could not be listed.') finally: db.session.close() else: flash('ERROR: Artist not added, please check errors below:') #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/ return redirect(url_for('index'))