From 4fcdcc0b77ba7bbce5751b090391c3ce29a9ed4c Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Tue, 10 Sep 2024 08:29:44 +1200 Subject: [PATCH] move import and export to dedicated form #316 rather than mixing it in with the settings --- app/templates/user/_user_nav.html | 3 + app/templates/user/edit_settings.html | 7 --- app/templates/user/import_export.html | 35 ++++++++++++ app/user/forms.py | 8 ++- app/user/routes.py | 81 +++++++++++++++++---------- 5 files changed, 95 insertions(+), 39 deletions(-) create mode 100644 app/templates/user/import_export.html diff --git a/app/templates/user/_user_nav.html b/app/templates/user/_user_nav.html index 8a5e5021..51f50caf 100644 --- a/app/templates/user/_user_nav.html +++ b/app/templates/user/_user_nav.html @@ -10,6 +10,9 @@ {{ _('Blocks & Filters') }} + + {{ _('Import & Export') }} + \ No newline at end of file diff --git a/app/templates/user/edit_settings.html b/app/templates/user/edit_settings.html index 233a1eea..4c6ea85f 100644 --- a/app/templates/user/edit_settings.html +++ b/app/templates/user/edit_settings.html @@ -34,14 +34,7 @@ {{ render_field(form.default_filter) }} {{ render_field(form.theme) }} {{ render_field(form.vote_privately) }} -
Import
- {{ render_field(form.import_file) }} {{ render_field(form.submit) }} -
-
Export
-

Export community subscriptions and user settings

- {{ render_field(form.export_settings) }} -

{{ _('Delete account') }} diff --git a/app/templates/user/import_export.html b/app/templates/user/import_export.html new file mode 100644 index 00000000..15c40d5f --- /dev/null +++ b/app/templates/user/import_export.html @@ -0,0 +1,35 @@ +{% 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_field %} +{% set active_child = 'settings' %} + +{% block app_content %} + +

+
+ +

{{ _('Change settings') }}

+ {% include "user/_user_nav.html" %} +
+ {{ form.csrf_token() }} +
Import
+ {{ render_field(form.import_file) }} + {{ render_field(form.submit) }} +
+
Export
+

Export community subscriptions and user settings

+ {{ render_field(form.export_settings) }} +
+
+
+
+{% endblock %} diff --git a/app/user/forms.py b/app/user/forms.py index 4ee33815..7a4b3e5a 100644 --- a/app/user/forms.py +++ b/app/user/forms.py @@ -45,8 +45,6 @@ class SettingsForm(FlaskForm): indexable = BooleanField(_l('My posts appear in search results')) manually_approves_followers = BooleanField(_l('Manually approve followers')) vote_privately = BooleanField(_l('Vote privately')) - import_file = FileField(_l('Import community subscriptions and user blocks from Lemmy')) - export_settings = SubmitField(_l('Export')) sorts = [('hot', _l('Hot')), ('top', _l('Top')), ('new', _l('New')), @@ -65,6 +63,12 @@ class SettingsForm(FlaskForm): submit = SubmitField(_l('Save settings')) +class ImportExportForm(FlaskForm): + import_file = FileField(_l('Import community subscriptions and user blocks from Lemmy')) + export_settings = SubmitField(_l('Export')) + submit = SubmitField(_l('Import')) + + class DeleteAccountForm(FlaskForm): submit = SubmitField(_l('Yes, delete my account')) diff --git a/app/user/routes.py b/app/user/routes.py index 9b4aae3c..1c20449c 100644 --- a/app/user/routes.py +++ b/app/user/routes.py @@ -20,7 +20,7 @@ from app.models import Post, Community, CommunityMember, User, PostReply, PostVo InstanceBlock, NotificationSubscription, PostBookmark, PostReplyBookmark from app.user import bp from app.user.forms import ProfileForm, SettingsForm, DeleteAccountForm, ReportUserForm, \ - FilterForm, KeywordFilterEditForm, RemoteFollowForm + FilterForm, KeywordFilterEditForm, RemoteFollowForm, ImportExportForm 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, \ @@ -372,21 +372,7 @@ def change_settings(): ('de', _l('German')), ('ja', _l('Japanese')), ] - # separate if to handle just the 'Export' button being clicked - if form.export_settings.data and form.validate(): - # get the user settings for this user - buffer = export_user_settings(user) - - # confirmation displayed to user when the page loads up again - flash(_('Export Complete.')) - - # send the file to the user as a download - # the as_attachment=True results in flask - # redirecting to the current page, so no - # url_for needed here - return send_file(buffer, download_name=f'{user.user_name}_piefed_settings.json', as_attachment=True, - mimetype='application/json') - elif form.validate_on_submit(): + if form.validate_on_submit(): propagate_indexable = form.indexable.data != current_user.indexable current_user.newsletter = form.newsletter.data current_user.searchable = form.searchable.data @@ -403,11 +389,57 @@ def change_settings(): current_user.alt_user_name = gibberish(randint(8, 20)) else: current_user.alt_user_name = '' - import_file = request.files['import_file'] if propagate_indexable: db.session.execute(text('UPDATE "post" set indexable = :indexable WHERE user_id = :user_id'), {'user_id': current_user.id, 'indexable': current_user.indexable}) + + db.session.commit() + + flash(_('Your changes have been saved.'), 'success') + return redirect(url_for('user.change_settings')) + elif request.method == 'GET': + form.newsletter.data = current_user.newsletter + form.email_unread.data = current_user.email_unread + form.searchable.data = current_user.searchable + form.indexable.data = current_user.indexable + form.default_sort.data = current_user.default_sort + form.default_filter.data = current_user.default_filter + form.theme.data = current_user.theme + form.markdown_editor.data = current_user.markdown_editor + form.interface_language.data = current_user.interface_language + form.vote_privately.data = current_user.vote_privately() + + return render_template('user/edit_settings.html', title=_('Edit profile'), form=form, user=current_user, + moderating_communities=moderating_communities(current_user.get_id()), + joined_communities=joined_communities(current_user.get_id()), + menu_topics=menu_topics(), site=g.site + ) + + +@bp.route('/user/settings/import_export', methods=['GET', 'POST']) +@login_required +def user_settings_import_export(): + user = User.query.filter_by(id=current_user.id, deleted=False, banned=False, ap_id=None).first() + if user is None: + abort(404) + form = ImportExportForm() + # separate if to handle just the 'Export' button being clicked + if form.export_settings.data and form.validate(): + # get the user settings for this user + buffer = export_user_settings(user) + + # confirmation displayed to user when the page loads up again + flash(_('Export Complete.')) + + # send the file to the user as a download + # the as_attachment=True results in flask + # redirecting to the current page, so no + # url_for needed here + return send_file(buffer, download_name=f'{user.user_name}_piefed_settings.json', as_attachment=True, + mimetype='application/json') + elif form.validate_on_submit(): + import_file = request.files['import_file'] if import_file and import_file.filename != '': file_ext = os.path.splitext(import_file.filename)[1] if file_ext.lower() != '.json': @@ -428,20 +460,9 @@ def change_settings(): db.session.commit() flash(_('Your changes have been saved.'), 'success') - return redirect(url_for('user.change_settings')) - elif request.method == 'GET': - form.newsletter.data = current_user.newsletter - form.email_unread.data = current_user.email_unread - form.searchable.data = current_user.searchable - form.indexable.data = current_user.indexable - form.default_sort.data = current_user.default_sort - form.default_filter.data = current_user.default_filter - form.theme.data = current_user.theme - form.markdown_editor.data = current_user.markdown_editor - form.interface_language.data = current_user.interface_language - form.vote_privately.data = current_user.vote_privately() + return redirect(url_for('user.user_settings_import_export')) - return render_template('user/edit_settings.html', title=_('Edit profile'), form=form, user=current_user, + return render_template('user/import_export.html', title=_('Import & Export'), form=form, user=current_user, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), menu_topics=menu_topics(), site=g.site