From 978086f512020bce2332e47f1229afdc48b7b014 Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Wed, 11 Jan 2023 10:53:01 +0000 Subject: [PATCH] Added Image deletion Added buttons for image functions Moved Image functions to their own file Added PFP row in users table Moved Groups back to the root templates folder --- gallery/__init__.py | 6 +- gallery/gallery.py | 35 +------- gallery/image.py | 83 +++++++++++++++++ gallery/schema.sql | 1 + gallery/templates/{groups => }/group.html | 0 gallery/templates/image.html | 89 +++++++++++++++++-- .../user/themes/default/buttons/img-tool.scss | 29 ++++++ gallery/user/themes/default/style.scss | 60 ++++++++++++- setup.py | 2 +- 9 files changed, 262 insertions(+), 43 deletions(-) create mode 100644 gallery/image.py rename gallery/templates/{groups => }/group.html (100%) create mode 100644 gallery/user/themes/default/buttons/img-tool.scss diff --git a/gallery/__init__.py b/gallery/__init__.py index 47ae973..046877b 100644 --- a/gallery/__init__.py +++ b/gallery/__init__.py @@ -5,7 +5,7 @@ print(""" | |_| | | | | | |_| | |__| __/ (_| \\__ \\ \\___/|_| |_|_|\\__, |_____\\___|\\__, |___/ |___/ |___/ -Created by Fluffy Bean - Version 100123 +Created by Fluffy Bean - Version 110123 """) # Import base packages @@ -98,6 +98,10 @@ def create_app(test_config=None): app.register_blueprint(gallery.blueprint) app.add_url_rule('/', endpoint='index') + # Load routes for images + from . import image + app.register_blueprint(image.blueprint) + # Load APIs from . import api app.register_blueprint(api.blueprint) diff --git a/gallery/gallery.py b/gallery/gallery.py index 2443eeb..c7500a2 100644 --- a/gallery/gallery.py +++ b/gallery/gallery.py @@ -22,41 +22,13 @@ def index(): return render_template('index.html', images=images) -@blueprint.route('/image/') -def image(id): - db = get_db() - image = db.execute( - 'SELECT * FROM posts' - ' WHERE id = ?', - (id,) - ).fetchone() - - if image is None: - abort(404) - - file = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'], 'original', image['file_name'])) - - raw_exif = file.getexif() - human_exif = {} - for tag in raw_exif: - name = TAGS.get(tag, tag) - value = raw_exif.get(tag) - - if isinstance(value, bytes): - value = value.decode() - - human_exif[name] = value - - return render_template('image.html', image=image, exif=human_exif) - - @blueprint.route('/group') def groups(): - return render_template('groups/group.html', group_id='gwa gwa') + return render_template('group.html', group_id='gwa gwa') @blueprint.route('/group/') def group(id): - return render_template('groups/group.html', group_id=id) + return render_template('group.html', group_id=id) @blueprint.route('/upload', methods=('GET', 'POST')) @@ -82,7 +54,8 @@ def upload(): db.commit() return 'Gwa Gwa' - + + # GET, or in human language, when you visit the page return render_template('upload.html') diff --git a/gallery/image.py b/gallery/image.py new file mode 100644 index 0000000..7c4787c --- /dev/null +++ b/gallery/image.py @@ -0,0 +1,83 @@ +from flask import Blueprint, flash, g, redirect, render_template, request, url_for, jsonify, current_app +from werkzeug.exceptions import abort +from werkzeug.utils import secure_filename +from gallery.auth import login_required +from gallery.db import get_db +import os +import datetime +from PIL import Image +from PIL.ExifTags import TAGS +dt = datetime.datetime.now() + +blueprint = Blueprint('image', __name__, url_prefix='/image') + + +def get_post(id, check_author=True): + post = get_db().execute( + 'SELECT p.author_id FROM posts p JOIN users u ON p.author_id = u.id' + ' WHERE p.id = ?', + (id,) + ).fetchone() + + if post is None: + return False + + if check_author and post['author_id'] != g.user['id']: + return False + + return post + + +@blueprint.route('/', methods=('GET', 'POST')) +def image(id): + if request.method == 'POST': + image = get_post(id) + action = request.form['action'] + + if not image: + abort(403) + + if action == 'delete': + try: + db = get_db() + db.execute('DELETE FROM posts WHERE id = ?', (id,)) + db.commit() + except: + return 'database error' + + try: + os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], 'original', image['file_name'])) + except: + return 'os error' + + # GET, it should be called Gwa Gwa because it sounds funny + # Get image from database + db = get_db() + image = db.execute( + 'SELECT * FROM posts' + ' WHERE id = ?', + (id,) + ).fetchone() + + if image is None: + abort(404) + + # Get exif data from image + file = Image.open(os.path.join(current_app.config['UPLOAD_FOLDER'], 'original', image['file_name'])) + raw_exif = file.getexif() + human_exif = {} + + for tag in raw_exif: + name = TAGS.get(tag, tag) + value = raw_exif.get(tag) + + if isinstance(value, bytes): + value = value.decode() + + human_exif[name] = value + + if len(human_exif) == 0: + human_exif = False + + # All in le head + return render_template('image.html', image=image, exif=human_exif) \ No newline at end of file diff --git a/gallery/schema.sql b/gallery/schema.sql index 16448b6..b6824e8 100644 --- a/gallery/schema.sql +++ b/gallery/schema.sql @@ -1,6 +1,7 @@ CREATE TABLE IF NOT EXISTS users ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, + profile_picture TEXT NOT NULL DEFAULT 'default.png', email TEXT NOT NULL, password TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP diff --git a/gallery/templates/groups/group.html b/gallery/templates/group.html similarity index 100% rename from gallery/templates/groups/group.html rename to gallery/templates/group.html diff --git a/gallery/templates/image.html b/gallery/templates/image.html index 26ffe27..6733180 100644 --- a/gallery/templates/image.html +++ b/gallery/templates/image.html @@ -13,15 +13,92 @@ onload="imgFade(this)" style="display:none;" /> +
+
+ + + +
+ {% if g.user['id'] == image['author_id'] %} +
+ + + +
+ {% endif %} +
-

