def delete_image(image_id): img = Image.get(image_id) key = urlparse(img.url).path.rsplit('/', 1)[1] bucket = connect_to_s3() k = bucket.get_key(key) if k: k.delete() Image.delete(img.id) flash("%s was deleted from S3 and the database. It is unrecoverable." % key) return redirect(url_for('list_entries'))
def delete_image(image_id): img = Image.get(image_id) key = urlparse(img.url).path.rsplit('/', 1)[1] bucket = connect_to_s3() k = bucket.get_key(key) if k: k.delete() Image.delete(img.id) flash( "%s was deleted from S3 and the database. It is unrecoverable." % key) return redirect(url_for('list_entries'))
def edit_user(user_id=-1): user_form = EditUserForm() if user_form.validate_on_submit(): user = Users.get(user_form.user_id.data) if user.id == session.get('user_id') or user.admin: user.first_name = user_form.first_name.data user.last_name = user_form.last_name.data user.email = user_form.email.data user.alias = user_form.alias.data user.last_modified = datetime.now() try: avatar = list(Image.select(Image.q.url==user_form.avatar.data))[0] except (SQLObjectNotFound, IndexError): pass else: user.avatar = avatar flash("%s %s has been updated" % (user.first_name, user.last_name)) return redirect(url_for('list_users')) else: flash("Sorry, you're not allowed to do that") return redirect(url_for('edit_user', user_id=user.id)) else: try: user = Users.get(user_id) except SQLObjectNotFound: user = {'first_name': '', 'last_name': '', 'email': '', 'password': '', 'avatar': ''} finally: return render_template('edit_user.html', data={'form': user_form, 'user': user})
def create_image(): image_form = ImageForm(request.form) if image_form.validate_on_submit(): fn = image_form.image.file.filename side = image_form.side.data resize_to = image_form.resize_to.data caption = image_form.caption.data if '.' in fn and fn.split('.', 1)[1].lower() in ALLOWED_EXTENSIONS: filename = os.path.join(TEMP_UPLOAD_FOLDER, secure_filename(fn)) mime = image_form.image.file.content_type try: os.stat(TEMP_UPLOAD_FOLDER) except OSError: os.makedirs(TEMP_UPLOAD_FOLDER) image_form.image.file.save(filename) if side: side_idx = int(side) length = int(resize_to) else: side_idx = None length = 0 image, thumb_size = store_image(filename, mime, length, side_idx) if image: os.unlink(filename) url = "%s/%s" % (IMAGE_BASEPATH, image) img = Image(url=url, width=thumb_size[0], height=thumb_size[1], caption=caption) return render_template('upload_file.html', data={ 'filename': url, 'form': image_form }) else: return render_template('upload_file.html', data={'form': image_form}) else: return render_template('upload_file.html', data={'form': image_form})
def create_user(): user_form = CreateUserForm() if user_form.validate_on_submit(): user = Users(first_name = user_form.first_name.data, last_name = user_form.last_name.data, email = user_form.email.data, password = generate_password(user_form.password.data), alias = user_form.alias.data) if user_form.avatar.data: i = Image(url=user_form.alias.data) user.avatar = i flash("%s %s has been created" % (user.first_name, user.last_name)) return redirect(url_for('list_users')) else: user = {'first_name': '', 'last_name': '', 'email': '', 'password': '', 'avatar': ''} return render_template('edit_user.html', data={'form': user_form, 'user': user})
def list_images(): images = list(Image.select()) return render_template('show_images.html', data={'images': images})