Skip to content

Instantly share code, notes, and snippets.

@Porter97
Last active February 13, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Porter97/cfaac8372f3b1f61ca30028bc00b8df8 to your computer and use it in GitHub Desktop.
Save Porter97/cfaac8372f3b1f61ca30028bc00b8df8 to your computer and use it in GitHub Desktop.
from flask import current_app, render_template, flash, redirect, url_for, abort
#...
@main.route('/edit-collection/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_collection(id):
collection = Collection.query.get_or_404(id)
if current_user != collection.author and \
not current_user.can(Permission.ADMIN):
abort(403)
form = CollectionForm()
if form.validate_on_submit():
# Update Database
collection.name = form.name.data
collection.description = form.description.data
db.session.commit()
# Update Stream Activity
if collection.update_stream():
return redirect(url_for('.edit_collection', id=id))
form.name.data = collection.name
form.description.data = collection.description
return render_template('edit_collection.html', collection=collection, form=form)
@main.route('/delete-collection/<int:id>', methods=['GET', 'POST'])
@login_required
def delete_collection(id):
collection = Collection.query.get_or_404(id)
if current_user != collection.author and \
not current_user.can(Permission.ADMIN):
abort(403)
if collection.delete_from_stream():
db.session.delete(collection)
db.session.commit()
flash('Your Collection has been deleted.')
return redirect(url_for('.index'))
else:
flash("An error occurred, please try again")
return redirect(url_for('.edit_collection', id=collection.id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment