mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-01-18 04:18:35 +00:00
Michał
0e24004c0b
Fix image view from a group not showing the groups its in Make drag target bigger for upload tab Tags are now correctly overflowing
131 lines
4.9 KiB
Python
131 lines
4.9 KiB
Python
"""
|
|
Onlylegs - Image Groups
|
|
Why groups? Because I don't like calling these albums
|
|
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.utils import contrast
|
|
|
|
|
|
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
|
|
"""
|
|
# Get all groups
|
|
group_list = db_session.query(db.Groups).all()
|
|
|
|
# For each group, get the 3 most recent images
|
|
for group_item in group_list:
|
|
group_item.author_username = db_session.query(db.Users.username)\
|
|
.filter(db.Users.id == group_item.author_id)\
|
|
.first()[0]
|
|
|
|
# Get the 3 most recent images
|
|
group_images = db_session.query(db.GroupJunction.post_id)\
|
|
.filter(db.GroupJunction.group_id == group_item.id)\
|
|
.order_by(db.GroupJunction.date_added.desc())\
|
|
.limit(3)
|
|
|
|
# Add an empty list to the group item
|
|
group_item.images = []
|
|
# For each image, get the image data and add it to the group item
|
|
for image in group_images:
|
|
group_item.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())
|
|
|
|
return render_template('groups/list.html', groups=group_list)
|
|
|
|
|
|
@blueprint.route('/<int:group_id>')
|
|
def group(group_id):
|
|
"""
|
|
Group view, shows all images in a group
|
|
"""
|
|
group_item = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
|
|
|
|
if group_item is None:
|
|
abort(404, 'Group not found! D:')
|
|
|
|
group_item.author_username = db_session.query(db.Users.username)\
|
|
.filter(db.Users.id == group_item.author_id)\
|
|
.first()[0]
|
|
|
|
group_images = db_session.query(db.GroupJunction.post_id)\
|
|
.filter(db.GroupJunction.group_id == group_id)\
|
|
.order_by(db.GroupJunction.date_added.desc())\
|
|
.all()
|
|
|
|
images = []
|
|
for image in group_images:
|
|
image = db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
|
|
images.append(image)
|
|
|
|
if images:
|
|
text_colour = contrast.contrast(images[0].image_colours[0],
|
|
'rgb(var(--fg-black))',
|
|
'rgb(var(--fg-white))')
|
|
else:
|
|
text_colour = 'rgb(var(--fg-black))'
|
|
|
|
return render_template('groups/group.html',
|
|
group=group_item,
|
|
images=images,
|
|
text_colour=text_colour)
|
|
|
|
|
|
@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
|
|
"""
|
|
img = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
|
|
|
|
if img is None:
|
|
abort(404, 'Image not found')
|
|
|
|
img.author_username = db_session.query(db.Users.username)\
|
|
.filter(db.Users.id == img.author_id)\
|
|
.first()[0]
|
|
|
|
groups = db_session.query(db.GroupJunction.group_id)\
|
|
.filter(db.GroupJunction.post_id == image_id)\
|
|
.all()
|
|
|
|
img.groups = []
|
|
for group in groups:
|
|
group = db_session.query(db.Groups).filter(db.Groups.id == group[0]).first()
|
|
img.groups.append(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())\
|
|
.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()
|
|
|
|
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])
|
|
|
|
return render_template('image.html', image=img, next_url=next_url, prev_url=prev_url)
|