mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-02-05 21:18:08 +00:00
Clean up Database queries
This commit is contained in:
parent
454d1f89c8
commit
1152856f2a
|
@ -40,11 +40,9 @@ def file(file_name):
|
|||
if not request.args:
|
||||
if not os.path.exists(os.path.join(current_app.config['UPLOAD_FOLDER'], file_name)):
|
||||
abort(404)
|
||||
|
||||
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name)
|
||||
|
||||
thumb = generate_thumbnail(file_name, res, ext)
|
||||
|
||||
if not thumb:
|
||||
abort(404)
|
||||
|
||||
|
@ -180,16 +178,16 @@ def modify_group():
|
|||
abort(403)
|
||||
|
||||
if request.form['action'] == 'add':
|
||||
if not db_session.query(db.GroupJunction)\
|
||||
.filter_by(group_id=group_id, post_id=image_id)\
|
||||
.first():
|
||||
if not (db_session.query(db.GroupJunction)
|
||||
.filter_by(group_id=group_id, post_id=image_id)
|
||||
.first()):
|
||||
db_session.add(db.GroupJunction(group_id=group_id,
|
||||
post_id=image_id,
|
||||
date_added=dt.utcnow()))
|
||||
elif request.form['action'] == 'remove':
|
||||
db_session.query(db.GroupJunction)\
|
||||
.filter_by(group_id=group_id, post_id=image_id)\
|
||||
.delete()
|
||||
(db_session.query(db.GroupJunction)
|
||||
.filter_by(group_id=group_id, post_id=image_id)
|
||||
.delete())
|
||||
|
||||
db_session.commit()
|
||||
|
||||
|
|
|
@ -24,22 +24,22 @@ 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.file_name, db.Posts.post_alt,
|
||||
db.Posts.image_colours, db.Posts.id)\
|
||||
.filter(db.Posts.id == image[0])\
|
||||
db.Posts.image_colours, db.Posts.id)
|
||||
.filter(db.Posts.id == image[0])
|
||||
.first())
|
||||
|
||||
return render_template('groups/list.html', groups=groups)
|
||||
|
@ -51,26 +51,30 @@ 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:')
|
||||
|
||||
# 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:
|
||||
image = db_session.query(db.Posts).filter(db.Posts.id == image[0]).first()
|
||||
images.append(image)
|
||||
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))'
|
||||
|
@ -91,44 +95,45 @@ 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')
|
||||
|
||||
# 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:
|
||||
group = db_session.query(db.Groups.id, db.Groups.name)\
|
||||
.filter(db.Groups.id == group[0])\
|
||||
.first()
|
||||
image.groups.append(group)
|
||||
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 is not None:
|
||||
if next_url:
|
||||
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])
|
||||
|
||||
return render_template('image.html', image=image, next_url=next_url, prev_url=prev_url)
|
||||
|
|
|
@ -41,30 +41,31 @@ def image(image_id):
|
|||
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:
|
||||
group = db_session.query(db.Groups.id, db.Groups.name)\
|
||||
.filter(db.Groups.id == group[0])\
|
||||
.first()
|
||||
image.groups.append(group)
|
||||
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
|
||||
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:
|
||||
|
|
Loading…
Reference in a new issue