python-gallery/onlylegs/views/profile.py

36 lines
1.1 KiB
Python
Raw Normal View History

2023-04-06 16:22:56 +00:00
"""
Onlylegs Gallery - Profile view
"""
from flask import Blueprint, render_template, request
from werkzeug.exceptions import abort
from flask_login import current_user
2023-04-20 13:58:40 +00:00
from onlylegs.models import Post, User, Group
from onlylegs.extensions import db
2023-04-06 16:22:56 +00:00
2023-04-07 12:35:30 +00:00
blueprint = Blueprint("profile", __name__, url_prefix="/profile")
2023-04-06 16:22:56 +00:00
2023-04-10 23:03:04 +00:00
@blueprint.route("/")
2023-04-06 16:22:56 +00:00
def profile():
"""
Profile overview, shows all profiles on the onlylegs gallery
"""
2023-04-07 12:35:30 +00:00
user_id = request.args.get("id", default=None, type=int)
2023-04-06 16:22:56 +00:00
# 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:
2023-04-07 12:35:30 +00:00
abort(404, "You must be logged in to view your own profile!")
2023-04-06 16:22:56 +00:00
# Get the user's data
2023-04-20 13:58:40 +00:00
user = db.get_or_404(User, user_id, description="User not found :<")
2023-04-06 16:22:56 +00:00
images = Post.query.filter(Post.author_id == user_id).all()
2023-04-20 13:58:40 +00:00
groups = Group.query.filter(Group.author_id == user_id).all()
2023-04-06 16:22:56 +00:00
2023-04-20 13:58:40 +00:00
return render_template("profile.html", user=user, images=images, groups=groups)