nopaque/app/profile/routes.py

136 lines
5.0 KiB
Python
Raw Normal View History

2022-12-05 08:40:02 +00:00
from flask import (
abort,
2022-12-13 14:01:04 +00:00
current_app,
2022-12-05 08:40:02 +00:00
flash,
Markup,
redirect,
render_template,
send_from_directory,
url_for
)
2022-11-29 15:46:33 +00:00
from flask_login import current_user, login_required
2022-12-13 14:01:04 +00:00
from threading import Thread
2022-12-05 08:40:02 +00:00
import os
2022-11-29 15:46:33 +00:00
from app import db
2022-12-13 14:01:04 +00:00
from app.models import Avatar, ProfilePrivacySettings, User
2022-11-29 15:46:33 +00:00
from . import bp
2022-11-30 13:36:42 +00:00
from .forms import (
2022-12-13 14:01:04 +00:00
EditPrivacySettingsForm,
EditProfileSettingsForm,
EditPublicProfileInformationForm
2022-11-30 13:36:42 +00:00
)
2022-11-29 15:46:33 +00:00
2022-12-05 08:40:02 +00:00
@bp.before_request
2022-11-29 15:46:33 +00:00
@login_required
2022-12-05 08:40:02 +00:00
def before_request():
pass
2022-12-05 15:25:54 +00:00
@bp.route('/<hashid:user_id>')
def profile(user_id):
user = User.query.get_or_404(user_id)
2022-12-13 14:01:04 +00:00
if not user.is_public and user != current_user:
abort(403)
2022-12-19 14:35:06 +00:00
return render_template(
'profile/profile.html.j2',
2022-12-19 15:25:10 +00:00
user=user.to_json_serializeable()
2022-12-19 14:35:06 +00:00
)
2022-11-29 15:46:33 +00:00
2022-12-19 14:35:06 +00:00
@bp.route('/<hashid:user_id>/avatar')
def profile_avatar(user_id):
user = User.query.get_or_404(user_id)
if user.avatar is None:
2022-12-05 08:40:02 +00:00
abort(404)
2022-12-19 14:35:06 +00:00
if not user.is_public and not (user == current_user or current_user.is_administrator()):
abort(403)
2022-12-05 08:40:02 +00:00
return send_from_directory(
2022-12-19 14:35:06 +00:00
os.path.dirname(user.avatar.path),
os.path.basename(user.avatar.path),
2022-12-05 08:40:02 +00:00
as_attachment=True,
2022-12-19 14:35:06 +00:00
attachment_filename=user.avatar.filename,
mimetype=user.avatar.mimetype
2022-12-05 08:40:02 +00:00
)
2022-12-19 14:35:06 +00:00
@bp.route('/<hashid:user_id>/avatar', methods=['DELETE'])
def delete_profile_avatar(user_id):
2022-12-13 14:01:04 +00:00
def _delete_avatar(app, avatar_id):
with app.app_context():
2022-12-19 14:35:06 +00:00
avatar = Avatar.query.get(avatar_id)
avatar.delete()
2022-12-13 14:01:04 +00:00
db.session.commit()
2022-12-19 14:35:06 +00:00
user = User.query.get_or_404(user_id)
if user.avatar is None:
abort(404)
2022-12-13 14:01:04 +00:00
thread = Thread(
target=_delete_avatar,
2022-12-19 14:35:06 +00:00
args=(current_app._get_current_object(), user.avatar.id)
2022-12-13 14:01:04 +00:00
)
thread.start()
return {}, 202
2022-12-19 14:35:06 +00:00
@bp.route('/<hashid:user_id>/edit', methods=['GET', 'POST'])
2022-12-05 15:25:54 +00:00
def edit_profile(user_id):
user = User.query.get_or_404(user_id)
2022-12-05 08:40:02 +00:00
edit_profile_settings_form = EditProfileSettingsForm(
2022-11-30 13:36:42 +00:00
current_user,
data=current_user.to_json_serializeable(),
prefix='edit-profile-settings-form'
2022-12-05 08:40:02 +00:00
)
2022-12-13 14:01:04 +00:00
edit_privacy_settings_form = EditPrivacySettingsForm(
data=current_user.to_json_serializeable(),
prefix='edit-privacy-settings-form'
)
edit_public_profile_information_form = EditPublicProfileInformationForm(
data=current_user.to_json_serializeable(),
prefix='edit-public-profile-information-form'
)
2022-12-05 08:40:02 +00:00
if edit_profile_settings_form.validate_on_submit():
2022-12-13 14:01:04 +00:00
current_user.email = edit_profile_settings_form.email.data
current_user.username = edit_profile_settings_form.username.data
db.session.commit()
2022-12-19 14:35:06 +00:00
flash('Profile settings updated')
2022-12-13 14:01:04 +00:00
return redirect(url_for('.profile', user_id=user.id))
2022-12-19 14:35:06 +00:00
if edit_privacy_settings_form.submit.data and edit_privacy_settings_form.validate():
2022-12-13 14:01:04 +00:00
current_user.is_public = edit_privacy_settings_form.is_public.data
if edit_privacy_settings_form.show_email.data:
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL)
else:
current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL)
if edit_privacy_settings_form.show_last_seen.data:
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN)
else:
current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN)
if edit_privacy_settings_form.show_member_since.data:
current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
else:
current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE)
db.session.commit()
flash('Your changes have been saved')
return redirect(url_for('.profile', user_id=user.id))
if edit_public_profile_information_form.validate_on_submit():
if edit_public_profile_information_form.avatar.data:
2022-12-05 08:40:02 +00:00
try:
2022-12-13 14:01:04 +00:00
Avatar.create(edit_public_profile_information_form.avatar.data, user=current_user)
2022-12-05 08:40:02 +00:00
except (AttributeError, OSError):
abort(500)
2022-12-13 14:01:04 +00:00
current_user.about_me = edit_public_profile_information_form.about_me.data
current_user.location = edit_public_profile_information_form.location.data
current_user.organization = edit_public_profile_information_form.organization.data
current_user.website = edit_public_profile_information_form.website.data
current_user.full_name = edit_public_profile_information_form.full_name.data
2022-11-30 13:36:42 +00:00
db.session.commit()
2022-12-19 14:35:06 +00:00
flash('Profile settings updated')
2022-12-05 15:25:54 +00:00
return redirect(url_for('.profile', user_id=user.id))
2022-12-19 14:35:06 +00:00
return render_template(
'profile/edit_profile.html.j2',
2022-12-05 08:40:02 +00:00
edit_profile_settings_form=edit_profile_settings_form,
2022-12-13 14:01:04 +00:00
edit_privacy_settings_form=edit_privacy_settings_form,
edit_public_profile_information_form=edit_public_profile_information_form,
2022-12-05 15:25:54 +00:00
user=user,
2022-12-19 14:35:06 +00:00
title='Edit Profile'
)