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) try: form.validate() artist = Artist.query.filter(Artist.id == artist_id).first() artist.name = form.name.data artist.city = form.city.data artist.state = form.state.data.name artist.phone = form.phone.data artist.image_link = form.image_link.data artist.facebook_link = form.facebook_link.data artist.genres = [genre.name for genre in form.genres.data] artist.website = form.website.data artist.seeking_venue = form.seeking_venue.data artist.seeking_description = form.seeking_description.data db.session.commit() except Exception as e: print(e) 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 form = ArtistForm(request.form) try: form.validate() artist = Artist( name=form.name.data, city=form.city.data, state=form.state.data.name, phone=form.phone.data, image_link=form.image_link.data, facebook_link=form.facebook_link.data, genres=[genre.name for genre in form.genres.data], website=form.website.data, seeking_venue=form.seeking_venue.data, seeking_description=form.seeking_description.data, ) db.session.add(artist) db.session.commit() # on successful db insert, flash success flash('Artist ' + form.name.data + ' 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 Exception as e: print(e) db.session.rollback() flash('An error occurred. Artist ' + form.name.data + ' could not be listed.') return render_template('pages/home.html')
def edit_artist_submission(artist_id): error_msg = '' try: f = request.form af = ArtistForm(f) if not af.validate(): for key, val in af.errors.items(): error_msg += key + ": " + ';'.join(val) assert af.validate() at = Artist.query.get(artist_id) at.name = f["name"] at.city = f["city"] at.state = f["state"] at.phone = f["phone"] at.facebook_link = f["facebook_link"] at.website = f["website"] at.image_link = f["image_link"] at.seeking_venue = bool('seeking_venue' in f) at.seeking_description = f['seeking_description'] g_ids = [int(i) for i in f.getlist('genres')] at.genres = db.session.query(Genre).filter(Genre.id.in_(g_ids)).all() db.session.commit() 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 artist could not be updated.') finally: db.session.close() return redirect(url_for('rt.show_artist', artist_id=artist_id))
def edit_artist(artist_id): at = Artist.query.get(artist_id) artist = { "id": at.id, "name": at.name, "city": at.city, "state": at.state, "phone": at.phone, "website": at.website, "facebook_link": at.facebook_link, "seeking_venue": at.seeking_venue, "seeking_description": at.seeking_description, "image_link": at.image_link, "genres": [g.id for g in at.genres] } form = ArtistForm(**artist) error_msg = '' form.validate() for key, val in form.errors.items(): if key != 'csrf_token': error_msg += key + ": " + ';'.join(val) if error_msg: flash( 'There are few errors in the old artist info. Please fix them first! ' + error_msg) return render_template('forms/edit_artist.html', form=form, artist=artist)
def editArtist(artist_id): # Checking if user is logged in if 'email' not in login_session: return redirect('/login') editedArtist = db.session.query(Artist).filter_by(id=artist_id).one() # Checking if artist belongs to logged in user if editedArtist.user_id != login_session['user_id']: return """<script>function myFunction() {alert('You are not authorized to edit this artist. Please create your own artist in order to edit.');} </script><body onload='myFunction()'>""" # Using the same form as artist creation for artist editing form = ArtistForm(request.form, editedArtist) if request.method == 'POST' and form.validate(): editedArtist.name = form.name.data editedArtist.year_of_birth = form.year_of_birth.data editedArtist.year_of_death = form.year_of_death.data editedArtist.country = form.country.data editedArtist.movement = form.art_movement.data db.session.add(editedArtist) db.session.commit() return redirect(url_for('showArtists')) else: return render_template('editArtist.html', form=form, artist=editedArtist)
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(): # validate form form = ArtistForm(request.form) if not form.validate(): return render_template('forms/new_artist.html', form=form) # create artist error = False try: artist = Artist() artist.set_data(form_data=request.form) db.session.add(artist) db.session.commit() except: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash('An error occurred. Artist ' + request.form.get('name', '') + ' could not be listed.') else: flash('Artist ' + request.form.get('name', '') + ' was successfully listed!') return render_template('pages/home.html')
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(): error = False form = ArtistForm() # Form validation if not form.validate(): for fieldName, errorMessages in form.errors.items(): show_form_errors(fieldName, errorMessages) return redirect(url_for('create_artist_form')) # Get data name = request.form['name'] city = request.form['city'] state = request.form['state'] phone = request.form['phone'] genres = request.form.getlist('genres') image_link = request.form['image_link'] facebook_link = request.form['facebook_link'] website = request.form['website'] seeking_venue = True if 'seeking_venue' in request.form else False seeking_description = request.form['seeking_description'] try: # Create model artist = Artist( name=name, city=city, state=state, phone=phone, genres=genres, image_link=image_link, facebook_link=facebook_link, website=website, seeking_venue=seeking_venue, seeking_description=seeking_description, ) # Update DB db.session.add(artist) db.session.commit() except Exception: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() # Show banner if error: abort(400) flash('An error occurred. Artist ' + name + ' could not be listed.', 'danger') if not error: flash('Artist ' + name + ' was successfully listed!', 'success') return render_template('pages/home.html')
def create_artist_submission(): form = ArtistForm(request.form) if form.validate(): try: seeking_venue = False name = request.form['name'] city = request.form['city'] state = request.form['state'] phone = request.form['phone'] genres = request.form['genres'] facebook_link = request.form['facebook_link'] image_link = request.form['image_link'] website = request.form['website'] seeking_venue = request.form['seeking_venue'] seeking_description = request.form['seeking_description'] artist = Artist(name=name, city=city, state=state, phone=phone, genres=genres, facebook_link=facebook_link, image_link=image_link, website=website, seeking_venue=seeking_venue, seeking_description=seeking_description) db.session.add(artist) db.session.commit() flash('Artist ' + request.form['name'] + ' was successfully listed!') except ValueError as e: flash('Artist ' + request.form['name'] + ' was not successfully listed!') db.session.rollback() print(e) finally: db.session.close() else: #get eror message message = [] for field, err in form.errors.items(): message.append(field + '' + '|'.join(err)) flash('Errors ' + str(message)) return render_template('pages/home.html')
def edit_artist_submission(artist_id): # LASTXX2: take values from the form submitted, and update existing # artist record with ID <artist_id> using the new attributes form = ArtistForm() name = form.name.data city = form.city.data state = form.state.data phone = form.phone.data genres = form.genres.data facebook_link = form.facebook_link.data image_link = form.image_link.data web_site = form.web_site.data seeking_venue = True if form.seeking_venue.data == 'Yes' else False seeking_description = form.seeking_description.data if not form.validate(): flash(form.errors) return redirect(url_for('edit_artist_submission', artist_id=artist_id)) else: error_in_update = False try: artist = Artist.query.get(artist_id) artist.name = name artist.city = city artist.state = state artist.phone = phone artist.facebook_link = facebook_link artist.image_link = image_link artist.web_site = web_site artist.seeking_venue = seeking_venue artist.seeking_description = seeking_description artist.genres = [] for genre in genres: artist.genres.append(genre) db.session.commit() except Exception: error_in_update = True print(f'Exception "{Exception}" in edit_artist_submission()') db.session.rollback() finally: db.session.close() if not error_in_update: # on successful db update, flash success flash('Artist ' + request.form['name'] + ' was successfully updated!') return redirect(url_for('show_artist', artist_id=artist_id)) else: flash('An error occurred. Artist ' + name + ' could not be updated.') print("Error in edit_artist_submission()") return render_template('errors/500.html')
def create_artist_submission(): # called upon submitting the new artist listing form success = True error_msg = '' try: f = request.form af = ArtistForm(request.form) if not af.validate(): for key, val in af.errors.items(): error_msg += key + ": " + ';'.join(val) assert af.validate() g_ids = [int(i) for i in f.getlist('genres')] at = Artist( name=f["name"], city=f['city'], state=f['state'], phone=f['phone'], facebook_link=f['facebook_link'], genres=db.session.query(Genre).filter(Genre.id.in_(g_ids)).all(), image_link=f['image_link'], website=f['website'], seeking_venue=('seeking_venue' in f), seeking_description=f['seeking_description'], ) db.session.add(at) db.session.commit() flash('Artist ' + request.form['name'] + ' was successfully listed!') 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. Artist ' + request.form['name'] + ' could not be listed.') finally: db.session.close() return render_template('pages/home.html')
def submit_view(): form = ArtistForm() if request.method == 'POST' and form.validate(): result = predict_cluster(form.artist.data, mode) if result == None: form.artist.errors.append('The submitted artist is not available.') return render_template("index.html", form=form) with open(os.path.join(APP_STATIC, 'data/prediction_data.pkl'), 'wb') as f: pickle.dump(result, f, pickle.HIGHEST_PROTOCOL) return redirect(url_for('evaluation_view')) else: return render_template("index.html", form=form)
def create_artist_submission(): """Creates a new artist in the db from a form submission. Returns: The template for a list of all artists """ form = ArtistForm() if not form.validate(): flash( list(form.errors.values())[0][0], "error", ) return redirect(url_for("create_artist_form")) error = False try: artist_name = request.form.get("name") city = request.form.get("city") state = request.form.get("state") artist = Artist( name=artist_name, genres=get_genres(request.form.getlist("genres")), area_id=get_area_id(city, state), phone=request.form.get("phone"), website=request.form.get("website"), facebook_link=request.form.get("facebook_link"), seeking_venue=bool(request.form.get("seeking_venue")), seeking_description=request.form.get("seeking_description"), image_link=request.form.get("image_link"), ) db.session.add(artist) db.session.commit() artist_id = artist.id except Exception: # pylint: disable=broad-except error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash(f"Artist {artist_name} was unable to be listed!", "error") abort(500) flash(f"Artist {artist_name} was successfully listed!") return redirect(url_for("show_artist", artist_id=artist_id))
def edit_artist_submission(artist_id): """Updates an existing artist in the db from a form submission. Args: artist_id: A str representing the id of the artist to update Returns: The template for the artist's detail page """ form = ArtistForm() if not form.validate(): flash( list(form.errors.values())[0][0], "error", ) return redirect(url_for("edit_artist_form", artist_id=artist_id)) error = False try: artist_name = request.form.get("name") city = request.form.get("city") state = request.form.get("state") artist = Artist.query.get(artist_id) artist.name = artist_name artist.genres = get_genres(request.form.getlist("genres")) artist.area_id = get_area_id(city, state) artist.phone = request.form.get("phone") artist.website = request.form.get("website") artist.facebook_link = request.form.get("facebook_link") artist.seeking_venue = bool(request.form.get("seeking_venue")) artist.seeking_description = request.form.get("seeking_description") artist.image_link = request.form.get("image_link") 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(f"Artist {artist_name} was unable to be updated!", "error") abort(500) flash(f"Artist {artist_name} was successfully updated!") return redirect(url_for("show_artist", artist_id=artist_id))
def newArtist(): # Only logged in users can create an artist if 'email' not in login_session: return redirect('/login') # Using the form located in forms.py form = ArtistForm(request.form) if request.method == 'POST' and form.validate(): newArtist = Artist(name=form.name.data, year_of_birth=form.year_of_birth.data, year_of_death=form.year_of_death.data, country=form.country.data, art_movement=form.art_movement.data, user_id=login_session['user_id']) db.session.add(newArtist) db.session.commit() return redirect(url_for('showArtists')) else: return render_template('newartist.html', form=form)
def create_artist_submission(): """ Create a new artist in the database, the artist details are POSTed """ form = ArtistForm() if form.validate(): name = form.name.data city = form.city.data state = form.state.data phone = form.phone.data image_link = form.image_link.data genres = form.genres.data website_link = form.website_link.data facebook_link = form.facebook_link.data date_added = datetime.datetime.now() try: artist = Artist(name=name, city=city, state=state, phone=phone, image_link=image_link, genres=genres, website_link=website_link, facebook_link=facebook_link, date_added=date_added) db.session.add(artist) db.session.commit() flash('Artist ' + form.name.data + ' 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: Artist not added, there was an error writing to the database: ' + str(e)) return render_template('forms/new_artist.html', form=form) finally: db.session.close() else: flash('ERROR: Artist not added, please check errors below:') return render_template('forms/new_artist.html', form=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.') return redirect(url_for('index'))
def edit_artist_submission(artist_id): # validate form form = ArtistForm(request.form) if not form.validate(): artist = Artist.query.get(artist_id) return render_template('forms/edit_artist.html', form=form, artist=artist) # edit artist try: artist = Artist.query.get(artist_id) artist.set_data(form_data=request.form) db.session.commit() except: db.session.rollback() print(sys.exc_info()) finally: db.session.close() return redirect(url_for('show_artist', artist_id=artist_id))
def create_artist_submission(): form = ArtistForm() if not form.validate(): flash(form.errors) return redirect(url_for("create_artist_form")) error = False try: data = request.form artist = _hydrate_artist(data) db.session.add(artist) db.session.commit() except Exception: error = True db.session.rollback() print(sys.exc_info()) finally: db.session.close() if error: flash("An error occurred. Artist " + data["name"] + " could not be listed.") else: flash("Artist " + data["name"] + " was successfully listed!") return render_template("pages/home.html")
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))