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
|
|
|
|
|
|
|
|
|
|
|
blueprint = Blueprint('group', __name__, url_prefix='/group')
|
|
|
|
db_session = sessionmaker(bind=db.engine)
|
|
|
|
db_session = db_session()
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/', methods=['GET'])
|
|
|
|
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:
|
|
|
|
group.author_username = db_session.query(db.Users.username)\
|
|
|
|
.filter(db.Users.id == group.author_id)\
|
2023-04-01 16:16:07 +00:00
|
|
|
.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-01 17:40:42 +00:00
|
|
|
images = db_session.query(db.GroupJunction.post_id)\
|
|
|
|
.filter(db.GroupJunction.group_id == group.id)\
|
2023-03-20 17:57:47 +00:00
|
|
|
.order_by(db.GroupJunction.date_added.desc())\
|
2023-04-01 16:16:07 +00:00
|
|
|
.limit(3)
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
group.images.append(db_session.query(db.Posts.file_name, db.Posts.post_alt,
|
|
|
|
db.Posts.image_colours, db.Posts.id)\
|
|
|
|
.filter(db.Posts.id == image[0])\
|
|
|
|
.first())
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
return render_template('groups/list.html', groups=groups)
|
2023-03-09 23:31:58 +00:00
|
|
|
|
2023-03-12 15:52:23 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
@blueprint.route('/<int:group_id>')
|
|
|
|
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
|
|
|
|
group = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
|
|
|
|
if group is None:
|
2023-03-12 15:52:23 +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
|
|
|
|
group.author_username = db_session.query(db.Users.username)\
|
|
|
|
.filter(db.Users.id == group.author_id)\
|
2023-03-20 17:57:47 +00:00
|
|
|
.first()[0]
|
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get all images in the group from the junction table
|
|
|
|
junction = db_session.query(db.GroupJunction.post_id)\
|
2023-03-20 17:57:47 +00:00
|
|
|
.filter(db.GroupJunction.group_id == group_id)\
|
|
|
|
.order_by(db.GroupJunction.date_added.desc())\
|
|
|
|
.all()
|
|
|
|
|
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-03-09 23:31:58 +00:00
|
|
|
image = db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
|
|
|
|
images.append(image)
|
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
|
|
|
|
text_colour = 'rgb(var(--fg-black))'
|
|
|
|
if images[0]:
|
2023-03-26 23:34:03 +00:00
|
|
|
text_colour = contrast.contrast(images[0].image_colours[0],
|
|
|
|
'rgb(var(--fg-black))',
|
|
|
|
'rgb(var(--fg-white))')
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-03-26 23:34:03 +00:00
|
|
|
return render_template('groups/group.html',
|
2023-04-01 17:40:42 +00:00
|
|
|
group=group,
|
2023-03-26 23:34:03 +00:00
|
|
|
images=images,
|
|
|
|
text_colour=text_colour)
|
2023-03-09 23:31:58 +00:00
|
|
|
|
2023-03-12 15:52:23 +00:00
|
|
|
|
2023-03-09 23:31:58 +00:00
|
|
|
@blueprint.route('/<int:group_id>/<int:image_id>')
|
|
|
|
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
|
|
|
|
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
|
|
|
|
if image is None:
|
2023-03-09 23:31:58 +00:00
|
|
|
abort(404, 'Image not found')
|
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get the image's author username
|
|
|
|
image.author_username = db_session.query(db.Users.username)\
|
|
|
|
.filter(db.Users.id == image.author_id)\
|
2023-03-20 17:57:47 +00:00
|
|
|
.first()[0]
|
|
|
|
|
2023-04-01 17:40:42 +00:00
|
|
|
# Get all groups the image is in
|
2023-04-01 16:16:07 +00:00
|
|
|
groups = db_session.query(db.GroupJunction.group_id)\
|
2023-03-20 17:57:47 +00:00
|
|
|
.filter(db.GroupJunction.post_id == image_id)\
|
|
|
|
.all()
|
|
|
|
|
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-01 17:40:42 +00:00
|
|
|
group = db_session.query(db.Groups.id, db.Groups.name)\
|
|
|
|
.filter(db.Groups.id == group[0])\
|
|
|
|
.first()
|
|
|
|
image.groups.append(group)
|
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-03-20 17:57:47 +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-04-01 17:40:42 +00:00
|
|
|
# If there is a next or previous image, get the URL for it
|
2023-03-11 22:14:03 +00:00
|
|
|
if next_url is not None:
|
|
|
|
next_url = url_for('group.group_post', group_id=group_id, image_id=next_url[0])
|
|
|
|
if prev_url is not None:
|
|
|
|
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-01 17:40:42 +00:00
|
|
|
return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)
|