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(): # called to create new shows in the db, upon submitting new show listing form # insert form data as a new Show record in the db, instead # see: http://flask.pocoo.org/docs/1.0/patterns/flashing/ form = ShowForm(request.form, meta={"csrf": False}) if form.validate_on_submit(): try: show: Show = Show() form.populate_obj(show) db.session.add(show) db.session.commit() # on successful db insert, flash success flash(f"Show {request.form['name']} was successfully listed!") except ValueError as e: print(sys.exc_info()) db.session.rollback() # on unsuccessful db insert, flash an error instead. flash(f"An error occurred: {str(e)}") finally: db.session.close() else: error_msg = [] for field, error in form.errors.items(): error_msg.append(f"{field}: {str(error)}") flash(f"Error occurred: {str(error_msg)}") return render_template('pages/home.html')
def create_show_submission(): form = ShowForm(request.form) try: show = Show() form.populate_obj(show) db.session.add(show) db.session.commit() flash('Show was successfully listed!') except: db.session.rollback() flash('An error occurred. Show could not be listed.') raise finally: db.session.close() return render_template('pages/home.html')
def create_show(): from forms import ShowForm form = ShowForm() if form.validate_on_submit(): show = Show() form.populate_obj(show) try: db.session.add(show) db.session.commit() except SQLAlchemyError: print(sys.exc_info()) db.session.rollback() db.session.close() flash('An error occurred. Show could not be listed.') return render_template('forms/new_show.html', form=form) flash('Show was successfully listed!') return render_template('pages/home.html') return render_template('forms/new_show.html', form=form)
def create_show_submission(): # called to create new shows in the db, upon submitting new show listing form # insert form data as a new Show record in the db, instead error = False form = ShowForm(request.form) try: show = Show() form.populate_obj(show) db.session.add(show) db.session.commit() # on successful db insert, flash success flash('Show was successfully listed!') except: error = True db.session.rollback() # on unsuccessful db insert, flash an error. flash('An error occurred. Show could not be listed.') finally: db.session.close() if error: abort(400) else: return render_template('pages/home.html')
def create_show_submission(): show = Show() form = ShowForm() if not form.validate_on_submit(): flash('Invalid value found in ' + ', '.join(form.errors.keys()) + ' field(s).') return render_template('forms/new_show.html', form=form) else: error = False try: form.populate_obj(show) db.session.add(show) db.session.flush() db.session.commit() except: error = True db.session.rollback() finally: db.session.close() if error: flash('An error occurred. Show could not be listed.') else: flash('Show was successfully listed!') return redirect(url_for('index'))