def create_show_submission(): form = ShowForm(request.form) # Initialize form instance with values from the request flashType = 'danger' # Initialize flashType to fail. Either it will be changed to "success" on successfully db insert, or in all other cases it should be equal to "fail" if form.validate(): try: newShow = Show.insert().values( Venue_id = request.form['venue_id'], Artist_id = request.form['artist_id'], start_time = request.form['start_time'] ) db.session.execute(newShow) db.session.commit() # on successful db insert, flash success flashType = 'success' flash('Show was successfully listed!') except : # TODO DONE: on unsuccessful db insert, flash an error instead. flash('An error occurred due to database insertion error. Show could not be listed.') finally: # Always close session db.session.close() else: flash(form.errors) # Flashes reason, why form is unsuccessful (not really pretty) flash('An error occurred due to form validation. Show could not be listed.') return render_template('pages/home.html', flashType = flashType)