{{ image['file_name'] }}

+

Info

+

{{ image['file_name'] }}

{{ image['id'] }}

{{ image['author_id'] }}

+

{{ image['created_at'] }}

+

{{ image['description'] }}

-
- {% for tag in exif %} -

{{ tag }}: {{ exif[tag] }}

- {% endfor %} -
+ {% if exif is not false %} +
+

Exif

+ {% for tag in exif %} +

{{ tag }}: {{ exif[tag] }}

+ {% endfor %} +
+ {% endif %} + {% endblock %} \ No newline at end of file diff --git a/gallery/user/themes/default/buttons/img-tool.scss b/gallery/user/themes/default/buttons/img-tool.scss new file mode 100644 index 0000000..5e0f08e --- /dev/null +++ b/gallery/user/themes/default/buttons/img-tool.scss @@ -0,0 +1,29 @@ +.tool-btn { + margin: 0; + padding: 0.5rem; + + width: 2.5rem; + height: 2.5rem; + + border: none; + background-color: transparent; + color: $white100; + + svg { + width: 1.25rem; + height: 1.25rem; + } + + &:hover { + cursor: pointer; + + color: $green; + } +} +.tool-btn--evil { + color: $red; + + &:hover { + color: $white100; + } +} \ No newline at end of file diff --git a/gallery/user/themes/default/style.scss b/gallery/user/themes/default/style.scss index bb45199..b3bf8ce 100644 --- a/gallery/user/themes/default/style.scss +++ b/gallery/user/themes/default/style.scss @@ -5,6 +5,7 @@ @import 'ui/nav'; @import 'ui/main'; +@import 'buttons/img-tool'; @import 'buttons/btn'; @import 'buttons/up'; @@ -28,6 +29,31 @@ z-index: 1; overflow: unset; + > h1 { + margin: 0 auto; + padding: 0; + + font-family: $font-header; + font-size: 5rem; + font-weight: 900; + line-height: 1; + text-align: center; + + color: $green; + } + > p { + margin: 0 auto; + padding: 0; + + font-family: $font-body; + font-size: 2rem; + font-weight: 600; + line-height: 1; + text-align: center; + + color: $white100; + } + /* h1 { margin: 0; padding: 0; @@ -39,6 +65,7 @@ color: $green; } + */ } @keyframes imgLoading { @@ -60,7 +87,7 @@ gap: 0; - h1 { + > h1 { margin: 0 auto; padding: 0; @@ -72,7 +99,7 @@ color: $green; } - p { + > p { margin: 0 auto; padding: 0; @@ -231,12 +258,16 @@ padding: 0; max-width: 100%; + height: 100%; max-height: 75vh; + background: linear-gradient(-45deg, $black100, $black400 40%, $black100); + background-size: 400% 400%; + border-radius: $rad; + animation: imgLoading 10s ease infinite; + object-fit: contain; object-position: center; - - border-radius: $rad; } } @@ -285,3 +316,24 @@ overflow: hidden; } } + +.img-tools { + width: 100%; + height: 2rem; + + display: flex; + justify-content: center; + align-items: center; + gap: 0.5rem; + + > div { + margin: 0; + padding: 0; + + display: flex; + gap: 0.5rem; + + background-color: $black200; + border-radius: $rad; + } +} \ No newline at end of file diff --git a/setup.py b/setup.py index 8343084..f69f639 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages, setup setup( name='onlylegs', - version='100123', + version='110123', packages=find_packages(), include_package_data=True, install_requires=[