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 # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.') form = ArtistForm(request.form, meta = {'csrf': False}) if form.validate(): try: artist = Artist() form.populate_obj(artist) db.session.add(artist) db.session.commit() flash('Artist ' + request.form['name'] + ' was successfully listed!') except ValueError as e: print(e) flash('Error in listing artist. 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_artist_submission(): # called upon submitting the new artist listing form # insert form data as a new Venue record in the db, instead # modify data to be the data object returned from db insertion form = ArtistForm(request.form, meta={"csrf": False}) if form.validate_on_submit(): try: artist: Artist = Artist() form.populate_obj(artist) db.session.add(artist) db.session.commit() # on successful db insert, flash success flash(f"Artist {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 edit_artist(artist_id): artist = Artist.query.get(artist_id) form = ArtistForm(obj=artist) if form.validate(): form.populate_obj(artist) # TODO: populate form with fields from artist with ID <artist_id> return render_template("forms/edit_artist.html", form=form, artist=artist)
def create_artist_submission(): artist = Artist() form = ArtistForm() if not form.validate_on_submit(): flash('Invalid value found in ' + ', '.join(form.errors.keys()) + ' field(s).') return render_template('forms/new_artist.html', form=form) else: error = False try: form.populate_obj(artist) artist.phone = format_phone(artist.phone) db.session.add(artist) db.session.flush() artist_id = artist.id db.session.commit() except: error = True db.session.rollback() finally: db.session.close() if error: flash('An error occurred. Artist ' + artist.name + ' could not be listed.') return redirect(url_for('index')) else: flash('Artist ' + request.form['name'] + ' was successfully listed!') return redirect(url_for('show_artist', artist_id=artist_id))
def edit_artist_submission(artist_id): artist = Artist.query.get(artist_id) if Artist is None: abort(404) name = Artist.name form = ArtistForm() if not form.validate_on_submit(): flash('Invalid value found in ' + ', '.join(form.errors.keys()) + ' field(s).') return render_template('forms/edit_artist.html', form=form, artist=artist) else: error = False try: edited = Artist() form.populate_obj(edited) for col in Artist.__table__.columns.keys(): if col != 'id': setattr(artist, col, getattr(edited, col)) name = artist.name artist.phone = format_phone(artist.phone) db.session.commit() except: error = True db.session.rollback() finally: db.session.close() if error: flash('An error occurred. Artist ' + name + ' could not be updated.') return redirect(url_for('index')) else: flash('Artist ' + name + ' was successfully updated!') return redirect(url_for('show_artist', artist_id=artist_id))
def edit_artist(artist_id): # it must be imported here to avoid circular import from forms import ArtistForm artist = Artist.query.get_or_404(artist_id) form = ArtistForm(obj=artist) artist_name = artist.name if form.validate_on_submit(): form.populate_obj(artist) try: db.session.add(artist) db.session.commit() except SQLAlchemyError: flash('An error occurred. Artist ' + artist_name + ' could not be edited.') print(sys.exc_info()) db.session.rollback() db.session.close() return render_template('forms/edit_artist.html', form=form, artist_name=artist_name) return redirect(url_for('show_artist', artist_id=artist_id)) return render_template('forms/edit_artist.html', form=form, artist_name=artist_name)
def edit_artist_submission(artist_id): form = ArtistForm(request.form) try: artist = Artist.query.get(artist_id) form.populate_obj(artist) db.session.commit() flash('Artist ' + request.form['name'] + ' was successfully edited!') except: db.session.rollback() flash('An error occurred. Venue ' + form['name'] + ' could not be edited.') finally: db.session.close() return redirect(url_for('show_artist', artist_id=artist_id))
def edit_artist_submission(artist_id): # take values from the form submitted, and update existing # artist record with ID <artist_id> using the new attributes form = ArtistForm(request.form) try: data = request.form artist = db.session.query(Artist).get(artist_id) form.populate_obj(artist) db.session.commit() except: db.session.rollback() finally: db.session.close() return redirect(url_for('show_artist', artist_id=artist_id))
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 # Same as we did with adding a new venue. form = ArtistForm(request.form) try: name = request.form['name'] city = request.form['city'] state = request.form['state'] phone = request.form['phone'] facebook_link = request.form['facebook_link'] genres = request.form.getlist('genres') website = request.form['website'] image_link = request.form['image_link'] add_artist = Artist(name=name, city=city, state=state, phone=phone, genres=genres, facebook_link=facebook_link, website=website, image_link=image_link) form.populate_obj(add_artist) db.session.add(add_artist) db.session.commit() flash('Artist ' + add_artist.name + ' was successfully listed!') except ValueError as e: print(e) # TODO DONE: on unsuccessful db insert, flash an error instead. flash('An error occurred due to database insertion error. Artist ' + add_artist.name + ' could not be listed.') finally: db.session.close() # TODO: on unsuccessful db insert, flash an error instead. # e.g., flash('An error occurred. Artist ' + data.name + ' could not be listed.') return render_template('pages/home.html')
def create_artist_submission(): error = False form = ArtistForm(request.form) try: artist = Artist() form.populate_obj(artist) db.session.add(artist) db.session.commit() flash('Artist ' + request.form['name'] + ' was successfully listed!') except ValueError as e: error = True print(e) flash('An error occurred. Artist ' + request.form['name'] + ' could not be listed.') db.session.rollback() finally: db.session.close() if error: abort(400) else: return render_template('pages/home.html')
def create_artist(): # it must be imported here to avoid circular import from forms import ArtistForm form = ArtistForm() if form.validate_on_submit(): artist = Artist() form.populate_obj(artist) try: db.session.add(artist) db.session.commit() except SQLAlchemyError: flash('An error occurred. Artist ' + form.name.data + ' could not be listed.') print(sys.exc_info()) db.session.rollback() db.session.close() return render_template('forms/new_artist.html', form=form) flash('Artist ' + artist.name + ' was successfully listed!') return redirect(url_for('show_artist', artist_id=artist.id)) return render_template('forms/new_artist.html', form=form)
def edit_artist_submission(artist_id): # check to make sure the artist exists # then update the artist by artist_id # error = False artist = Artist.query.get(artist_id) # get form form = ArtistForm(request.form) # check if artist exists # if artist is None: # abort(404) # flash("artist was not found") # validation + request type # populate artist w populate_obj form.populate_obj(artist) db.session.add(artist) db.session.commit() flash("Artist " + request.form["name"] + " was successfully updated!") return redirect(url_for("show_artist", artist_id=artist_id))
def edit_artist_submission(artist_id): error = False artist = Artist.query.get(artist_id) form = ArtistForm() try: if form.validate_on_submit(): form.populate_obj(artist) db.session.add(artist) db.session.commit() flash('Artist {} was successfully updated!'.format(form.name.data)) return redirect(url_for('show_artist', artist_id=artist_id)) flash('An error occurred. Artist {} could not be listed. {}'.format( form.name.data, form.errors)) except (): db.session.rollback() error = True print(sys.exc_info()) finally: db.session.close() if error: abort(500) flash('There was an error, please try again.') return redirect(url_for('show_artist', artist_id=artist_id))
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, meta = {'csrf': False}) if form.validate(): try: artist = Artist.query.get_or_404(artist_id) form.populate_obj(artist) db.session.add(artist) db.session.commit() flash('Update success!') except ValueError as e: print(e) flash('Error updating artist. 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('Field error: ' + str(err_msg)) return redirect(url_for('show_artist', artist_id=artist_id))
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 error = False form = ArtistForm() artist = Artist.query.get(artist_id) if artist is None: abort(404) try: if form.validate_on_submit(): form.populate_obj(artist) artist.genres = ','.join(form.genres.data) db.session.commit() else: error = True for fieldName, errorMessages in form.errors.items(): for err in errorMessages: print(err, file=sys.stdout) except: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash('Error!') return render_template('forms/edit_artist.html', form=form, artist=artist) else: flash('Success!') return redirect(url_for('show_artist', artist_id=artist_id))