From 148df4fcbaf193e7aefdac9b205ee51ad44f70eb Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Tue, 10 Dec 2024 09:56:30 +0100 Subject: [PATCH] edit form for user note --- app/templates/user/edit_note.html | 69 ++++++++++++++++++++++++++++ app/templates/user/show_profile.html | 1 + app/user/forms.py | 5 ++ app/user/routes.py | 38 ++++++++++++++- 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 app/templates/user/edit_note.html diff --git a/app/templates/user/edit_note.html b/app/templates/user/edit_note.html new file mode 100644 index 00000000..6ab886a6 --- /dev/null +++ b/app/templates/user/edit_note.html @@ -0,0 +1,69 @@ +{% if theme() and file_exists('app/templates/themes/' + theme() + '/base.html') -%} + {% extends 'themes/' + theme() + '/base.html' %} +{% else -%} + {% extends "base.html" %} +{% endif -%} +{% from 'bootstrap/form.html' import render_form %} + +{% block app_content %} +
+ +
+ +{% endblock %} diff --git a/app/templates/user/show_profile.html b/app/templates/user/show_profile.html index 5ef64f2f..f50865dd 100644 --- a/app/templates/user/show_profile.html +++ b/app/templates/user/show_profile.html @@ -100,6 +100,7 @@ {% endif -%} {% endif -%}
  • {{ _('Report') }}
  • +
  • {{ _('Edit note') }}
  • {% endif %} diff --git a/app/user/forms.py b/app/user/forms.py index dc702ad2..e17407ce 100644 --- a/app/user/forms.py +++ b/app/user/forms.py @@ -147,3 +147,8 @@ class RemoteFollowForm(FlaskForm): instance_type = SelectField(_l('Instance type'), choices=type_choices, render_kw={'class': 'form-select'}) submit = SubmitField(_l('View profile on remote instance')) + + +class UserNoteForm(FlaskForm): + note = StringField(_l('User note'), validators=[Optional(), Length(max=50)]) + submit = SubmitField(_l('Save note')) diff --git a/app/user/routes.py b/app/user/routes.py index 5bc30c38..12a6e7a9 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -16,10 +16,10 @@ from app.constants import * from app.email import send_verification_email from app.models import Post, Community, CommunityMember, User, PostReply, PostVote, Notification, utcnow, File, Site, \ Instance, Report, UserBlock, CommunityBan, CommunityJoinRequest, CommunityBlock, Filter, Domain, DomainBlock, \ - InstanceBlock, NotificationSubscription, PostBookmark, PostReplyBookmark, read_posts, Topic + InstanceBlock, NotificationSubscription, PostBookmark, PostReplyBookmark, read_posts, Topic, UserNote from app.user import bp from app.user.forms import ProfileForm, SettingsForm, DeleteAccountForm, ReportUserForm, \ - FilterForm, KeywordFilterEditForm, RemoteFollowForm, ImportExportForm + FilterForm, KeywordFilterEditForm, RemoteFollowForm, ImportExportForm, UserNoteForm from app.user.utils import purge_user_then_delete, unsubscribe_from_community from app.utils import get_setting, render_template, markdown_to_html, user_access, markdown_to_text, shorten_string, \ is_image_url, ensure_directory_exists, gibberish, file_get_contents, community_membership, user_filters_home, \ @@ -1330,3 +1330,37 @@ def user_read_posts_delete(): db.session.commit() flash(_('Reading history has been deleted')) return redirect(url_for('user.user_read_posts')) + + +@bp.route('/u//note', methods=['GET', 'POST']) +@login_required +def edit_user_note(actor): + actor = actor.strip() + if '@' in actor: + user: User = User.query.filter_by(ap_id=actor, deleted=False, banned=False).first() + else: + user: User = User.query.filter_by(user_name=actor, deleted=False, ap_id=None).first() + if user is None: + abort(404) + form = UserNoteForm() + if form.validate_on_submit() and not current_user.banned: + text = form.note.data.strip() + usernote = UserNote.query.filter(UserNote.target_id == user.id, UserNote.user_id == current_user.id).first() + if usernote: + usernote.body = text + else: + usernote = UserNote(target_id=user.id, user_id=current_user.id, body=text) + db.session.add(usernote) + db.session.commit() + + flash(_('Your changes have been saved.'), 'success') + referrer = request.headers.get('Referer', None) + if referrer is not None: + return redirect(referrer) + else: + return redirect(url_for('user.edit_user_note', actor=actor)) + elif request.method == 'GET': + form.note.data = user.get_note(current_user) + + return render_template('user/edit_note.html', title=_('Edit note'), form=form, user=user, + menu_topics=menu_topics(), site=g.site)