2023-03-09 23:31:58 +00:00
|
|
|
"""
|
2023-03-11 22:14:03 +00:00
|
|
|
Onlylegs - Image Groups
|
2023-03-20 17:57:47 +00:00
|
|
|
Why groups? Because I don't like calling these albums
|
|
|
|
sounds more limiting that it actually is in this gallery
|
2023-03-09 23:31:58 +00:00
|
|
|
"""
|
2023-03-20 17:57:47 +00:00
|
|
|
from flask import Blueprint, abort, render_template, url_for
|
2023-03-09 23:31:58 +00:00
|
|
|
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
2023-03-12 15:52:23 +00:00
|
|
|
from gallery import db
|
2023-03-26 01:04:13 +00:00
|
|
|
from gallery.utils import contrast
|
2023-03-09 23:31:58 +00:00
|
|
|
|
|
|
|
|
2023-04-07 12:35:30 +00:00
|
|
|
blueprint = Blueprint("group", __name__, url_prefix="/group")
|
2023-03-09 23:31:58 +00:00
|
|
|
db_session = sessionmaker(bind=db.engine)
|
|
|
|
db_session = db_session()
|
|
|
|
|
|
|
|
|
2023-04-07 12:35:30 +00:00
|
|
|
@blueprint.route("/", methods=["GET"])
|
2023-03-09 23:31:58 +00:00
|
|
|
def groups():
|
|
|
|
"""
|
|
|
|
Group overview, shows all image groups
|
|
|
|
"""
|
2023-04-01 17:40:42 +00:00
|
|
|
groups = db_session.query(db.Groups).all()
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 16:16:07 +00:00
|
|
|
# For each group, get the 3 most recent images
|
2023-04-01 17:40:42 +00:00
|
|
|
for group in groups:
|
2023-04-07 12:35:30 +00:00
|
|
|
group.author_username = (
|
|
|
|
db_session.query(db.Users.username)
|
|
|
|
.filter(db.Users.id == group.author_id)
|
|
|
|
.first()[0]
|
|
|
|
)
|
2023-04-01 17:40:42 +00:00
|
|
|
|
2023-04-01 16:16:07 +00:00
|
|
|
# Get the 3 most recent images
|
2023-04-07 12:35:30 +00:00
|
|
|
images = (
|
|
|
|
db_session.query(db.GroupJunction.post_id)
|
|
|
|
.filter(db.GroupJunction.group_id == group.id)
|
|
|
|
.order_by(db.GroupJunction.date_added.desc())
|
|
|
|
.limit(3)
|
|
|
|
)
|
2023-04-01 16:16:07 +00:00
|
|
|
|
|
|
|
# For each image, get the image data and add it to the group item
|
2023-04-01 17:40:42 +00:00
|
|
|
group.images = []
|
|
|
|
for image in images:
|
2023-04-07 12:35:30 +00:00
|
|
|
group.images.append(
|
|
|
|
db_session.query(
|
|
|
|
db.Posts.filename, db.Posts.alt, db.Posts.colours, db.Posts.id
|
|
|
|
)
|
|
|
|
.filter(db.Posts.id == image[0])
|
|
|
|
.first()
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-07 12:35:30 +00:00
|
|
|
return render_template("list.html", groups=groups)
|
2023-03-09 23:31:58 +00:00
|
|
|
|
2023-03-12 15:52:23 +00:00
|
|
|
|
2023-04-07 12:35:30 +00:00
|
|
|
@blueprint.route("/<int:group_id>")
|
2023-03-09 23:31:58 +00:00
|
|
|
def group(group_id):
|
|
|
|
"""
|
|
|
|
Group view, shows all images in a group
|
|
|
|
"""
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the group, if it doesn't exist, 404
|
2023-04-07 12:35:30 +00:00
|
|
|
group = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
|
2023-04-02 17:00:32 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
if group is None:
|
2023-04-07 12:35:30 +00:00
|
|
|
abort(404, "Group not found! D:")
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the group's author username
|
2023-04-07 12:35:30 +00:00
|
|
|
group.author_username = (
|
|
|
|
db_session.query(db.Users.username)
|
|
|
|
.filter(db.Users.id == group.author_id)
|
|
|
|
.first()[0]
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get all images in the group from the junction table
|
2023-04-07 12:35:30 +00:00
|
|
|
junction = (
|
|
|
|
db_session.query(db.GroupJunction.post_id)
|
|
|
|
.filter(db.GroupJunction.group_id == group_id)
|
|
|
|
.order_by(db.GroupJunction.date_added.desc())
|
|
|
|
.all()
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the image data for each image in the group
|
2023-03-09 23:31:58 +00:00
|
|
|
images = []
|
2023-04-01 17:40:42 +00:00
|
|
|
for image in junction:
|
2023-04-07 12:35:30 +00:00
|
|
|
images.append(
|
|
|
|
db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
|
|
|
|
)
|
2023-03-26 23:34:03 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Check contrast for the first image in the group for the banner
|
2023-04-07 12:35:30 +00:00
|
|
|
text_colour = "rgb(var(--fg-black))"
|
2023-04-02 21:15:24 +00:00
|
|
|
if images:
|
2023-04-07 12:35:30 +00:00
|
|
|
text_colour = contrast.contrast(
|
|
|
|
images[0].colours[0], "rgb(var(--fg-black))", "rgb(var(--fg-white))"
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-07 12:35:30 +00:00
|
|
|
return render_template(
|
|
|
|
"group.html", group=group, images=images, text_colour=text_colour
|
|
|
|
)
|
2023-03-09 23:31:58 +00:00
|
|
|
|
2023-03-12 15:52:23 +00:00
|
|
|
|
2023-04-07 12:35:30 +00:00
|
|
|
@blueprint.route("/<int:group_id>/<int:image_id>")
|
2023-03-09 23:31:58 +00:00
|
|
|
def group_post(group_id, image_id):
|
|
|
|
"""
|
|
|
|
Image view, shows the image and its metadata from a specific group
|
|
|
|
"""
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the image, if it doesn't exist, 404
|
2023-04-07 12:35:30 +00:00
|
|
|
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
|
2023-04-01 17:40:42 +00:00
|
|
|
if image is None:
|
2023-04-07 12:35:30 +00:00
|
|
|
abort(404, "Image not found")
|
2023-03-09 23:31:58 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the image's author username
|
2023-04-07 12:35:30 +00:00
|
|
|
image.author_username = (
|
|
|
|
db_session.query(db.Users.username)
|
|
|
|
.filter(db.Users.id == image.author_id)
|
|
|
|
.first()[0]
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get all groups the image is in
|
2023-04-07 12:35:30 +00:00
|
|
|
groups = (
|
|
|
|
db_session.query(db.GroupJunction.group_id)
|
|
|
|
.filter(db.GroupJunction.post_id == image_id)
|
|
|
|
.all()
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the group data for each group the image is in
|
|
|
|
image.groups = []
|
2023-04-01 16:16:07 +00:00
|
|
|
for group in groups:
|
2023-04-07 12:35:30 +00:00
|
|
|
image.groups.append(
|
|
|
|
db_session.query(db.Groups.id, db.Groups.name)
|
|
|
|
.filter(db.Groups.id == group[0])
|
|
|
|
.first()
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the next and previous images in the group
|
2023-04-07 12:35:30 +00:00
|
|
|
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())
|
|
|
|
.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())
|
|
|
|
.first()
|
|
|
|
)
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# If there is a next or previous image, get the URL for it
|
2023-04-02 16:36:05 +00:00
|
|
|
if next_url:
|
2023-04-07 12:35:30 +00:00
|
|
|
next_url = url_for("group.group_post", group_id=group_id, image_id=next_url[0])
|
2023-04-02 16:36:05 +00:00
|
|
|
if prev_url:
|
2023-04-07 12:35:30 +00:00
|
|
|
prev_url = url_for("group.group_post", group_id=group_id, image_id=prev_url[0])
|
2023-03-09 23:31:58 +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
|
|
|
|
)
|