Skip to content

Instantly share code, notes, and snippets.

@redspider
Created May 2, 2013 07:49
Show Gist options
  • Save redspider/5500757 to your computer and use it in GitHub Desktop.
Save redspider/5500757 to your computer and use it in GitHub Desktop.
@blueprint.route('/<library_id>/upload', methods=['GET', 'POST'], auth=auth.admin)
@render_html()
def upload(library_id):
try:
library = m.Library.objects.get(id=library_id)
except m.DoesNotExist:
abort(404)
class Form(IForm):
name = wtf.TextField(_('Item name'), [wtf.Required(), wtf.Length(max=80)])
file = wtf.FileField(_('Document'), [wtf.Required()])
def validate_file(form, field):
if field.data and field.data.content_type != 'application/pdf':
raise wtf.ValidationError(_("File must be a PDF"))
form = Form()
if form.validate_on_submit():
file = request.files[form.file.name]
li = m.LibraryItem()
li.name = form.name.data
li.library = library
li.document.put(
request.files[form.file.name].read(),
content_type = file.content_type,
filename = file.filename
)
li.save()
flask.flash(_("Document uploaded"))
return redirect(url_for('.read', library_id=library_id))
return dict(form=form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment