nopaque/app/profile/routes.py

130 lines
5.1 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
user_data = user.to_json_serializeable()
if not user.is_public and user != current_user:
abort(403)
2022-12-05 08:40:02 +00:00
return render_template('profile/profile_page.html.j2',
2022-12-13 14:01:04 +00:00
user=user,
user_data=user_data)
2022-11-29 15:46:33 +00:00
2022-12-05 15:25:54 +00:00
@bp.route('/<hashid:user_id>/avatars/<hashid:avatar_id>')
def avatar_download(user_id, avatar_id):
avatar_file = Avatar.query.filter_by(user_id = user_id, id = avatar_id).first_or_404()
2022-12-05 08:40:02 +00:00
if not (avatar_file and avatar_file.filename):
abort(404)
return send_from_directory(
os.path.dirname(avatar_file.path),
os.path.basename(avatar_file.path),
as_attachment=True,
attachment_filename=avatar_file.filename,
mimetype=avatar_file.mimetype
)
2022-12-13 14:01:04 +00:00
@bp.route('/<hashid:user_id>/avatars/<hashid:avatar_id>', methods=['DELETE'])
def delete_avatar(avatar_id, user_id):
def _delete_avatar(app, avatar_id):
with app.app_context():
avatar_file = Avatar.query.get(avatar_id)
print(avatar_file)
avatar_file.delete()
db.session.commit()
thread = Thread(
target=_delete_avatar,
args=(current_app._get_current_object(), avatar_id)
)
thread.start()
return {}, 202
2022-12-05 15:25:54 +00:00
@bp.route('/<hashid:user_id>/edit-profile', methods=['GET', 'POST'])
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()
message = Markup(f'Profile settings updated')
flash(message, 'success')
return redirect(url_for('.profile', user_id=user.id))
if (edit_privacy_settings_form.submit.data
and edit_privacy_settings_form.validate()):
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-05 08:40:02 +00:00
message = Markup(f'Profile settings updated')
flash(message, 'success')
2022-12-05 15:25:54 +00:00
return redirect(url_for('.profile', user_id=user.id))
2022-12-05 08:40:02 +00:00
return render_template('profile/edit_profile.html.j2',
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-05 08:40:02 +00:00
title='Edit Profile')