Rename the gallery to onlylegs in the files

Im sorry
This commit is contained in:
Michał Gdula 2023-04-12 16:58:13 +00:00
parent 8029fff73e
commit 2174b10879
76 changed files with 66 additions and 20 deletions

View file

@ -0,0 +1 @@
# :3

132
onlylegs/views/group.py Normal file
View file

@ -0,0 +1,132 @@
"""
Onlylegs - Image Groups
Why groups? Because I don't like calling these albums
sounds more limiting that it actually is in this gallery
"""
from flask import Blueprint, render_template, url_for
from onlylegs.models import Post, User, GroupJunction, Group
from onlylegs.extensions import db
from onlylegs.utils import contrast
blueprint = Blueprint("group", __name__, url_prefix="/group")
@blueprint.route("/", methods=["GET"])
def groups():
"""
Group overview, shows all image groups
"""
groups = Group.query.all()
# For each group, get the 3 most recent images
for group in groups:
group.author_username = (
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)
.filter(GroupJunction.group_id == group.id)
.order_by(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(
Post.query.with_entities(Post.filename, Post.alt, Post.colours, Post.id)
.filter(Post.id == image[0])
.first()
)
return render_template("list.html", groups=groups)
@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.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)
.filter(GroupJunction.group_id == group_id)
.order_by(GroupJunction.date_added.desc())
.all()
)
# Get the image data for each image in the group
images = []
for image in junction:
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))"
if images:
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
)
@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.get_or_404(Post, image_id, description="Image not found :<")
# Get all groups the image is in
groups = (
GroupJunction.query.with_entities(GroupJunction.group_id)
.filter(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(
Group.query.with_entities(Group.id, Group.name)
.filter(Group.id == group[0])
.first()
)
# Get the next and previous images in the group
next_url = (
GroupJunction.query.with_entities(GroupJunction.post_id)
.filter(GroupJunction.group_id == group_id)
.filter(GroupJunction.post_id > image_id)
.order_by(GroupJunction.date_added.asc())
.first()
)
prev_url = (
GroupJunction.query.with_entities(GroupJunction.post_id)
.filter(GroupJunction.group_id == group_id)
.filter(GroupJunction.post_id < image_id)
.order_by(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])
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
)

81
onlylegs/views/image.py Normal file
View file

@ -0,0 +1,81 @@
"""
Onlylegs - Image View
"""
from math import ceil
from flask import Blueprint, render_template, url_for, current_app
from onlylegs.models import Post, GroupJunction, Group
from onlylegs.extensions import db
blueprint = Blueprint("image", __name__, url_prefix="/image")
@blueprint.route("/<int:image_id>")
def image(image_id):
"""
Image view, shows the image and its metadata
"""
# Get the image, if it doesn't exist, 404
image = db.get_or_404(Post, image_id, description="Image not found :<")
# Get all groups the image is in
groups = (
GroupJunction.query.with_entities(GroupJunction.group_id)
.filter(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(
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 = (
Post.query.with_entities(Post.id)
.filter(Post.id > image_id)
.order_by(Post.id.asc())
.first()
)
prev_url = (
Post.query.with_entities(Post.id)
.filter(Post.id < image_id)
.order_by(Post.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])
if prev_url:
prev_url = url_for("image.image", image_id=prev_url[0])
# Yoink all the images in the database
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
if len(total_images) <= limit:
return_page = None
else:
# 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]:
# Is our image in this chunk?
if not image_id > j[-1]:
return_page = i + 1
break
return render_template(
"image.html",
image=image,
next_url=next_url,
prev_url=prev_url,
return_page=return_page,
)

52
onlylegs/views/index.py Normal file
View file

@ -0,0 +1,52 @@
"""
Onlylegs Gallery - Index view
"""
from math import ceil
from flask import Blueprint, render_template, request, current_app
from werkzeug.exceptions import abort
from onlylegs.models import Post
blueprint = Blueprint("gallery", __name__)
@blueprint.route("/")
def index():
"""
Home page of the website, shows the feed of the latest images
"""
# meme
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"]
# 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 = Post.query.with_entities(Post.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 = (
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()
)
return render_template(
"index.html", images=images, total_images=total_images, pages=pages, page=page
)

36
onlylegs/views/profile.py Normal file
View file

@ -0,0 +1,36 @@
"""
Onlylegs Gallery - Profile view
"""
from flask import Blueprint, render_template, request
from werkzeug.exceptions import abort
from flask_login import current_user
from onlylegs.models import Post, User
blueprint = Blueprint("profile", __name__, url_prefix="/profile")
@blueprint.route("/")
def profile():
"""
Profile overview, shows all profiles on the onlylegs gallery
"""
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!")
# Get the user's data
user = User.query.filter(User.id == user_id).first()
if not user:
abort(404, "User not found :c")
images = Post.query.filter(Post.author_id == user_id).all()
return render_template("profile.html", user=user, images=images)

View file

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