Switch to SQLAlchemy for database management

Add Favicon
This commit is contained in:
Michał Gdula 2023-03-03 00:26:46 +00:00
parent 9e87c74c96
commit 2e86bfa072
11 changed files with 341 additions and 192 deletions

View file

@ -3,23 +3,22 @@ from werkzeug.exceptions import abort
from werkzeug.utils import secure_filename
from gallery.auth import login_required
from gallery.db import get_db
from . import db
from sqlalchemy.orm import sessionmaker
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
from . import metadata as mt
from PIL import Image
import os
from datetime import datetime
dt = datetime.now()
blueprint = Blueprint('gallery', __name__)
@blueprint.route('/')
def index():
db = get_db()
images = db.execute('SELECT * FROM posts'
' ORDER BY created_at DESC').fetchall()
images = db_session.query(db.posts).order_by(db.posts.id.desc()).all()
return render_template('index.html',
images=images,
@ -30,17 +29,15 @@ def index():
@blueprint.route('/image/<int:id>')
def image(id):
# Get image from database
db = get_db()
image = db.execute('SELECT * FROM posts WHERE id = ?', (id, )).fetchone()
img = db_session.query(db.posts).filter_by(id=id).first()
if image is None:
if img is None:
abort(404)
exif = mt.metadata.yoink(
os.path.join(current_app.config['UPLOAD_FOLDER'], image['file_name']))
os.path.join(current_app.config['UPLOAD_FOLDER'], img.file_name))
return render_template('image.html', image=image, exif=exif)
return render_template('image.html', image=img, exif=exif)
@blueprint.route('/group')