From 6dbd4ca2298b92696fe69500c3c0fe62637b21d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannnes=20B=C3=BCrst?= Date: Thu, 19 Mar 2020 17:12:31 +0100 Subject: [PATCH] basic admin interface done --- calender/calender.py | 57 ++++++++++++++++++------- calender/schema.sql | 4 +- calender/templates/calender/index.html | 54 ++++++++++++----------- calender/templates/calender/update.html | 20 ++++++++- 4 files changed, 92 insertions(+), 43 deletions(-) diff --git a/calender/calender.py b/calender/calender.py index 87852f2..1fa32b7 100644 --- a/calender/calender.py +++ b/calender/calender.py @@ -2,31 +2,45 @@ import os from flask import ( Blueprint, render_template, - flash, request, url_for, redirect, abort, g, send_from_directory, current_app) + flash, request, url_for, redirect, abort, send_from_directory, current_app) from werkzeug.utils import secure_filename from calender.auth import login_required from calender.db import get_db bp = Blueprint('calender', __name__) -ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'} +ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webm', 'mp4', 'bmp'} -#Todo admin interface for managing submit, comments, submission date + +# Todo admin interface for managing submit, comments, submission date @bp.route('/') def index(): db = get_db() posts = db.execute( - 'SELECT p.id, title, body, created, author_id, username, nickname, file' - ' FROM post p JOIN user u ON p.author_id = u.id' + 'SELECT p.id, title, body, created, nickname, file, chosen, type' + ' FROM post p' ' ORDER BY created DESC' ).fetchall() # noinspection PyUnresolvedReferences return render_template('calender/index.html', posts=posts) -@bp.route('/create', methods=('GET', 'POST')) +@bp.route('/admin') @login_required +def admin(): + db = get_db() + posts = db.execute( + 'SELECT p.id, title, body, created, nickname, file, chosen, type' + ' FROM post p' + ' ORDER BY created DESC' + ).fetchall() + # noinspection PyUnresolvedReferences + return render_template('calender/admin.html', posts=posts) + + +@bp.route('/create', methods=('GET', 'POST')) +# @login_required def create(): if request.method == 'POST': title = request.form['title'] @@ -52,14 +66,16 @@ def create(): flash(error) else: if file and allowed_file(file.filename): + file_type = check_file_type(file.filename) filename = secure_filename(file.filename) file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) + db = get_db() db.execute( - 'INSERT INTO post (title, body, author_id, nickname, file)' - ' VALUES (?, ?, ?, ?, ?)', - (title, body, g.user['id'], nickname, url_for('calender.uploaded_file', - filename=filename)) + 'INSERT INTO post (title, body, nickname, file, chosen, type)' + ' VALUES (?, ?, ?, ?, ?, ?)', + (title, body, nickname, url_for('calender.uploaded_file', + filename=filename), False, file_type) ) db.commit() # return redirect(url_for('calender.uploaded_file',filename=filename)) @@ -70,8 +86,8 @@ def create(): def get_post(id, check_author=True): post = get_db().execute( - 'SELECT p.id, title, body, created, author_id, username, nickname, file' - ' FROM post p JOIN user u ON p.author_id = u.id' + 'SELECT p.id, title, body, created, nickname, file, chosen, type' + ' FROM post p' ' WHERE p.id = ?', (id,) ).fetchone() @@ -79,8 +95,8 @@ def get_post(id, check_author=True): if post is None: abort(404, "Post id {0} doesn't exist.".format(id)) - if check_author and post['author_id'] != g.user['id']: - abort(403) + # if check_author and post['author_id'] != g.user['id']: + # abort(403) return post @@ -92,7 +108,9 @@ def update(id): if request.method == 'POST': title = request.form['title'] + nickname = request.form['nickname'] body = request.form['body'] + chosen = request.form['chosen'] error = None if not title: @@ -103,9 +121,9 @@ def update(id): else: db = get_db() db.execute( - 'UPDATE post SET title = ?, body = ?' + 'UPDATE post SET title = ?, body = ?, nickname = ?, chosen = ?' ' WHERE id = ?', - (title, body, id) + (title, body, nickname, chosen, id) ) db.commit() return redirect(url_for('calender.index')) @@ -128,6 +146,13 @@ def allowed_file(filename): filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS +def check_file_type(filename): + if filename.rsplit('.', 1)[1].lower() in ['webm', 'mp4']: + return 'video' + else: + return 'image' + + @bp.route('/uploads/') def uploaded_file(filename): return send_from_directory(current_app.config['UPLOAD_FOLDER'], diff --git a/calender/schema.sql b/calender/schema.sql index 17c92f1..9c79923 100644 --- a/calender/schema.sql +++ b/calender/schema.sql @@ -9,11 +9,11 @@ CREATE TABLE user ( CREATE TABLE post ( id INTEGER PRIMARY KEY AUTOINCREMENT, - author_id INTEGER NOT NULL, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, title TEXT NOT NULL, nickname TEXT NOT NULL, body TEXT NOT NULL, file TEXT NOT NULL, - FOREIGN KEY (author_id) REFERENCES user (id) + chosen INTEGER NOT NULL , + type TEXT NOT NULL ); diff --git a/calender/templates/calender/index.html b/calender/templates/calender/index.html index de3eb97..9aa9e62 100644 --- a/calender/templates/calender/index.html +++ b/calender/templates/calender/index.html @@ -8,11 +8,11 @@
Submit your meme for the next day now! Feel free to create and submit your own creations.

- {% if g.user %} - - - - {% endif %} + + + + +
{% endblock %} @@ -20,26 +20,32 @@ {% block content %}
{% for post in posts %} -
-
-
-
-

{{ post['title'] }}

-

{{ post['nickname'] }}

-
by {{ post['username'] }} - on {{ post['created'].strftime('%Y-%m-%d') }}
-
- {% if g.user['id'] == post['author_id'] %} - Edit + {% if post['chosen'] == 1 %} +
+
+
+
+

{{ post['title'] }}

+

{{ post['nickname'] }}

+
by {{ post['username'] }} + on {{ post['created'].strftime('%Y-%m-%d') }}
+
+
+ {% if post['type'] == 'image' %} + {% endif %} -
- -

{{ post['body'] }}

-
- {% if not loop.last %} -
- {% endif %} -
+ {% if post['type'] == 'video' %} + + {% endif %} +

{{ post['body'] }}

+ + {% if not loop.last %} +
+ {% endif %} +
+ {% endif %} {% endfor %} {% endblock %} diff --git a/calender/templates/calender/update.html b/calender/templates/calender/update.html index 6ae4f46..d40b6b1 100644 --- a/calender/templates/calender/update.html +++ b/calender/templates/calender/update.html @@ -4,12 +4,30 @@ {% endblock %} {% block content %} -
+ + + + + {% if post['type'] == 'image' %} + + {% endif %} + {% if post['type'] == 'video' %} + + {% endif %} +
+

Choose a mp4, webm, gif , png or jpg file.(max 25MB)

+ +
+
+