mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-06-29 11:36:16 +00:00
Switch to Flask-SQLAlchemy
Add FLask-Migrate for next step in the Migration 😉
This commit is contained in:
parent
7d0078ea9a
commit
7c553e99b8
12 changed files with 368 additions and 403 deletions
|
@ -5,14 +5,11 @@ sounds more limiting that it actually is in this gallery
|
|||
"""
|
||||
from flask import Blueprint, abort, render_template, url_for
|
||||
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from gallery import db
|
||||
from gallery.models import Posts, Users, GroupJunction, Groups
|
||||
from gallery.utils import contrast
|
||||
|
||||
|
||||
blueprint = Blueprint("group", __name__, url_prefix="/group")
|
||||
db_session = sessionmaker(bind=db.engine)
|
||||
db_session = db_session()
|
||||
|
||||
|
||||
@blueprint.route("/", methods=["GET"])
|
||||
|
@ -20,21 +17,21 @@ def groups():
|
|||
"""
|
||||
Group overview, shows all image groups
|
||||
"""
|
||||
groups = db_session.query(db.Groups).all()
|
||||
groups = Groups.query.all()
|
||||
|
||||
# For each group, get the 3 most recent images
|
||||
for group in groups:
|
||||
group.author_username = (
|
||||
db_session.query(db.Users.username)
|
||||
.filter(db.Users.id == group.author_id)
|
||||
Users.query.with_entities(Users.username)
|
||||
.filter(Users.id == group.author_id)
|
||||
.first()[0]
|
||||
)
|
||||
|
||||
# Get the 3 most recent images
|
||||
images = (
|
||||
db_session.query(db.GroupJunction.post_id)
|
||||
.filter(db.GroupJunction.group_id == group.id)
|
||||
.order_by(db.GroupJunction.date_added.desc())
|
||||
GroupJunction.query.with_entities(GroupJunction.post_id)
|
||||
.filter(GroupJunction.group_id == group.id)
|
||||
.order_by(GroupJunction.date_added.desc())
|
||||
.limit(3)
|
||||
)
|
||||
|
||||
|
@ -42,10 +39,10 @@ def groups():
|
|||
group.images = []
|
||||
for image in images:
|
||||
group.images.append(
|
||||
db_session.query(
|
||||
db.Posts.filename, db.Posts.alt, db.Posts.colours, db.Posts.id
|
||||
Posts.query.with_entities(
|
||||
Posts.filename, Posts.alt, Posts.colours, Posts.id
|
||||
)
|
||||
.filter(db.Posts.id == image[0])
|
||||
.filter(Posts.id == image[0])
|
||||
.first()
|
||||
)
|
||||
|
||||
|
@ -58,32 +55,30 @@ def group(group_id):
|
|||
Group view, shows all images in a group
|
||||
"""
|
||||
# Get the group, if it doesn't exist, 404
|
||||
group = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
|
||||
group = Groups.query.filter(Groups.id == group_id).first()
|
||||
|
||||
if group is None:
|
||||
abort(404, "Group not found! D:")
|
||||
|
||||
# Get the group's author username
|
||||
group.author_username = (
|
||||
db_session.query(db.Users.username)
|
||||
.filter(db.Users.id == group.author_id)
|
||||
Users.query.with_entities(Users.username)
|
||||
.filter(Users.id == group.author_id)
|
||||
.first()[0]
|
||||
)
|
||||
|
||||
# Get all images in the group from the junction table
|
||||
junction = (
|
||||
db_session.query(db.GroupJunction.post_id)
|
||||
.filter(db.GroupJunction.group_id == group_id)
|
||||
.order_by(db.GroupJunction.date_added.desc())
|
||||
GroupJunction.query.with_entities(GroupJunction.post_id)
|
||||
.filter(GroupJunction.group_id == group_id)
|
||||
.order_by(GroupJunction.date_added.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
# Get the image data for each image in the group
|
||||
images = []
|
||||
for image in junction:
|
||||
images.append(
|
||||
db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
|
||||
)
|
||||
images.append(Posts.query.filter(Posts.id == image[0]).first())
|
||||
|
||||
# Check contrast for the first image in the group for the banner
|
||||
text_colour = "rgb(var(--fg-black))"
|
||||
|
@ -103,21 +98,21 @@ def group_post(group_id, image_id):
|
|||
Image view, shows the image and its metadata from a specific group
|
||||
"""
|
||||
# Get the image, if it doesn't exist, 404
|
||||
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
|
||||
image = Posts.query.filter(Posts.id == image_id).first()
|
||||
if image is None:
|
||||
abort(404, "Image not found")
|
||||
|
||||
# Get the image's author username
|
||||
image.author_username = (
|
||||
db_session.query(db.Users.username)
|
||||
.filter(db.Users.id == image.author_id)
|
||||
Users.query.with_entities(Users.username)
|
||||
.filter(Users.id == image.author_id)
|
||||
.first()[0]
|
||||
)
|
||||
|
||||
# Get all groups the image is in
|
||||
groups = (
|
||||
db_session.query(db.GroupJunction.group_id)
|
||||
.filter(db.GroupJunction.post_id == image_id)
|
||||
GroupJunction.query.with_entities(GroupJunction.group_id)
|
||||
.filter(GroupJunction.post_id == image_id)
|
||||
.all()
|
||||
)
|
||||
|
||||
|
@ -125,24 +120,24 @@ def group_post(group_id, image_id):
|
|||
image.groups = []
|
||||
for group in groups:
|
||||
image.groups.append(
|
||||
db_session.query(db.Groups.id, db.Groups.name)
|
||||
.filter(db.Groups.id == group[0])
|
||||
Groups.query.with_entities(Groups.id, Groups.name)
|
||||
.filter(Groups.id == group[0])
|
||||
.first()
|
||||
)
|
||||
|
||||
# Get the next and previous images in the group
|
||||
next_url = (
|
||||
db_session.query(db.GroupJunction.post_id)
|
||||
.filter(db.GroupJunction.group_id == group_id)
|
||||
.filter(db.GroupJunction.post_id > image_id)
|
||||
.order_by(db.GroupJunction.date_added.asc())
|
||||
GroupJunction.query.with_entities(GroupJunction.post_id)
|
||||
.filter(GroupJunction.group_id == group_id)
|
||||
.filter(GroupJunction.post_id > image_id)
|
||||
.order_by(GroupJunction.date_added.asc())
|
||||
.first()
|
||||
)
|
||||
prev_url = (
|
||||
db_session.query(db.GroupJunction.post_id)
|
||||
.filter(db.GroupJunction.group_id == group_id)
|
||||
.filter(db.GroupJunction.post_id < image_id)
|
||||
.order_by(db.GroupJunction.date_added.desc())
|
||||
GroupJunction.query.with_entities(GroupJunction.post_id)
|
||||
.filter(GroupJunction.group_id == group_id)
|
||||
.filter(GroupJunction.post_id < image_id)
|
||||
.order_by(GroupJunction.date_added.desc())
|
||||
.first()
|
||||
)
|
||||
|
||||
|
|
|
@ -5,13 +5,10 @@ from math import ceil
|
|||
|
||||
from flask import Blueprint, abort, render_template, url_for, current_app
|
||||
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from gallery import db
|
||||
from gallery.models import Posts, Users, GroupJunction, Groups
|
||||
|
||||
|
||||
blueprint = Blueprint("image", __name__, url_prefix="/image")
|
||||
db_session = sessionmaker(bind=db.engine)
|
||||
db_session = db_session()
|
||||
|
||||
|
||||
@blueprint.route("/<int:image_id>")
|
||||
|
@ -20,21 +17,21 @@ def image(image_id):
|
|||
Image view, shows the image and its metadata
|
||||
"""
|
||||
# Get the image, if it doesn't exist, 404
|
||||
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
|
||||
image = Posts.query.filter(Posts.id == image_id).first()
|
||||
if not image:
|
||||
abort(404, "Image not found :<")
|
||||
|
||||
# Get the image's author username
|
||||
image.author_username = (
|
||||
db_session.query(db.Users.username)
|
||||
.filter(db.Users.id == image.author_id)
|
||||
Users.query.with_entities(Users.username)
|
||||
.filter(Users.id == image.author_id)
|
||||
.first()[0]
|
||||
)
|
||||
|
||||
# Get the image's groups
|
||||
groups = (
|
||||
db_session.query(db.GroupJunction.group_id)
|
||||
.filter(db.GroupJunction.post_id == image_id)
|
||||
GroupJunction.query.with_entities(GroupJunction.group_id)
|
||||
.filter(GroupJunction.post_id == image_id)
|
||||
.all()
|
||||
)
|
||||
|
||||
|
@ -42,23 +39,23 @@ def image(image_id):
|
|||
image.groups = []
|
||||
for group in groups:
|
||||
image.groups.append(
|
||||
db_session.query(db.Groups.id, db.Groups.name)
|
||||
.filter(db.Groups.id == group[0])
|
||||
Groups.query.with_entities(Groups.name, Groups.id)
|
||||
.filter(Groups.id == group[0])
|
||||
.first()
|
||||
)
|
||||
|
||||
# Get the next and previous images
|
||||
# Check if there is a group ID set
|
||||
next_url = (
|
||||
db_session.query(db.Posts.id)
|
||||
.filter(db.Posts.id > image_id)
|
||||
.order_by(db.Posts.id.asc())
|
||||
Posts.query.with_entities(Posts.id)
|
||||
.filter(Posts.id > image_id)
|
||||
.order_by(Posts.id.asc())
|
||||
.first()
|
||||
)
|
||||
prev_url = (
|
||||
db_session.query(db.Posts.id)
|
||||
.filter(db.Posts.id < image_id)
|
||||
.order_by(db.Posts.id.desc())
|
||||
Posts.query.with_entities(Posts.id)
|
||||
.filter(Posts.id < image_id)
|
||||
.order_by(Posts.id.desc())
|
||||
.first()
|
||||
)
|
||||
|
||||
|
@ -69,7 +66,7 @@ def image(image_id):
|
|||
prev_url = url_for("image.image", image_id=prev_url[0])
|
||||
|
||||
# Yoink all the images in the database
|
||||
total_images = db_session.query(db.Posts.id).order_by(db.Posts.id.desc()).all()
|
||||
total_images = Posts.query.with_entities(Posts.id).order_by(Posts.id.desc()).all()
|
||||
limit = current_app.config["UPLOAD_CONF"]["max-load"]
|
||||
|
||||
# If the number of items is less than the limit, no point of calculating the page
|
||||
|
|
|
@ -6,13 +6,10 @@ from math import ceil
|
|||
from flask import Blueprint, render_template, request, current_app
|
||||
from werkzeug.exceptions import abort
|
||||
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from gallery import db
|
||||
from gallery.models import Posts
|
||||
|
||||
|
||||
blueprint = Blueprint("gallery", __name__)
|
||||
db_session = sessionmaker(bind=db.engine)
|
||||
db_session = db_session()
|
||||
|
||||
|
||||
@blueprint.route("/")
|
||||
|
@ -30,7 +27,7 @@ def index():
|
|||
|
||||
# get the total number of images in the database
|
||||
# calculate the total number of pages, and make sure the page number is valid
|
||||
total_images = db_session.query(db.Posts.id).count()
|
||||
total_images = Posts.query.with_entities(Posts.id).count()
|
||||
pages = ceil(max(total_images, limit) / limit)
|
||||
if page > pages:
|
||||
abort(
|
||||
|
@ -41,14 +38,10 @@ def index():
|
|||
|
||||
# get the images for the current page
|
||||
images = (
|
||||
db_session.query(
|
||||
db.Posts.filename,
|
||||
db.Posts.alt,
|
||||
db.Posts.colours,
|
||||
db.Posts.created_at,
|
||||
db.Posts.id,
|
||||
Posts.query.with_entities(
|
||||
Posts.filename, Posts.alt, Posts.colours, Posts.created_at, Posts.id
|
||||
)
|
||||
.order_by(db.Posts.id.desc())
|
||||
.order_by(Posts.id.desc())
|
||||
.offset((page - 1) * limit)
|
||||
.limit(limit)
|
||||
.all()
|
||||
|
|
|
@ -5,13 +5,10 @@ from flask import Blueprint, render_template, request
|
|||
from werkzeug.exceptions import abort
|
||||
from flask_login import current_user
|
||||
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from gallery import db
|
||||
from gallery.models import Posts, Users
|
||||
|
||||
|
||||
blueprint = Blueprint("profile", __name__, url_prefix="/profile")
|
||||
db_session = sessionmaker(bind=db.engine)
|
||||
db_session = db_session()
|
||||
|
||||
|
||||
@blueprint.route("/profile")
|
||||
|
@ -29,11 +26,11 @@ def profile():
|
|||
abort(404, "You must be logged in to view your own profile!")
|
||||
|
||||
# Get the user's data
|
||||
user = db_session.query(db.Users).filter(db.Users.id == user_id).first()
|
||||
user = Users.query.filter(Users.id == user_id).first()
|
||||
|
||||
if not user:
|
||||
abort(404, "User not found :c")
|
||||
|
||||
images = db_session.query(db.Posts).filter(db.Posts.author_id == user_id).all()
|
||||
images = Posts.query.filter(Posts.author_id == user_id).all()
|
||||
|
||||
return render_template("profile.html", user=user, images=images)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue