mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-06-29 03:26:16 +00:00
Format code with Black
This commit is contained in:
parent
fef8a557d2
commit
128464d43f
18 changed files with 697 additions and 604 deletions
|
@ -9,12 +9,12 @@ from sqlalchemy.orm import sessionmaker
|
|||
from gallery import db
|
||||
|
||||
|
||||
blueprint = Blueprint('image', __name__, url_prefix='/image')
|
||||
blueprint = Blueprint("image", __name__, url_prefix="/image")
|
||||
db_session = sessionmaker(bind=db.engine)
|
||||
db_session = db_session()
|
||||
|
||||
|
||||
@blueprint.route('/<int:image_id>')
|
||||
@blueprint.route("/<int:image_id>")
|
||||
def image(image_id):
|
||||
"""
|
||||
Image view, shows the image and its metadata
|
||||
|
@ -22,48 +22,55 @@ def image(image_id):
|
|||
# Get the image, if it doesn't exist, 404
|
||||
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
|
||||
if not image:
|
||||
abort(404, 'Image not found :<')
|
||||
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)
|
||||
.first()[0])
|
||||
image.author_username = (
|
||||
db_session.query(db.Users.username)
|
||||
.filter(db.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)
|
||||
.all())
|
||||
groups = (
|
||||
db_session.query(db.GroupJunction.group_id)
|
||||
.filter(db.GroupJunction.post_id == image_id)
|
||||
.all()
|
||||
)
|
||||
|
||||
# For each group, get the group data and add it to the image item
|
||||
image.groups = []
|
||||
for group in groups:
|
||||
image.groups.append(db_session.query(db.Groups.id, db.Groups.name)
|
||||
.filter(db.Groups.id == group[0])
|
||||
.first())
|
||||
image.groups.append(
|
||||
db_session.query(db.Groups.id, db.Groups.name)
|
||||
.filter(db.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())
|
||||
.first())
|
||||
prev_url = (db_session.query(db.Posts.id)
|
||||
.filter(db.Posts.id < image_id)
|
||||
.order_by(db.Posts.id.desc())
|
||||
.first())
|
||||
next_url = (
|
||||
db_session.query(db.Posts.id)
|
||||
.filter(db.Posts.id > image_id)
|
||||
.order_by(db.Posts.id.asc())
|
||||
.first()
|
||||
)
|
||||
prev_url = (
|
||||
db_session.query(db.Posts.id)
|
||||
.filter(db.Posts.id < image_id)
|
||||
.order_by(db.Posts.id.desc())
|
||||
.first()
|
||||
)
|
||||
|
||||
# If there is a next or previous image, get the url
|
||||
if next_url:
|
||||
next_url = url_for('image.image', image_id=next_url[0])
|
||||
next_url = url_for("image.image", image_id=next_url[0])
|
||||
if prev_url:
|
||||
prev_url = url_for('image.image', image_id=prev_url[0])
|
||||
|
||||
|
||||
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())
|
||||
limit = current_app.config['UPLOAD_CONF']['max-load']
|
||||
total_images = db_session.query(db.Posts.id).order_by(db.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
|
||||
if len(total_images) <= limit:
|
||||
|
@ -72,11 +79,16 @@ def image(image_id):
|
|||
# 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
|
||||
for j in total_images[i * limit:(i + 1) * limit]:
|
||||
for j in total_images[i * limit : (i + 1) * limit]:
|
||||
# Is our image in this chunk?
|
||||
if image_id in j:
|
||||
return_page = i + 1
|
||||
break
|
||||
|
||||
return render_template('image.html', image=image, next_url=next_url,
|
||||
prev_url=prev_url, return_page=return_page)
|
||||
return render_template(
|
||||
"image.html",
|
||||
image=image,
|
||||
next_url=next_url,
|
||||
prev_url=prev_url,
|
||||
return_page=return_page,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue