def new_playlist(): """Show new playlist form, handle form submission. Redirect to my playlists""" # If no user logged in, flash error message and redirect them to login page if not g.user: flash('You must be logged in to create a playlist!', 'danger') return redirect('/login') else: form = PlaylistForm() u_playlists = [playlist.name for playlist in g.user.playlists] # If form is submitted: if form.validate_on_submit(): # import pdb # pdb.set_trace() # save playlist to DB, redirect to user's playlist page if form.name.data not in u_playlists: playlist = Playlist(name=form.name.data, description=form.description.data, user_id=g.user.id) db.session.add(playlist) db.session.commit() return redirect(f'/users/{g.user.id}/playlists') else: flash('You already have a playlist with that name', 'danger') return render_template('new_playlist.html', user=g.user, form=form) else: # show new playlist form: return render_template('new_playlist.html', user=g.user, form=form)
def playlists(username): """Show playlists and create playlists.""" user = User.query.filter_by(username=username).first() if not user: abort(404, f"{username} not found") form = PlaylistForm() if user and g.user and user.id is g.user.id: if form.validate_on_submit(): if user.id is g.user.id: playlist = Playlist(user_id=user.id, name=form.name.data, description=form.description.data) user.playlists.append(playlist) db.session.commit() return redirect(f'/u/{user.username}/playlists/{playlist.id}') else: flash('Sign up to make playlsits', 'light') return redirect('/sign-up') else: form = False return render_template('playlist/playlists.html', form=form, user=user)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data new_playlist = Playlist(name=name, description=description) db.session.add(new_playlist) try: db.session.commit() except IntegrityError as e: if 'name' in e.orig.pgerror: form.name.errors.append( 'Playlist name already exists. Please choose another.') return render_template('new_playlist.html', form=form) return redirect('/playlists') return render_template('new_playlist.html', form=form)
def edit_playlist_info(id): if not user_check(id): playlists = db.get_public_playlists() error = "You are not allowed to do this command." return render_template('home.html', error=error, playlists=playlists) playlist = db.get_playlist(id) form = PlaylistForm(request.form) form.title.data = playlist['title'] form.comment.data = playlist['comment'] checked = playlist['isprivate'] if request.method == 'POST' and form.validate(): title = request.form['title'] comment = request.form['comment'] if request.form.get("isprivate") == "1": isprivate = 1 else: isprivate = 0 if request.files["inputFile"]: file = request.files["inputFile"] if imghdr.what(file) is not None: f = file.read() db.update_playlist(id, title, comment, isprivate, image=f) else: return render_template('/edit_playlist_info.html', form=form, error="Only images are accepted", checked=checked) db.update_playlist(id, title, comment, isprivate) flash('Playlist updated', 'success') return redirect(url_for('edit_playlist', id=id)) return render_template('/edit_playlist_info.html', form=form, checked=checked)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data new_playlist = Playlist(name=name, description=description) db.session.add(new_playlist) try: db.session.commit() except IntegrityError: form.name.errors.append( 'Playlist name taken. Please pick another.') return render_template('new_playlist.html', form=form) flash('Successfully created new playlist!', "success") return redirect('/playlists') return render_template('new_playlist.html', form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = form.name.data desc = form.description.data # Check if input is empty if name.isspace(): flash("Platlist Name is required", "danger") return redirect("/playlists/add") playlist = Playlist(name=name, description=desc) db.session.add(playlist) db.session.commit() flash("Playlist Added!", "success") return redirect("/playlists") return render_template("new_playlist.html", form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK form = PlaylistForm() # if the CSRF token is validated after the form is submitted if form.validate_on_submit(): name = form.name.data description = form.description.data # Create a new playlist instance new_playlist = Playlist(name=name, description=description or None) db.session.add(new_playlist) try: db.session.commit() flash("Playlist created!") return redirect('/playlists') except IntegrityError: form.name.errors.append( "That playlist name is taken. Please choose another name.") return render_template('new_playlist.html', form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK form = PlaylistForm() if request.method == "GET": return render_template('new_playlist.html', form=form) elif request.method == "POST": if form.validate_on_submit(): name = form.name.data description = form.description.data playlist = Playlist(name=name, description=description) db.session.add(playlist) db.session.commit() return redirect('/playlists') else: return render_template('new_playlist.html', form=form) else: flash('something went wrong') return redirect('/playlists')
def update_playlist(playlist_id): """Show update form and process it.""" playlist = Playlist.query.get(playlist_id) if "user_id" not in session or playlist.user_id != session['user_id']: flash("You must be logged in to view!") return redirect("/login") form = PlaylistForm(obj=playlist) if form.validate_on_submit(): playlist.name = form.name.data db.session.commit() return redirect(f"/users/profile/{session['user_id']}") return render_template("/playlist/edit.html", form=form, playlist=playlist)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): Playlist.create_playlist(form) return redirect(f"/playlists") return render_template('new_playlist.html', form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): playlist = Playlist(name=form.name.data, description=form.description.data) db.session.add(playlist) db.session.commit() return redirect("/playlists") return render_template("new_playlist.html", form=form)
def add_playlist(): """Handle add-playlist form.""" form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data new_playlist = Playlist(name=name, description=description) db.session.add(new_playlist) db.session.commit() return redirect("/playlists") return render_template("new_playlist.html", form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = request.form['name'] description = request.form['description'] playlist = Playlist(name=name, description=description) db.session.add(playlist) db.session.commit() return redirect('/playlists') else: return render_template('new_playlist.html', form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK form = PlaylistForm() if form.validate_on_submit(): playlist = Playlist() form.populate_obj(playlist) db.session.add(playlist) db.session.commit() return redirect(url_for('show_all_playlists')) return render_template("new_playlist.html", form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): data = {k: v for k, v in form.data.items() if k != 'csrf_token'} new_playlist = Playlist(**data) db.session.add(new_playlist) db.session.commit() flash(f"{new_playlist.name} has been added!", 'success') return redirect('/playlists') else: return render_template('new_playlist.html', form=form)
def tracks(request, track_slug): track = get_object_or_404(Track, link = track_slug) art = track.artist.all() if request.method == 'POST': postdata = request.POST.copy() form = PlaylistForm(request, postdata) if form.is_valid(): playlist.add_playlist(request) return HttpResponseRedirect('/playlist') else: form = PlaylistForm(request=request, label_suffix=':') form.fields['track_slug'].widget.attrs['value'] = track_slug request.session.set_test_cookie() return render_to_response('tracks.html',locals(), context_instance=RequestContext(request))
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data new_playlist = Playlist(name=name, description=description) db.session.add(new_playlist) db.session.commit() flash("Playlist created successfully", "success") return redirect('/playlists') return render_template('new_playlist.html', form=form)
def playlist_create(request): """ If the request is a GET, renders the Playlist editing page for a new Playlist. If it's a POST, creates a new Playlist with the POST data. """ if request.method == 'POST': f = PlaylistForm(request.POST, instance=Playlist()) piforms = [PlaylistItemForm(request.POST, prefix=str(x), instance=PlaylistItem()) for x in range(0,int(request.POST['n-forms']))] if f.is_valid() and all([piform.is_valid() for piform in piforms]): newplaylist = f.save() for piform in piforms: newpi = piform.save(commit=False) newpi.playlist = newplaylist newpi.save() return redirect('orwell-playlist-index') else: f = PlaylistForm(instance=Playlist()) piforms = [PlaylistItemForm(prefix=str(x), instance=PlaylistItem(position=(x+1))) for x in range(0,2)] # two PlaylistItems to start return render_to_response('orwell/edit-playlist.html', {'form':f, 'itemforms':piforms, 'nforms':2, 'mode':'Create', 'butval':'Create'},context_instance=RequestContext(request))
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ # ADD THE NECESSARY CODE HERE FOR THIS ROUTE TO WORK form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data new_playlist = Playlist(name=name, description=description) db.session.add(new_playlist) db.session.commit() return redirect("/playlists") return render_template("new_playlist.html", form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): data = { key: val for key, val in form.data.items() if key != "csrf_token" } playlist = Playlist(**data) db.session.add(playlist) db.session.commit() return redirect("/playlists") return render_template("new_playlist.html", form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): data = {k: v for k, v in form.data.items() if k != "csrf_token"} new_playlist = Playlist(**data) # new_pet = Pet(name=form.name.data, age=form.age.data, ...) db.session.add(new_playlist) db.session.commit() flash(f"{new_playlist.name} added.") return redirect('/playlists') else: # re-present form for editing return render_template("new_playlist.html", form=form)
def playlist_edit(request, playlist_id): """ Very similar to playlist_create, but for a Playlist that already exists. """ playlist = Playlist.objects.get(id=playlist_id) items = playlist.playlistitem_set.all() if request.method == 'POST': f = PlaylistForm(request.POST, instance=playlist) items.delete() piforms = [PlaylistItemForm(request.POST, prefix=str(x), instance=PlaylistItem()) for x in range(0,int(request.POST['n-forms']))] if f.is_valid() and all([piform.is_valid() for piform in piforms]): f.save() for piform in piforms: newpi = piform.save(commit=False) newpi.playlist = playlist newpi.save() return redirect('orwell-playlist-index') else: f = PlaylistForm(instance=playlist) piforms = [PlaylistItemForm(prefix=str(x), instance=items[x]) for x in range(0,len(items))] return render_to_response('orwell/edit-playlist.html', {'form':f, 'itemforms':piforms, 'nforms':len(items), 'mode':'Edit', 'butval':'Save'},context_instance=RequestContext(request))
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): playlist = Playlist(name=form.name.data, description=form.description.data) if form.name.data.isspace(): form.name.errors.append('Please enter a name for this playlist') return render_template('new_playlist.html', form=form) if form.description.data.isspace(): form.description.errors.append( 'Please enter an description for this song') return render_template('new_playlist.html', form=form) db.session.add(playlist) db.session.commit() return redirect('/playlists') return render_template('new_playlist.html', form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data playlist = Playlist(name=name, description=description) db.session.add(playlist) db.session.commit() flash('Successfully create a playlist!', 'success') return redirect(url_for('show_playlist', playlist_id=playlist.id)) else: return render_template('new_playlist.html', form=form)
def add_playlist(): print(request.form) """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): name = form.name.data description = form.description.data flash( f"Created new playlist: name is {name}, description is {description}" ) ps = Playlist(name=name, description=description) db.session.add(ps) # ps = Playlist(name=name, description=description) db.session.commit() return redirect('/') else: return render_template('new_playlist.html', form=form)
def add_playlist(): """Handle add-playlist form: - if form not filled out or invalid: show form - if valid: add playlist to SQLA and redirect to list-of-playlists """ form = PlaylistForm() if form.validate_on_submit(): #csrf & is POST name = form.name.data description = form.description.data playlist = Playlist(name=name, description=description) db.session.add(playlist) db.session.commit() flash(f"Playlist created!") return redirect(f'/playlists') else: return render_template("new_playlist.html", form=form)
def create_playlist(): form = PlaylistForm(request.form) if request.method == 'POST' and form.validate(): title = form.title.data comment = form.comment.data userid = session['id'] if request.form.get("isprivate") == "1": isprivate = 1 else: isprivate = 0 if request.files["inputFile"]: file = request.files["inputFile"] if (imghdr.what(file) is not None): f = file.read() db.create_playlist(title, comment, userid, isprivate, image=f) else: return render_template('create_playlist.html', form=form, error="Only images are accepted") else: db.create_playlist(title, comment, userid, isprivate) flash('Playlist created', 'success') return redirect(url_for('dashboard')) return render_template('create_playlist.html', form=form)
def profile(id): """Example hidden page for logged-in users only.""" # raise 'here' if "user_id" not in session or id != session['user_id']: flash("You must be logged in to view!") return redirect("/login") else: id = session["user_id"] user = User.query.get_or_404(id) form = PlaylistForm() user = User.query.get_or_404(id) playlists = Playlist.query.filter_by(user_id=id).all() if form.validate_on_submit(): name = form.name.data new_playlist = Playlist(name=name, user_id=session['user_id']) db.session.add(new_playlist) db.session.commit() playlists.append(new_playlist) return redirect(f"/users/profile/{id}") return render_template("users/profile.html", playlists=playlists, form=form, user=user)