nopaque/app/users/routes.py

66 lines
2.0 KiB
Python
Raw Normal View History

2022-12-23 17:08:33 +00:00
from flask import (
abort,
redirect,
render_template,
send_from_directory,
url_for
)
2023-03-14 10:44:15 +00:00
from flask_breadcrumbs import register_breadcrumb
2022-09-02 11:07:30 +00:00
from flask_login import current_user, login_required
2022-12-23 17:08:33 +00:00
import os
from app.models import Corpus, User
2022-09-02 11:07:30 +00:00
from . import bp
from .utils import user_dynamic_list_constructor as user_dlc
2023-03-14 10:44:15 +00:00
2022-09-02 11:07:30 +00:00
2023-03-14 10:44:15 +00:00
@bp.route('')
2023-03-16 08:29:57 +00:00
@register_breadcrumb(bp, '.', '<i class="material-icons left">group</i>Users')
2022-12-23 17:08:33 +00:00
@login_required
2023-03-14 10:44:15 +00:00
def users():
return redirect(url_for('main.social_area', _anchor='users'))
2022-09-02 11:07:30 +00:00
@bp.route('/<hashid:user_id>')
2023-03-14 10:44:15 +00:00
@register_breadcrumb(bp, '.entity', '', dynamic_list_constructor=user_dlc)
2022-09-02 11:07:30 +00:00
@login_required
def user(user_id):
2022-12-23 17:08:33 +00:00
user = User.query.get_or_404(user_id)
last_seen = user.last_seen.strftime('%Y-%m-%d %H:%M')
member_since = user.member_since.strftime('%Y-%m-%d')
followed_corpora = [
c.to_json_serializeable() for c in user.followed_corpora
]
own_public_corpora = [
c.to_json_serializeable() for c
in Corpus.query.filter_by(is_public = True, user = user).all()
]
2022-12-23 17:08:33 +00:00
if not user.is_public and user != current_user:
abort(403)
return render_template(
'users/profile.html.j2',
followed_corpora=followed_corpora,
last_seen=last_seen,
member_since=member_since,
own_public_corpora=own_public_corpora,
2023-03-21 09:50:29 +00:00
user=user,
2023-03-14 10:44:15 +00:00
user_id=user_id,
title=user.username
2022-12-23 17:08:33 +00:00
)
2022-09-02 11:07:30 +00:00
2022-12-23 17:08:33 +00:00
@bp.route('/<hashid:user_id>/avatar')
2023-03-14 10:44:15 +00:00
@login_required
2022-12-23 17:08:33 +00:00
def profile_avatar(user_id):
user = User.query.get_or_404(user_id)
if not (user.is_public or user == current_user or current_user.is_administrator()):
abort(403)
2022-12-23 17:08:33 +00:00
if user.avatar is None:
2023-03-14 13:21:23 +00:00
return redirect(url_for('static', filename='images/user_avatar.png'))
2022-12-23 17:08:33 +00:00
return send_from_directory(
os.path.dirname(user.avatar.path),
os.path.basename(user.avatar.path),
as_attachment=True,
attachment_filename=user.avatar.filename,
mimetype=user.avatar.mimetype
)