python-gallery/gallery/views/image.py

92 lines
2.6 KiB
Python
Raw Normal View History

2023-04-06 16:22:56 +00:00
"""
Onlylegs - Image View
"""
2023-04-06 18:42:04 +00:00
from math import ceil
2023-04-07 09:18:03 +00:00
from flask import Blueprint, abort, render_template, url_for, current_app
2023-04-06 16:22:56 +00:00
from gallery.models import Posts, Users, GroupJunction, Groups
2023-04-06 16:22:56 +00:00
2023-04-07 12:35:30 +00:00
blueprint = Blueprint("image", __name__, url_prefix="/image")
2023-04-06 16:22:56 +00:00
2023-04-07 12:35:30 +00:00
@blueprint.route("/<int:image_id>")
2023-04-06 16:22:56 +00:00
def image(image_id):
"""
Image view, shows the image and its metadata
"""
# Get the image, if it doesn't exist, 404
image = Posts.query.filter(Posts.id == image_id).first()
2023-04-06 16:22:56 +00:00
if not image:
2023-04-07 12:35:30 +00:00
abort(404, "Image not found :<")
2023-04-06 16:22:56 +00:00
# Get the image's author username
2023-04-07 12:35:30 +00:00
image.author_username = (
Users.query.with_entities(Users.username)
.filter(Users.id == image.author_id)
2023-04-07 12:35:30 +00:00
.first()[0]
)
2023-04-06 16:22:56 +00:00
# Get the image's groups
2023-04-07 12:35:30 +00:00
groups = (
GroupJunction.query.with_entities(GroupJunction.group_id)
.filter(GroupJunction.post_id == image_id)
2023-04-07 12:35:30 +00:00
.all()
)
2023-04-06 16:22:56 +00:00
# For each group, get the group data and add it to the image item
image.groups = []
for group in groups:
2023-04-07 12:35:30 +00:00
image.groups.append(
Groups.query.with_entities(Groups.name, Groups.id)
.filter(Groups.id == group[0])
2023-04-07 12:35:30 +00:00
.first()
)
2023-04-06 16:22:56 +00:00
# Get the next and previous images
# Check if there is a group ID set
2023-04-07 12:35:30 +00:00
next_url = (
Posts.query.with_entities(Posts.id)
.filter(Posts.id > image_id)
.order_by(Posts.id.asc())
2023-04-07 12:35:30 +00:00
.first()
)
prev_url = (
Posts.query.with_entities(Posts.id)
.filter(Posts.id < image_id)
.order_by(Posts.id.desc())
2023-04-07 12:35:30 +00:00
.first()
)
2023-04-06 16:22:56 +00:00
# If there is a next or previous image, get the url
if next_url:
2023-04-07 12:35:30 +00:00
next_url = url_for("image.image", image_id=next_url[0])
2023-04-06 16:22:56 +00:00
if prev_url:
2023-04-07 12:35:30 +00:00
prev_url = url_for("image.image", image_id=prev_url[0])
2023-04-06 18:42:04 +00:00
# Yoink all the images in the database
total_images = Posts.query.with_entities(Posts.id).order_by(Posts.id.desc()).all()
2023-04-07 12:35:30 +00:00
limit = current_app.config["UPLOAD_CONF"]["max-load"]
2023-04-06 18:42:04 +00:00
# If the number of items is less than the limit, no point of calculating the page
if len(total_images) <= limit:
2023-04-07 09:18:03 +00:00
return_page = None
2023-04-06 18:42:04 +00:00
else:
# How many pages should there be
for i in range(ceil(len(total_images) / limit)):
# Slice the list of IDs into chunks of the limit
2023-04-07 12:35:30 +00:00
for j in total_images[i * limit : (i + 1) * limit]:
2023-04-06 18:42:04 +00:00
# Is our image in this chunk?
2023-04-10 23:09:39 +00:00
if image_id < j[-1]:
2023-04-06 18:42:04 +00:00
return_page = i + 1
break
2023-04-06 16:22:56 +00:00
2023-04-07 12:35:30 +00:00
return render_template(
"image.html",
image=image,
next_url=next_url,
prev_url=prev_url,
return_page=return_page,
)