diff --git a/gallery/__init__.py b/gallery/__init__.py index d2cabfa..ea5c7e1 100644 --- a/gallery/__init__.py +++ b/gallery/__init__.py @@ -26,11 +26,13 @@ def create_app(test_config=None): # Get environment variables load_dotenv(os.path.join(app.root_path, 'user', '.env')) - + + # App configuration app.config.from_mapping( SECRET_KEY=os.environ.get('FLASK_SECRET'), DATABASE=os.path.join(app.instance_path, 'gallery.sqlite'), UPLOAD_FOLDER=os.path.join(app.root_path, 'user', 'uploads'), + ALLOWED_EXTENSIONS=['png', 'jpg', 'jpeg', 'webp'], ) if test_config is None: @@ -91,14 +93,13 @@ def create_app(test_config=None): from . import auth app.register_blueprint(auth.blueprint) - # Load apis - from . import api - app.register_blueprint(api.blueprint) - # Load routes for home and images from . import gallery app.register_blueprint(gallery.blueprint) app.add_url_rule('/', endpoint='index') - + + # Load APIs + from . import api + app.register_blueprint(api.blueprint) return app \ No newline at end of file diff --git a/gallery/api.py b/gallery/api.py index b0fbacc..5b87e7d 100644 --- a/gallery/api.py +++ b/gallery/api.py @@ -1,28 +1,13 @@ -import functools -from flask import ( - Blueprint, flash, g, redirect, render_template, request, session, url_for, abort, jsonify, send_from_directory -) -from werkzeug.security import check_password_hash, generate_password_hash +from flask import Blueprint, render_template, current_app, send_from_directory from werkzeug.utils import secure_filename -from gallery.db import get_db +import os -blueprint = Blueprint('api', __name__, url_prefix='/api') +blueprint = Blueprint('viewsbp', __name__, url_prefix='/') -@blueprint.route('/uploads//', methods=['POST']) -def uploads(quality, request_file): - if request.method != 'POST': - abort(405) +@blueprint.route('/uploads//') +def uploads(quality, file): + dir = os.path.join(current_app.config['UPLOAD_FOLDER'], secure_filename(quality)) + file = secure_filename(file) - #quality = secure_filename(quality) - #quality_dir = os.path.join(app.config['UPLOAD_FOLDER'], quality) - #if not os.path.isdir(quality_dir): - # abort(404) - - #request_file = secure_filename(request_file) - - #if not os.path.isfile(os.path.join(quality_dir, request_file)): - # abort(404) - - #return send_from_directory(quality_dir, request_file) - abort(404) \ No newline at end of file + return send_from_directory(dir, file, as_attachment=True) \ No newline at end of file diff --git a/gallery/auth.py b/gallery/auth.py index 6371e77..295a250 100644 --- a/gallery/auth.py +++ b/gallery/auth.py @@ -1,7 +1,5 @@ import functools -from flask import ( - Blueprint, flash, g, redirect, render_template, request, session, url_for -) +from flask import Blueprint, flash, g, redirect, render_template, request, session, url_for from werkzeug.security import check_password_hash, generate_password_hash from gallery.db import get_db diff --git a/gallery/gallery.py b/gallery/gallery.py index 1b943b3..1b9a991 100644 --- a/gallery/gallery.py +++ b/gallery/gallery.py @@ -1,30 +1,38 @@ -from flask import ( - Blueprint, flash, g, redirect, render_template, request, url_for, jsonify, current_app -) +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 json import os +import datetime +dt = datetime.datetime.now() + blueprint = Blueprint('gallery', __name__) @blueprint.route('/') def index(): - return render_template('index.html') + db = get_db() + images = db.execute( + 'SELECT * FROM posts' + ' ORDER BY created_at DESC' + ).fetchall() + + return render_template('index.html', images=images) -@blueprint.route('/image/') -def image(request_id): - # Check if request_id is valid - try: - request_id = int(request_id) - except ValueError: +@blueprint.route('/image/') +def image(id): + db = get_db() + post = db.execute( + 'SELECT * FROM posts' + ' WHERE id = ?', + (id,) + ).fetchone() + + if post is None: abort(404) - result = onlylegsDB.getImage(request_id) - - return render_template('image.html', fileName=result[1], id=request_id) + return render_template('image.html', fileName=post['file_name'], id=id) @blueprint.route('/group') @@ -46,10 +54,19 @@ def upload(): if not file: flash('No selected file') return abort(404) + if secure_filename(file.filename).lower().split('.')[-1] in current_app.config['ALLOWED_EXTENSIONS']: + file_name = f"GWAGWA_{dt.year}{dt.month}{dt.day}-{dt.microsecond}.{secure_filename(file.filename).lower().split('.')[-1]}" + file.save(os.path.join(current_app.config['UPLOAD_FOLDER']+'/original', file_name)) + + db = get_db() + db.execute( + 'INSERT INTO posts (file_name, author_id, description, alt)' + ' VALUES (?, ?, ?, ?)', + (file_name, g.user['id'], form['description'], form['alt']) + ) + db.commit() - file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], secure_filename(file.filename))) - - return json.dumps({'filename': secure_filename(file.filename), 'form': form}) + return 'Gwa Gwa' return render_template('upload.html') diff --git a/gallery/templates/image.html b/gallery/templates/image.html index cac5b52..93d9742 100644 --- a/gallery/templates/image.html +++ b/gallery/templates/image.html @@ -1,13 +1,17 @@ {% extends 'layout.html' %} {% block header %} -leaves + leaves {% endblock %} {% block content %}
- +

{{ fileName }}

diff --git a/gallery/templates/index.html b/gallery/templates/index.html index afa222b..0e655f4 100644 --- a/gallery/templates/index.html +++ b/gallery/templates/index.html @@ -7,7 +7,18 @@ {% block content %}

Gallery

- +
diff --git a/gallery/user/themes/default/style.scss b/gallery/user/themes/default/style.scss index 9fbce62..6c94b7a 100644 --- a/gallery/user/themes/default/style.scss +++ b/gallery/user/themes/default/style.scss @@ -68,6 +68,7 @@ font-size: 5rem; font-weight: 900; line-height: 1; + text-align: center; color: $green; } @@ -79,6 +80,7 @@ font-size: 2rem; font-weight: 600; line-height: 1; + text-align: center; color: $white100; } @@ -208,7 +210,7 @@ } .image__container { - margin: 0; + margin: -40vh 0 0 0; padding: 0; width: 100%;