def upload(): """Upload an image to Eventum **Route:** ``/admin/media/upload`` **Methods:** ``POST`` """ form = UploadImageForm(request.form) uploaded_from = form.uploaded_from.data if form.validate_on_submit(): f = request.files['image'] if f and allowed_file(f.filename.lower()): filename = create_filename(f, request.form['filename']) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) image = Image(filename=filename, default_path=app.config['RELATIVE_UPLOAD_FOLDER']+filename, creator=g.user) image.save() return redirect(url_for('.index')) flash("Filename {} is invalid".format(f.filename)) if form.errors: flash(form.errors) if uploaded_from: return redirect(uploaded_from) return render_template('admin/media/upload.html', form=form)
def index(): form = UploadImageForm() if form.validate_on_submit(): f = form.image.data f_name = secure_filename(f.filename) try: file, extension = f_name.rsplit('.', 1) if extension not in ACCEPTED_EXTENSIONS: flash('The file should be an image', category='error') return redirect(url_for('index')) i = ImageModel(file_name=f_name) img_f = Image.open(f) i.save_image(img_f) i.save_thumbnail(img_f) db.session.add(i) db.session.commit() except (Image.DecompressionBombWarning, Image.DecompressionBombError) as e: print(e) flash('Decompression bomb error.', category='error') except AssertionError as e: print(e) flash(str(e), category='error') except Exception: flash('Что-то пошло не так, пока мы сохраняли вашу фотографию :(', category='error') return redirect(url_for('index')) return render_template('index.html', title='Мой альбом', form=form)
def create(): """Create a new event. **Route:** ``/admin/events/create`` **Methods:** ``GET, POST`` """ form = CreateEventForm(request.form) if form.validate_on_submit(): try: EventsHelper.create_event(form, g.user) except EventumError.GCalAPI as e: flash(e.message, ERROR_FLASH) return redirect(url_for('.index')) if form.errors: for error in form.errors: for message in form.errors[error]: flash(message, ERROR_FLASH) upload_form = UploadImageForm() delete_form = DeleteEventForm() images = Image.objects() return render_template('admin/events/create.html', form=form, delete_form=delete_form, upload_form=upload_form, images=images)
def new(): """Create a new blog post. **Route:** ``/admin/posts/new`` **Methods:** ``POST`` """ form = CreateBlogPostForm(request.form) form.author.choices = [ (str(u.id), u.name + " (You)" if u == g.user else u.name) for u in User.objects()] form.author.data = str(g.user.id) upload_form = UploadImageForm() if form.validate_on_submit(): author = User.objects().get(id=ObjectId(form.author.data)) post = BlogPost(title=form.title.data, slug=form.slug.data, images=[Image.objects().get(filename=fn) for fn in form.images.data], markdown_content=form.body.data, author=author, posted_by=g.user, tags=form.tags.data) post.save() if form.published.data: post.publish() else: post.unpublish() return redirect(url_for('.index')) images = Image.objects() return render_template('admin/posts/edit.html', user=g.user, form=form, images=images, upload_form=upload_form)
def edit(event_id): """""" try: event = Event.objects().get(id=event_id) except (DoesNotExist, ValidationError): flash('Cannont find event with id "%s"' % event_id) return redirect(url_for('.index')) form = EditEventForm(request.form) if request.method == 'POST' else \ EventsHelper.create_form(event, request) if form.validate_on_submit(): try: EventsHelper.update_event(event, form) except GoogleCalendarAPIError as e: flash(e.message) return redirect(url_for('.index')) if form.errors: for error in form.errors: for message in form.errors[error]: flash(message) delete_form = DeleteEventForm() upload_form = UploadImageForm() images = Image.objects() return render_template('admin/events/edit.html', form=form, event=event, delete_form=delete_form, upload_form=upload_form, images=images)
def index(): """View all of the uploaded images. **Route:** ``/admin/media`` **Methods:** ``GET`` """ images = Image.objects() form = UploadImageForm() return render_template('admin/media/media.html', images=images, form=form)
def upload(): """Upload an image to Eventum :returns: A JSON containing the status of the file upload, or error messages, if any. :rtype: json """ form = UploadImageForm(request.form) if form.validate_on_submit(): f = request.files['image'] if f: filename = create_filename(f, request.form['filename']) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) default_path = app.config['RELATIVE_UPLOAD_FOLDER'] + filename image = Image(filename=filename, default_path=default_path, creator=g.user) image.save() return jsonify({"status": "true"}) if form.errors: return jsonify(form.errors) return jsonify({"status": "error"})
def index(): """View and manage users Whitelisted users are the only ones allowed to make user accounts. """ upload_form = UploadImageForm() whitelist_form = AddToWhitelistForm() return render_template('admin/users/users.html', whitelist_form=whitelist_form, upload_form=upload_form, whitelist=Whitelist.objects(redeemed=False), users=User.objects(), images=Image.objects(), current_user=g.user)
def uploadimage(username): form = UploadImageForm(request.form) user = User.query.filter(User.username == username).first() image_url = None filename = None if request.method == 'POST' and request.files['image']: # save image filename = userimage.save(request.files['image'], name=current_user.username + '.jpg') image_url = userimage.url(filename) flash(u"%s 已上传" % filename, 'successfully') return render_template("user/uploadimage.html", form=form, user=user, image_url=image_url) else: return render_template("user/uploadimage.html", form=form, user=user, image_url=image_url)
def edit(event_id): """Edit an existing event. **Route:** ``/admin/events/edit/<event_id>`` **Methods:** ``GET, POST`` :param str event_id: The ID of the event to edit. """ try: event = Event.objects().get(id=event_id) except (DoesNotExist, ValidationError): flash('Cannot find event with id "{}"'.format(event_id), ERROR_FLASH) return redirect(url_for('.index')) form = EditEventForm(event, request.form) if request.method == 'POST' else \ EventsHelper.create_form(event, request) if form.validate_on_submit(): try: EventsHelper.update_event(event, form) except EventumError.GCalAPI as e: flash(e.message, ERROR_FLASH) return redirect(url_for('.index')) if form.errors: for error in form.errors: for message in form.errors[error]: flash(message, ERROR_FLASH) delete_form = DeleteEventForm() upload_form = UploadImageForm() images = Image.objects() return render_template('admin/events/edit.html', form=form, event=event, delete_form=delete_form, upload_form=upload_form, images=images)
def create(): """""" form = CreateEventForm(request.form) if form.validate_on_submit(): try: EventsHelper.create_event(form, g.user) except GoogleCalendarAPIError as e: flash(e.message) return redirect(url_for('.index')) if form.errors: for error in form.errors: for message in form.errors[error]: flash(message) upload_form = UploadImageForm() delete_form = DeleteEventForm() images = Image.objects() return render_template('admin/events/create.html', form=form, delete_form=delete_form, upload_form=upload_form, images=images)
def edit(post_id): """Edit an existing blog post. **Route:** ``/admin/posts/edit/<post_id>`` **Methods:** ``GET, POST`` :param str post_id: The ID of the post to edit. """ try: object_id = ObjectId(post_id) except InvalidId: return abort(404) try: post = BlogPost.objects().with_id(object_id) except (DoesNotExist, ValidationError): flash('Cannot find blog post with id {}.'.format(post_id)) return redirect(url_for('.index')) if request.method == 'POST': form = CreateBlogPostForm(request.form) form.author.choices = [ (str(u.id), u.name + " (You)" if u == g.user else u.name) for u in User.objects()] form.author.default = str(g.user.id) if form.validate_on_submit(): post.title = form.title.data post.author = User.objects.get(id=ObjectId(form.author.data)) post.slug = form.slug.data post.markdown_content = form.body.data post.images = [Image.objects().get(filename=fn) for fn in form.images.data] post.tags = form.tags.data if form.featured_image.data: post.featured_image = Image.objects().get(filename=form.featured_image.data) else: post.featured_image = None post.save() if post.published != form.published.data: if form.published.data: post.publish() flash('Blogpost published') else: post.unpublish() flash('Blogpost unpublished') return redirect(url_for('.index')) upload_form = UploadImageForm() featured_image = post.featured_image.filename if post.featured_image else None form = CreateBlogPostForm(request.form, title=post.title, slug=post.slug, published=post.published, body=post.markdown_content, images=[image.filename for image in post.images], author=str(post.author.id), featured_image=featured_image, tags=post.tags) form.author.choices = [ (str(u.id), u.name + " (You)" if u == g.user else u.name) for u in User.objects()] form.author.default = str(g.user.id) images = [image for image in Image.objects() if image not in post.images] return render_template('admin/posts/edit.html', user=g.user, form=form, post=post, images=images, upload_form=upload_form, tags=post.tags)
def index(): images = Image.objects() form = UploadImageForm() return render_template('admin/media/media.html', images=images, form=form)