Format code with Black

This commit is contained in:
Michał Gdula 2023-04-07 12:35:30 +00:00
parent fef8a557d2
commit 128464d43f
18 changed files with 697 additions and 604 deletions

View file

@ -10,12 +10,12 @@ from gallery import db
from gallery.utils import contrast
blueprint = Blueprint('group', __name__, url_prefix='/group')
blueprint = Blueprint("group", __name__, url_prefix="/group")
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/', methods=['GET'])
@blueprint.route("/", methods=["GET"])
def groups():
"""
Group overview, shows all image groups
@ -24,116 +24,134 @@ def groups():
# For each group, get the 3 most recent images
for group in groups:
group.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id)
.first()[0])
group.author_username = (
db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id)
.first()[0]
)
# Get the 3 most recent images
images = (db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group.id)
.order_by(db.GroupJunction.date_added.desc())
.limit(3))
images = (
db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group.id)
.order_by(db.GroupJunction.date_added.desc())
.limit(3)
)
# For each image, get the image data and add it to the group item
group.images = []
for image in images:
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())
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()
)
return render_template('list.html', groups=groups)
return render_template("list.html", groups=groups)
@blueprint.route('/<int:group_id>')
@blueprint.route("/<int:group_id>")
def group(group_id):
"""
Group view, shows all images in a group
"""
# Get the group, if it doesn't exist, 404
group = (db_session.query(db.Groups)
.filter(db.Groups.id == group_id)
.first())
group = db_session.query(db.Groups).filter(db.Groups.id == group_id).first()
if group is None:
abort(404, 'Group not found! D:')
abort(404, "Group not found! D:")
# Get the group's author username
group.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id)
.first()[0])
group.author_username = (
db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id)
.first()[0]
)
# Get all images in the group from the junction table
junction = (db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id)
.order_by(db.GroupJunction.date_added.desc())
.all())
junction = (
db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id)
.order_by(db.GroupJunction.date_added.desc())
.all()
)
# Get the image data for each image in the group
images = []
for image in junction:
images.append(db_session.query(db.Posts)
.filter(db.Posts.id == image[0])
.first())
images.append(
db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
)
# Check contrast for the first image in the group for the banner
text_colour = 'rgb(var(--fg-black))'
text_colour = "rgb(var(--fg-black))"
if images:
text_colour = contrast.contrast(images[0].colours[0],
'rgb(var(--fg-black))',
'rgb(var(--fg-white))')
text_colour = contrast.contrast(
images[0].colours[0], "rgb(var(--fg-black))", "rgb(var(--fg-white))"
)
return render_template('group.html',
group=group,
images=images,
text_colour=text_colour)
return render_template(
"group.html", group=group, images=images, text_colour=text_colour
)
@blueprint.route('/<int:group_id>/<int:image_id>')
@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
"""
# Get the image, if it doesn't exist, 404
image = (db_session.query(db.Posts)
.filter(db.Posts.id == image_id)
.first())
image = db_session.query(db.Posts).filter(db.Posts.id == image_id).first()
if image is None:
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 all groups the image is in
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()
)
# Get the group data for each group the image is in
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 in the 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())
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 there is a next or previous image, get the URL for it
if next_url:
next_url = url_for('group.group_post', group_id=group_id, image_id=next_url[0])
next_url = url_for("group.group_post", group_id=group_id, image_id=next_url[0])
if prev_url:
prev_url = url_for('group.group_post', group_id=group_id, image_id=prev_url[0])
prev_url = url_for("group.group_post", group_id=group_id, image_id=prev_url[0])
return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)
return render_template(
"image.html", image=image, next_url=next_url, prev_url=prev_url
)

View file

@ -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,
)

View file

@ -10,40 +10,50 @@ from sqlalchemy.orm import sessionmaker
from gallery import db
blueprint = Blueprint('gallery', __name__)
blueprint = Blueprint("gallery", __name__)
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/')
@blueprint.route("/")
def index():
"""
Home page of the website, shows the feed of the latest images
"""
# meme
if request.args.get('coffee') == 'please':
if request.args.get("coffee") == "please":
abort(418)
# pagination, defaults to page 1 if no page is specified
page = request.args.get('page', default=1, type=int)
limit = current_app.config['UPLOAD_CONF']['max-load']
page = request.args.get("page", default=1, type=int)
limit = current_app.config["UPLOAD_CONF"]["max-load"]
# get the total number of images in the database
# calculate the total number of pages, and make sure the page number is valid
total_images = db_session.query(db.Posts.id).count()
pages = ceil(max(total_images, limit) / limit)
if page > pages:
abort(404, 'You have reached the far and beyond, ' +
'but you will not find your answers here.')
# get the images for the current page
images = (db_session.query(db.Posts.filename, db.Posts.alt, db.Posts.colours,
db.Posts.created_at, db.Posts.id)
.order_by(db.Posts.id.desc())
.offset((page - 1) * limit)
.limit(limit)
.all())
abort(
404,
"You have reached the far and beyond, "
+ "but you will not find your answers here.",
)
return render_template('index.html', images=images,
total_images=total_images,
pages=pages, page=page)
# get the images for the current page
images = (
db_session.query(
db.Posts.filename,
db.Posts.alt,
db.Posts.colours,
db.Posts.created_at,
db.Posts.id,
)
.order_by(db.Posts.id.desc())
.offset((page - 1) * limit)
.limit(limit)
.all()
)
return render_template(
"index.html", images=images, total_images=total_images, pages=pages, page=page
)

View file

@ -9,31 +9,31 @@ from sqlalchemy.orm import sessionmaker
from gallery import db
blueprint = Blueprint('profile', __name__, url_prefix='/profile')
blueprint = Blueprint("profile", __name__, url_prefix="/profile")
db_session = sessionmaker(bind=db.engine)
db_session = db_session()
@blueprint.route('/profile')
@blueprint.route("/profile")
def profile():
"""
Profile overview, shows all profiles on the onlylegs gallery
"""
user_id = request.args.get('id', default=None, type=int)
user_id = request.args.get("id", default=None, type=int)
# If there is no userID set, check if the user is logged in and display their profile
if not user_id:
if current_user.is_authenticated:
user_id = current_user.id
else:
abort(404, 'You must be logged in to view your own profile!')
abort(404, "You must be logged in to view your own profile!")
# Get the user's data
user = db_session.query(db.Users).filter(db.Users.id == user_id).first()
if not user:
abort(404, 'User not found :c')
abort(404, "User not found :c")
images = db_session.query(db.Posts).filter(db.Posts.author_id == user_id).all()
return render_template('profile.html', user=user, images=images)
return render_template("profile.html", user=user, images=images)

View file

@ -4,40 +4,40 @@ OnlyLegs - Settings page
from flask import Blueprint, render_template
from flask_login import login_required
blueprint = Blueprint('settings', __name__, url_prefix='/settings')
blueprint = Blueprint("settings", __name__, url_prefix="/settings")
@blueprint.route('/')
@blueprint.route("/")
@login_required
def general():
"""
General settings page
"""
return render_template('settings/general.html')
return render_template("settings/general.html")
@blueprint.route('/server')
@blueprint.route("/server")
@login_required
def server():
"""
Server settings page
"""
return render_template('settings/server.html')
return render_template("settings/server.html")
@blueprint.route('/account')
@blueprint.route("/account")
@login_required
def account():
"""
Account settings page
"""
return render_template('settings/account.html')
return render_template("settings/account.html")
@blueprint.route('/logs')
@blueprint.route("/logs")
@login_required
def logs():
"""
Logs settings page
"""
return render_template('settings/logs.html')
return render_template("settings/logs.html")