Skip to content

Instantly share code, notes, and snippets.

@Porter97
Created February 13, 2020 19:58
Show Gist options
  • Save Porter97/7f721eb957661c6754e0430871558fe4 to your computer and use it in GitHub Desktop.
Save Porter97/7f721eb957661c6754e0430871558fe4 to your computer and use it in GitHub Desktop.
#...
from .forms import EditProfileForm, EditProfileAdminForm, CollectionForm, ContentForm
from ..models import User, Role, Collection, Permission, Content
#...
@main.route('/content/<int:id>', methods=['GET'])
def get_content(id):
content = Content.query.get_or_404(id)
return render_template('content.html', content=content, author=content.collection.author)
@main.route('/new-content/', methods=['GET', 'POST'])
@login_required
def new_content():
form = ContentForm()
form.collection.choices = [(c.id, c.name) for c in current_user.collections]
if current_user.can(Permission.WRITE) and form.validate_on_submit():
collection = Collection.query.get_or_404(form.collection.data)
if collection.has_content(form.url.data):
flash('That link is already in this Collection!')
return redirect(url_for('.new_content'))
content = Content(title=form.title.data,
url=form.url.data,
description=form.description.data,
collection=collection)
db.session.add(content)
db.session.commit()
if content.add_to_stream():
flash("Your Content has been added!")
return redirect(url_for('.get_content', id=content.id))
else:
db.session.delete(content)
db.session.commit()
flash("An error occurred, please try again")
return redirect(url_for('.new_content'))
return render_template('new_content.html', form=form)
@main.route('/edit-content/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_content(id):
content = Content.query.get_or_404(id)
if current_user != content.collection.author and \
not current_user.can(Permission.ADMIN):
abort(403)
form = ContentForm()
form.collection.choices = [(c.id, c.name) for c in current_user.collections]
if form.validate_on_submit():
collection = Collection.query.get_or_404(form.collection.data)
if content.url != form.url.data and collection.has_content(form.url.data):
flash('That link is already in this Collection!')
return redirect(url_for('.edit_content', id=id))
# Update Database
content.title = form.title.data
content.description = form.description.data
content.url = form.url.data
content.collection = collection
db.session.commit()
if content.update_stream():
return redirect(url_for('.get_content', id=id))
form.title.data = content.title
form.description.data = content.description
form.url.data = content.url
return render_template('edit_content.html', content=content, form=form)
@main.route('/delete-content/<int:id>', methods=['GET', 'POST'])
@login_required
def delete_content(id):
content = Content.query.get_or_404(id)
if current_user != content.collection.author and \
not current_user.can(Permission.ADMIN):
abort(403)
if content.delete_from_stream():
db.session.delete(content)
db.session.commit()
flash('Your Content has been deleted.')
return redirect(url_for('.index'))
else:
flash('An error occurred, please try again.')
return redirect(url_for('.edit_content', id=id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment