Update database

Correctly link user to their posts and groups
Change the table names to Group, Post and User
Remove unused Bans and Logs table, possibly will return later
This commit is contained in:
Michał Gdula 2023-04-12 15:16:43 +00:00
parent 9a21064dd5
commit d36699bd1f
12 changed files with 185 additions and 270 deletions

View file

@ -5,7 +5,8 @@ sounds more limiting that it actually is in this gallery
"""
from flask import Blueprint, abort, render_template, url_for
from gallery.models import Posts, Users, GroupJunction, Groups
from gallery.models import Post, User, GroupJunction, Group
from gallery.extensions import db
from gallery.utils import contrast
@ -17,19 +18,20 @@ def groups():
"""
Group overview, shows all image groups
"""
groups = Groups.query.all()
groups = Group.query.all()
# For each group, get the 3 most recent images
for group in groups:
group.author_username = (
Users.query.with_entities(Users.username)
.filter(Users.id == group.author_id)
User.query.with_entities(User.username)
.filter(User.id == group.author_id)
.first()[0]
)
# Get the 3 most recent images
images = (
GroupJunction.query.with_entities(GroupJunction.post_id)
GroupJunction.query
.with_entities(GroupJunction.post_id)
.filter(GroupJunction.group_id == group.id)
.order_by(GroupJunction.date_added.desc())
.limit(3)
@ -39,10 +41,9 @@ def groups():
group.images = []
for image in images:
group.images.append(
Posts.query.with_entities(
Posts.filename, Posts.alt, Posts.colours, Posts.id
)
.filter(Posts.id == image[0])
Post.query
.with_entities(Post.filename, Post.alt, Post.colours, Post.id)
.filter(Post.id == image[0])
.first()
)
@ -55,21 +56,12 @@ def group(group_id):
Group view, shows all images in a group
"""
# Get the group, if it doesn't exist, 404
group = Groups.query.filter(Groups.id == group_id).first()
if group is None:
abort(404, "Group not found! D:")
# Get the group's author username
group.author_username = (
Users.query.with_entities(Users.username)
.filter(Users.id == group.author_id)
.first()[0]
)
group = db.get_or_404(Group, group_id, description="Group not found! D:")
# Get all images in the group from the junction table
junction = (
GroupJunction.query.with_entities(GroupJunction.post_id)
GroupJunction.query
.with_entities(GroupJunction.post_id)
.filter(GroupJunction.group_id == group_id)
.order_by(GroupJunction.date_added.desc())
.all()
@ -78,7 +70,7 @@ def group(group_id):
# Get the image data for each image in the group
images = []
for image in junction:
images.append(Posts.query.filter(Posts.id == image[0]).first())
images.append(Post.query.filter(Post.id == image[0]).first())
# Check contrast for the first image in the group for the banner
text_colour = "rgb(var(--fg-black))"
@ -98,16 +90,7 @@ 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 = Posts.query.filter(Posts.id == image_id).first()
if image is None:
abort(404, "Image not found")
# Get the image's author username
image.author_username = (
Users.query.with_entities(Users.username)
.filter(Users.id == image.author_id)
.first()[0]
)
image = db.get_or_404(Post, image_id, description="Image not found :<")
# Get all groups the image is in
groups = (
@ -120,8 +103,8 @@ def group_post(group_id, image_id):
image.groups = []
for group in groups:
image.groups.append(
Groups.query.with_entities(Groups.id, Groups.name)
.filter(Groups.id == group[0])
Group.query.with_entities(Group.id, Group.name)
.filter(Group.id == group[0])
.first()
)

View file

@ -2,10 +2,9 @@
Onlylegs - Image View
"""
from math import ceil
from flask import Blueprint, abort, render_template, url_for, current_app
from gallery.models import Posts, Users, GroupJunction, Groups
from flask import Blueprint, render_template, url_for, current_app
from gallery.models import Post, GroupJunction, Group
from gallery.extensions import db
blueprint = Blueprint("image", __name__, url_prefix="/image")
@ -17,45 +16,36 @@ def image(image_id):
Image view, shows the image and its metadata
"""
# Get the image, if it doesn't exist, 404
image = Posts.query.filter(Posts.id == image_id).first()
if not image:
abort(404, "Image not found :<")
image = db.get_or_404(Post, image_id, description="Image not found :<")
# Get the image's author username
image.author_username = (
Users.query.with_entities(Users.username)
.filter(Users.id == image.author_id)
.first()[0]
)
# Get the image's groups
# Get all groups the image is in
groups = (
GroupJunction.query.with_entities(GroupJunction.group_id)
.filter(GroupJunction.post_id == image_id)
.all()
)
# For each group, get the group data and add it to the image item
# Get the group data for each group the image is in
image.groups = []
for group in groups:
image.groups.append(
Groups.query.with_entities(Groups.name, Groups.id)
.filter(Groups.id == group[0])
Group.query.with_entities(Group.id, Group.name)
.filter(Group.id == group[0])
.first()
)
# Get the next and previous images
# Check if there is a group ID set
next_url = (
Posts.query.with_entities(Posts.id)
.filter(Posts.id > image_id)
.order_by(Posts.id.asc())
Post.query.with_entities(Post.id)
.filter(Post.id > image_id)
.order_by(Post.id.asc())
.first()
)
prev_url = (
Posts.query.with_entities(Posts.id)
.filter(Posts.id < image_id)
.order_by(Posts.id.desc())
Post.query.with_entities(Post.id)
.filter(Post.id < image_id)
.order_by(Post.id.desc())
.first()
)
@ -66,7 +56,7 @@ def image(image_id):
prev_url = url_for("image.image", image_id=prev_url[0])
# Yoink all the images in the database
total_images = Posts.query.with_entities(Posts.id).order_by(Posts.id.desc()).all()
total_images = Post.query.with_entities(Post.id).order_by(Post.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

View file

@ -6,7 +6,7 @@ from math import ceil
from flask import Blueprint, render_template, request, current_app
from werkzeug.exceptions import abort
from gallery.models import Posts
from gallery.models import Post
blueprint = Blueprint("gallery", __name__)
@ -27,7 +27,7 @@ def index():
# 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 = Posts.query.with_entities(Posts.id).count()
total_images = Post.query.with_entities(Post.id).count()
pages = ceil(max(total_images, limit) / limit)
if page > pages:
abort(
@ -38,10 +38,9 @@ def index():
# get the images for the current page
images = (
Posts.query.with_entities(
Posts.filename, Posts.alt, Posts.colours, Posts.created_at, Posts.id
)
.order_by(Posts.id.desc())
Post.query
.with_entities( Post.filename, Post.alt, Post.colours, Post.created_at, Post.id)
.order_by(Post.id.desc())
.offset((page - 1) * limit)
.limit(limit)
.all()

View file

@ -5,7 +5,7 @@ from flask import Blueprint, render_template, request
from werkzeug.exceptions import abort
from flask_login import current_user
from gallery.models import Posts, Users
from gallery.models import Post, User
blueprint = Blueprint("profile", __name__, url_prefix="/profile")
@ -26,11 +26,11 @@ def profile():
abort(404, "You must be logged in to view your own profile!")
# Get the user's data
user = Users.query.filter(Users.id == user_id).first()
user = User.query.filter(User.id == user_id).first()
if not user:
abort(404, "User not found :c")
images = Posts.query.filter(Posts.author_id == user_id).all()
images = Post.query.filter(Post.author_id == user_id).all()
return render_template("profile.html", user=user, images=images)