From d2d122fc98d90aa8ba9593e6b2f87f35496cea47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Fri, 19 May 2017 11:35:55 +0200 Subject: [PATCH] Redigera och ta bort foton --- app/forms.py | 5 +++++ app/templates/admin.html | 4 ++-- app/templates/admin_photo.html | 31 +++++++++++++++++++++++++++++++ app/views.py | 27 ++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 app/templates/admin_photo.html diff --git a/app/forms.py b/app/forms.py index d403a1f..3b1278a 100644 --- a/app/forms.py +++ b/app/forms.py @@ -16,3 +16,8 @@ class PageForm(FlaskForm): widget=widgets.ListWidget(prefix_label=False), get_label = lambda p: ''.format(p.id)) submit = SubmitField('Ok') + +class PhotoForm(FlaskForm): + alt = StringField('Beskrivning', validators=[DataRequired()]) + link = QuerySelectField('Länkar till', get_label = lambda p: '{} - {}'.format(p.permalink, p.title)) + submit = SubmitField('Ok') diff --git a/app/templates/admin.html b/app/templates/admin.html index 2351a4f..84ea88e 100644 --- a/app/templates/admin.html +++ b/app/templates/admin.html @@ -127,7 +127,7 @@
@@ -154,7 +154,7 @@ $('#deletemodal').on('show.bs.modal', function (event) { var href = '/admin/delpage/' + id } else { var message = 'Vill du verkligen ta bort bilden - ' + title + '?' - var href = '##' + var href = '/admin/delphoto/' + id } modal.find('.modal-body p').text(message) modal.find('.modal-footer a').attr('href', href) diff --git a/app/templates/admin_photo.html b/app/templates/admin_photo.html new file mode 100644 index 0000000..d63a9a5 --- /dev/null +++ b/app/templates/admin_photo.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} +{% import "bootstrap/wtf.html" as wtf %} +{% block navbar %} +{% endblock %} + +{% block content %} +
+
+
+
+
+ + {{ wtf.form_field(form.alt, form_type="horizontal") }} + + {{ wtf.form_field(form.link, form_type="horizontal") }} + + {{ wtf.form_field(form.submit, form_type="horizontal") }} + +
+
+
+
+
+
+ +
+
+
+
+
+{% endblock %} diff --git a/app/views.py b/app/views.py index f1f4c39..c70d31e 100644 --- a/app/views.py +++ b/app/views.py @@ -3,7 +3,7 @@ from flask import render_template, send_from_directory, redirect, url_for, reque from werkzeug.utils import secure_filename from . import app, db from .models import Page, Photo -from .forms import PageForm +from .forms import PageForm, PhotoForm @app.route('/') @@ -100,3 +100,28 @@ def upload(): os.remove(tempname) return redirect(url_for('admin')) +@app.route('/admin/delphoto/') +def admin_delphoto(id): + photo = Photo.query.filter_by(id=id).first_or_404() + db.session.delete(photo) + db.session.commit() + filename = '{}.jpg'.format(id) + imgpath = os.path.join('app', 'photos', filename) + thumb = os.path.join('app', 'photos', 'thumbs', filename) + slide = os.path.join('app', 'photos', 'slides', filename) + os.remove(imgpath) + os.remove(thumb) + os.remove(slide) + return redirect(url_for('admin')) +@app.route('/admin/photo/', methods=['GET', 'POST']) +def admin_photo(id): + photo = Photo.query.filter_by(id=id).first_or_404() + form = PhotoForm(obj=photo) + form.link.query = Page.query.all() + + if form.validate_on_submit(): + form.populate_obj(photo) + db.session.commit() + return redirect(url_for('admin')) + else: + return render_template('admin_photo.html', photo=photo, form=form)