Skip to content

Instantly share code, notes, and snippets.

@Porter97
Last active March 11, 2020 16:29
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/7ad68ef939ff20d3a2b03868ceb60eb6 to your computer and use it in GitHub Desktop.
Save Porter97/7ad68ef939ff20d3a2b03868ceb60eb6 to your computer and use it in GitHub Desktop.
#...
@main.route('/new-content/', methods=['GET', 'POST'])
@login_required
def new_content():
form = ContentForm()
# Generate Stream token for use in template
token = current_user.stream_user_token()
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,
image=form.image.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'))
# Pass through token in return statement
return render_template('new_content.html', form=form, token=token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment