Clean up Database queries

This commit is contained in:
Michał Gdula 2023-04-02 16:36:05 +00:00
parent 454d1f89c8
commit 1152856f2a
3 changed files with 70 additions and 66 deletions

View file

@ -40,11 +40,9 @@ def file(file_name):
if not request.args: if not request.args:
if not os.path.exists(os.path.join(current_app.config['UPLOAD_FOLDER'], file_name)): if not os.path.exists(os.path.join(current_app.config['UPLOAD_FOLDER'], file_name)):
abort(404) abort(404)
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name) return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name)
thumb = generate_thumbnail(file_name, res, ext) thumb = generate_thumbnail(file_name, res, ext)
if not thumb: if not thumb:
abort(404) abort(404)
@ -180,16 +178,16 @@ def modify_group():
abort(403) abort(403)
if request.form['action'] == 'add': if request.form['action'] == 'add':
if not db_session.query(db.GroupJunction)\ if not (db_session.query(db.GroupJunction)
.filter_by(group_id=group_id, post_id=image_id)\ .filter_by(group_id=group_id, post_id=image_id)
.first(): .first()):
db_session.add(db.GroupJunction(group_id=group_id, db_session.add(db.GroupJunction(group_id=group_id,
post_id=image_id, post_id=image_id,
date_added=dt.utcnow())) date_added=dt.utcnow()))
elif request.form['action'] == 'remove': elif request.form['action'] == 'remove':
db_session.query(db.GroupJunction)\ (db_session.query(db.GroupJunction)
.filter_by(group_id=group_id, post_id=image_id)\ .filter_by(group_id=group_id, post_id=image_id)
.delete() .delete())
db_session.commit() db_session.commit()

View file

@ -24,22 +24,22 @@ def groups():
# For each group, get the 3 most recent images # For each group, get the 3 most recent images
for group in groups: for group in groups:
group.author_username = db_session.query(db.Users.username)\ group.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id)\ .filter(db.Users.id == group.author_id)
.first()[0] .first()[0])
# Get the 3 most recent images # Get the 3 most recent images
images = db_session.query(db.GroupJunction.post_id)\ images = (db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group.id)\ .filter(db.GroupJunction.group_id == group.id)
.order_by(db.GroupJunction.date_added.desc())\ .order_by(db.GroupJunction.date_added.desc())
.limit(3) .limit(3))
# For each image, get the image data and add it to the group item # For each image, get the image data and add it to the group item
group.images = [] group.images = []
for image in images: for image in images:
group.images.append(db_session.query(db.Posts.file_name, db.Posts.post_alt, group.images.append(db_session.query(db.Posts.file_name, db.Posts.post_alt,
db.Posts.image_colours, db.Posts.id)\ db.Posts.image_colours, db.Posts.id)
.filter(db.Posts.id == image[0])\ .filter(db.Posts.id == image[0])
.first()) .first())
return render_template('groups/list.html', groups=groups) return render_template('groups/list.html', groups=groups)
@ -51,26 +51,30 @@ def group(group_id):
Group view, shows all images in a group Group view, shows all images in a group
""" """
# Get the group, if it doesn't exist, 404 # 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: if group is None:
abort(404, 'Group not found! D:') abort(404, 'Group not found! D:')
# Get the group's author username # Get the group's author username
group.author_username = db_session.query(db.Users.username)\ group.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == group.author_id)\ .filter(db.Users.id == group.author_id)
.first()[0] .first()[0])
# Get all images in the group from the junction table # Get all images in the group from the junction table
junction = db_session.query(db.GroupJunction.post_id)\ junction = (db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id)\ .filter(db.GroupJunction.group_id == group_id)
.order_by(db.GroupJunction.date_added.desc())\ .order_by(db.GroupJunction.date_added.desc())
.all() .all())
# Get the image data for each image in the group # Get the image data for each image in the group
images = [] images = []
for image in junction: for image in junction:
image = db_session.query(db.Posts).filter(db.Posts.id == image[0]).first() images.append(db_session.query(db.Posts)
images.append(image) .filter(db.Posts.id == image[0])
.first())
# Check contrast for the first image in the group for the banner # Check contrast for the first image in the group for the banner
text_colour = 'rgb(var(--fg-black))' text_colour = 'rgb(var(--fg-black))'
@ -91,44 +95,45 @@ def group_post(group_id, image_id):
Image view, shows the image and its metadata from a specific group Image view, shows the image and its metadata from a specific group
""" """
# Get the image, if it doesn't exist, 404 # 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: if image is None:
abort(404, 'Image not found') abort(404, 'Image not found')
# Get the image's author username # Get the image's author username
image.author_username = db_session.query(db.Users.username)\ image.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == image.author_id)\ .filter(db.Users.id == image.author_id)
.first()[0] .first()[0])
# Get all groups the image is in # Get all groups the image is in
groups = db_session.query(db.GroupJunction.group_id)\ groups = (db_session.query(db.GroupJunction.group_id)
.filter(db.GroupJunction.post_id == image_id)\ .filter(db.GroupJunction.post_id == image_id)
.all() .all())
# Get the group data for each group the image is in # Get the group data for each group the image is in
image.groups = [] image.groups = []
for group in groups: for group in groups:
group = db_session.query(db.Groups.id, db.Groups.name)\ image.groups.append(db_session.query(db.Groups.id, db.Groups.name)
.filter(db.Groups.id == group[0])\ .filter(db.Groups.id == group[0])
.first() .first())
image.groups.append(group)
# Get the next and previous images in the group # Get the next and previous images in the group
next_url = db_session.query(db.GroupJunction.post_id)\ next_url = (db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id)\ .filter(db.GroupJunction.group_id == group_id)
.filter(db.GroupJunction.post_id > image_id)\ .filter(db.GroupJunction.post_id > image_id)
.order_by(db.GroupJunction.date_added.asc())\ .order_by(db.GroupJunction.date_added.asc())
.first() .first())
prev_url = db_session.query(db.GroupJunction.post_id)\ prev_url = (db_session.query(db.GroupJunction.post_id)
.filter(db.GroupJunction.group_id == group_id)\ .filter(db.GroupJunction.group_id == group_id)
.filter(db.GroupJunction.post_id < image_id)\ .filter(db.GroupJunction.post_id < image_id)
.order_by(db.GroupJunction.date_added.desc())\ .order_by(db.GroupJunction.date_added.desc())
.first() .first())
# If there is a next or previous image, get the URL for it # If there is a next or previous image, get the URL for it
if next_url is not None: 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 is not None: 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

@ -41,30 +41,31 @@ def image(image_id):
abort(404, 'Image not found :<') abort(404, 'Image not found :<')
# Get the image's author username # Get the image's author username
image.author_username = db_session.query(db.Users.username)\ image.author_username = (db_session.query(db.Users.username)
.filter(db.Users.id == image.author_id).first()[0] .filter(db.Users.id == image.author_id)
.first()[0])
# Get the image's groups # Get the image's groups
groups = db_session.query(db.GroupJunction.group_id)\ groups = (db_session.query(db.GroupJunction.group_id)
.filter(db.GroupJunction.post_id == image_id).all() .filter(db.GroupJunction.post_id == image_id)
.all())
# For each group, get the group data and add it to the image item # For each group, get the group data and add it to the image item
image.groups = [] image.groups = []
for group in groups: for group in groups:
group = db_session.query(db.Groups.id, db.Groups.name)\ image.groups.append(db_session.query(db.Groups.id, db.Groups.name)
.filter(db.Groups.id == group[0])\ .filter(db.Groups.id == group[0])
.first() .first())
image.groups.append(group)
# Get the next and previous images # Get the next and previous images
next_url = db_session.query(db.Posts.id)\ next_url = (db_session.query(db.Posts.id)
.filter(db.Posts.id > image_id)\ .filter(db.Posts.id > image_id)
.order_by(db.Posts.id.asc())\ .order_by(db.Posts.id.asc())
.first() .first())
prev_url = db_session.query(db.Posts.id)\ prev_url = (db_session.query(db.Posts.id)
.filter(db.Posts.id < image_id)\ .filter(db.Posts.id < image_id)
.order_by(db.Posts.id.desc())\ .order_by(db.Posts.id.desc())
.first() .first())
# If there is a next or previous image, get the url # If there is a next or previous image, get the url
if next_url: if next_url: