From 788e282dce34b4f35e143376add194c9af41f7c9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 15:12:36 -0700 Subject: [PATCH 01/12] Moves user admin pages into their own directory --- bookwyrm/templates/moderation/reports.html | 2 +- .../templates/{settings => user_admin}/server_filter.html | 0 bookwyrm/templates/{settings => user_admin}/user_admin.html | 2 +- .../{settings => user_admin}/user_admin_filters.html | 4 ++-- .../templates/{settings => user_admin}/username_filter.html | 0 bookwyrm/views/user_admin.py | 2 +- 6 files changed, 5 insertions(+), 5 deletions(-) rename bookwyrm/templates/{settings => user_admin}/server_filter.html (100%) rename bookwyrm/templates/{settings => user_admin}/user_admin.html (97%) rename bookwyrm/templates/{settings => user_admin}/user_admin_filters.html (51%) rename bookwyrm/templates/{settings => user_admin}/username_filter.html (100%) diff --git a/bookwyrm/templates/moderation/reports.html b/bookwyrm/templates/moderation/reports.html index 72cadae5..f9d9d99b 100644 --- a/bookwyrm/templates/moderation/reports.html +++ b/bookwyrm/templates/moderation/reports.html @@ -30,7 +30,7 @@ -{% include 'settings/user_admin_filters.html' %} +{% include 'user_admin/user_admin_filters.html' %}
{% if not reports %} diff --git a/bookwyrm/templates/settings/server_filter.html b/bookwyrm/templates/user_admin/server_filter.html similarity index 100% rename from bookwyrm/templates/settings/server_filter.html rename to bookwyrm/templates/user_admin/server_filter.html diff --git a/bookwyrm/templates/settings/user_admin.html b/bookwyrm/templates/user_admin/user_admin.html similarity index 97% rename from bookwyrm/templates/settings/user_admin.html rename to bookwyrm/templates/user_admin/user_admin.html index a96d37f5..554fbdd3 100644 --- a/bookwyrm/templates/settings/user_admin.html +++ b/bookwyrm/templates/user_admin/user_admin.html @@ -13,7 +13,7 @@ {% block panel %} -{% include 'settings/user_admin_filters.html' %} +{% include 'user_admin/user_admin_filters.html' %} diff --git a/bookwyrm/templates/settings/user_admin_filters.html b/bookwyrm/templates/user_admin/user_admin_filters.html similarity index 51% rename from bookwyrm/templates/settings/user_admin_filters.html rename to bookwyrm/templates/user_admin/user_admin_filters.html index a7b5c8aa..57e017e5 100644 --- a/bookwyrm/templates/settings/user_admin_filters.html +++ b/bookwyrm/templates/user_admin/user_admin_filters.html @@ -1,6 +1,6 @@ {% extends 'snippets/filters_panel/filters_panel.html' %} {% block filter_fields %} -{% include 'settings/server_filter.html' %} -{% include 'settings/username_filter.html' %} +{% include 'user_admin/server_filter.html' %} +{% include 'user_admin/username_filter.html' %} {% endblock %} diff --git a/bookwyrm/templates/settings/username_filter.html b/bookwyrm/templates/user_admin/username_filter.html similarity index 100% rename from bookwyrm/templates/settings/username_filter.html rename to bookwyrm/templates/user_admin/username_filter.html diff --git a/bookwyrm/views/user_admin.py b/bookwyrm/views/user_admin.py index cc3ff841..8b4704d0 100644 --- a/bookwyrm/views/user_admin.py +++ b/bookwyrm/views/user_admin.py @@ -54,4 +54,4 @@ class UserAdmin(View): "sort": sort, "server": server, } - return TemplateResponse(request, "settings/user_admin.html", data) + return TemplateResponse(request, "user_admin/user_admin.html", data) From 0dd8b339910708eaf82d342fc13ff39dd096705f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 15:19:55 -0700 Subject: [PATCH 02/12] Moves user admin templates into snippets --- bookwyrm/templates/moderation/report.html | 71 +------------------ bookwyrm/templates/user_admin/user.html | 19 +++++ bookwyrm/templates/user_admin/user_info.html | 56 +++++++++++++++ .../user_admin/user_moderation_actions.html | 17 +++++ 4 files changed, 94 insertions(+), 69 deletions(-) create mode 100644 bookwyrm/templates/user_admin/user.html create mode 100644 bookwyrm/templates/user_admin/user_info.html create mode 100644 bookwyrm/templates/user_admin/user_moderation_actions.html diff --git a/bookwyrm/templates/moderation/report.html b/bookwyrm/templates/moderation/report.html index 1cadd28d..a078fe45 100644 --- a/bookwyrm/templates/moderation/report.html +++ b/bookwyrm/templates/moderation/report.html @@ -15,76 +15,9 @@ {% include 'moderation/report_preview.html' with report=report %} -
-
-

{% trans "User details" %}

-
- {% include 'user/user_preview.html' with user=report.user %} - {% if report.user.summary %} -
- {{ report.user.summary | to_markdown | safe }} -
- {% endif %} +{% include 'user_admin/user_info.html' with user=report.user %} -

{% trans "View user profile" %}

-
-
- {% if not report.user.local %} - {% with server=report.user.federated_server %} -
-

{% trans "Instance details" %}

-
- {% if server %} -
{{ server.server_name }}
-
-
-
{% trans "Software:" %}
-
{{ server.application_type }}
-
-
-
{% trans "Version:" %}
-
{{ server.application_version }}
-
-
-
{% trans "Status:" %}
-
{{ server.status }}
-
-
- {% if server.notes %} -
{% trans "Notes" %}
-
- {{ server.notes }} -
- {% endif %} - -

- {% trans "View instance" %} -

- {% else %} - {% trans "Not set" %} - {% endif %} -
-
- {% endwith %} - {% endif %} -
- -
-

{% trans "Actions" %}

-
-

- {% trans "Send direct message" %} -

-
- {% csrf_token %} - {% if report.user.is_active %} - - {% else %} - - {% endif %} - -
-
+{% include 'user_admin/user_moderation_actions.html' with user=report.user %}

{% trans "Moderator Comments" %}

diff --git a/bookwyrm/templates/user_admin/user.html b/bookwyrm/templates/user_admin/user.html new file mode 100644 index 00000000..7bb6abfa --- /dev/null +++ b/bookwyrm/templates/user_admin/user.html @@ -0,0 +1,19 @@ +{% extends 'settings/admin_layout.html' %} +{% load i18n %} +{% load bookwyrm_tags %} +{% load humanize %} + +{% block title %}{{ username }}{% endblock %} +{% block header %}{{ username }}{% endblock %} + +{% block panel %} + + +{% include 'user_admin/user_info.html' with user=user %} + +{% include 'user_admin/user_moderation_actions.html' with user=user %} + +{% endblock %} + diff --git a/bookwyrm/templates/user_admin/user_info.html b/bookwyrm/templates/user_admin/user_info.html new file mode 100644 index 00000000..e5f5d580 --- /dev/null +++ b/bookwyrm/templates/user_admin/user_info.html @@ -0,0 +1,56 @@ +{% load i18n %} +{% load bookwyrm_tags %} +
+
+

{% trans "User details" %}

+
+ {% include 'user/user_preview.html' with user=user %} + {% if user.summary %} +
+ {{ user.summary | to_markdown | safe }} +
+ {% endif %} + +

{% trans "View user profile" %}

+
+
+ {% if not user.local %} + {% with server=user.federated_server %} +
+

{% trans "Instance details" %}

+
+ {% if server %} +
{{ server.server_name }}
+
+
+
{% trans "Software:" %}
+
{{ server.application_type }}
+
+
+
{% trans "Version:" %}
+
{{ server.application_version }}
+
+
+
{% trans "Status:" %}
+
{{ server.status }}
+
+
+ {% if server.notes %} +
{% trans "Notes" %}
+
+ {{ server.notes }} +
+ {% endif %} + +

+ {% trans "View instance" %} +

+ {% else %} + {% trans "Not set" %} + {% endif %} +
+
+ {% endwith %} + {% endif %} +
+ diff --git a/bookwyrm/templates/user_admin/user_moderation_actions.html b/bookwyrm/templates/user_admin/user_moderation_actions.html new file mode 100644 index 00000000..41010994 --- /dev/null +++ b/bookwyrm/templates/user_admin/user_moderation_actions.html @@ -0,0 +1,17 @@ +{% load i18n %} +
+

{% trans "Actions" %}

+
+

+ {% trans "Send direct message" %} +

+
+ {% csrf_token %} + {% if user.is_active %} + + {% else %} + + {% endif %} + +
+
From 1542866b6c9e07393ca03067c27475a807155f90 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 15:40:51 -0700 Subject: [PATCH 03/12] Adds user admin view --- bookwyrm/templates/user_admin/user.html | 2 +- bookwyrm/templates/user_admin/user_admin.html | 2 +- .../user_admin/user_moderation_actions.html | 2 +- bookwyrm/urls.py | 13 ++++++++++--- bookwyrm/views/__init__.py | 2 +- bookwyrm/views/user_admin.py | 16 +++++++++++++++- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/bookwyrm/templates/user_admin/user.html b/bookwyrm/templates/user_admin/user.html index 7bb6abfa..a33c7110 100644 --- a/bookwyrm/templates/user_admin/user.html +++ b/bookwyrm/templates/user_admin/user.html @@ -8,7 +8,7 @@ {% block panel %} {% include 'user_admin/user_info.html' with user=user %} diff --git a/bookwyrm/templates/user_admin/user_admin.html b/bookwyrm/templates/user_admin/user_admin.html index 554fbdd3..2ab526a9 100644 --- a/bookwyrm/templates/user_admin/user_admin.html +++ b/bookwyrm/templates/user_admin/user_admin.html @@ -41,7 +41,7 @@
{% for user in users %} - + diff --git a/bookwyrm/templates/user_admin/user_moderation_actions.html b/bookwyrm/templates/user_admin/user_moderation_actions.html index 41010994..043ef386 100644 --- a/bookwyrm/templates/user_admin/user_moderation_actions.html +++ b/bookwyrm/templates/user_admin/user_moderation_actions.html @@ -5,7 +5,7 @@

{% trans "Send direct message" %}

- + {% csrf_token %} {% if user.is_active %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 9e280f3e..5768ee01 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -51,13 +51,20 @@ urlpatterns = [ r"^password-reset/(?P[A-Za-z0-9]+)/?$", views.PasswordReset.as_view() ), # admin - re_path(r"^settings/site-settings", views.Site.as_view(), name="settings-site"), + re_path(r"^settings/site-settings/?$", views.Site.as_view(), name="settings-site"), re_path( - r"^settings/email-preview", + r"^settings/email-preview/?$", views.site.email_preview, name="settings-email-preview", ), - re_path(r"^settings/users", views.UserAdmin.as_view(), name="settings-users"), + re_path( + r"^settings/users/?$", views.UserAdminList.as_view(), name="settings-users" + ), + re_path( + r"^settings/users/(?P\d+)/?$", + views.UserAdmin.as_view(), + name="settings-user" + ), re_path( r"^settings/federation/?$", views.Federation.as_view(), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 9f8463b4..7714bbda 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -37,5 +37,5 @@ from .status import CreateStatus, DeleteStatus, DeleteAndRedraft from .tag import Tag, AddTag, RemoveTag from .updates import get_notification_count, get_unread_status_count from .user import User, EditUser, Followers, Following -from .user_admin import UserAdmin +from .user_admin import UserAdmin, UserAdminList from .wellknown import * diff --git a/bookwyrm/views/user_admin.py b/bookwyrm/views/user_admin.py index 8b4704d0..895c1464 100644 --- a/bookwyrm/views/user_admin.py +++ b/bookwyrm/views/user_admin.py @@ -1,6 +1,7 @@ """ manage user """ from django.contrib.auth.decorators import login_required, permission_required from django.core.paginator import Paginator +from django.shortcuts import get_object_or_404 from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View @@ -15,7 +16,7 @@ from bookwyrm.settings import PAGE_LENGTH permission_required("bookwyrm.moderate_users", raise_exception=True), name="dispatch", ) -class UserAdmin(View): +class UserAdminList(View): """ admin view of users on this server """ def get(self, request): @@ -55,3 +56,16 @@ class UserAdmin(View): "server": server, } return TemplateResponse(request, "user_admin/user_admin.html", data) + +@method_decorator(login_required, name="dispatch") +@method_decorator( + permission_required("bookwyrm.moderate_users", raise_exception=True), + name="dispatch", +) +class UserAdmin(View): + """ moderate an individual user """ + + def get(self, request, user): + """ user view """ + user = get_object_or_404(models.User, id=user) + return TemplateResponse(request, "user_admin/user.html", {"user": user}) From da19089d34ffb374b8c09a0f2c0917e722aa7dbe Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 15:51:39 -0700 Subject: [PATCH 04/12] Changes language from "deactivate" to "suspend" --- .../templates/user_admin/user_moderation_actions.html | 6 +++--- bookwyrm/urls.py | 8 ++++---- bookwyrm/views/__init__.py | 2 +- bookwyrm/views/reports.py | 11 ++++++----- bookwyrm/views/user_admin.py | 1 + 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bookwyrm/templates/user_admin/user_moderation_actions.html b/bookwyrm/templates/user_admin/user_moderation_actions.html index 043ef386..311cbb4b 100644 --- a/bookwyrm/templates/user_admin/user_moderation_actions.html +++ b/bookwyrm/templates/user_admin/user_moderation_actions.html @@ -5,12 +5,12 @@

{% trans "Send direct message" %}

- + {% csrf_token %} {% if user.is_active %} - + {% else %} - + {% endif %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 5768ee01..8c5266a6 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -63,7 +63,7 @@ urlpatterns = [ re_path( r"^settings/users/(?P\d+)/?$", views.UserAdmin.as_view(), - name="settings-user" + name="settings-user", ), re_path( r"^settings/federation/?$", @@ -120,9 +120,9 @@ urlpatterns = [ name="settings-report", ), re_path( - r"^settings/reports/(?P\d+)/deactivate/?$", - views.deactivate_user, - name="settings-report-deactivate", + r"^settings/reports/(?P\d+)/suspend/?$", + views.suspend_user, + name="settings-report-suspend", ), re_path( r"^settings/reports/(?P\d+)/resolve/?$", diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 7714bbda..c0f35ba8 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -25,7 +25,7 @@ from .notifications import Notifications from .outbox import Outbox from .reading import edit_readthrough, create_readthrough, delete_readthrough from .reading import start_reading, finish_reading, delete_progressupdate -from .reports import Report, Reports, make_report, resolve_report, deactivate_user +from .reports import Report, Reports, make_report, resolve_report, suspend_user from .rss_feed import RssFeed from .password import PasswordResetRequest, PasswordReset, ChangePassword from .search import Search diff --git a/bookwyrm/views/reports.py b/bookwyrm/views/reports.py index 3dd53cb9..07eb9b97 100644 --- a/bookwyrm/views/reports.py +++ b/bookwyrm/views/reports.py @@ -74,12 +74,13 @@ class Report(View): @login_required @permission_required("bookwyrm_moderate_user") -def deactivate_user(_, report_id): +def suspend_user(_, user_id): """ mark an account as inactive """ - report = get_object_or_404(models.Report, id=report_id) - report.user.is_active = not report.user.is_active - report.user.save() - return redirect("settings-report", report.id) + user = get_object_or_404(models.User, id=user_id) + user.is_active = not user.is_active + # this isn't a full deletion, so we don't want to tell the world + user.save(broadcast=False) + return redirect("settings-user", user.id) @login_required diff --git a/bookwyrm/views/user_admin.py b/bookwyrm/views/user_admin.py index 895c1464..0aea74ed 100644 --- a/bookwyrm/views/user_admin.py +++ b/bookwyrm/views/user_admin.py @@ -57,6 +57,7 @@ class UserAdminList(View): } return TemplateResponse(request, "user_admin/user_admin.html", data) + @method_decorator(login_required, name="dispatch") @method_decorator( permission_required("bookwyrm.moderate_users", raise_exception=True), From 35cb3fa8fce6644a7653edc43e980bffeb71df69 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 16:08:01 -0700 Subject: [PATCH 05/12] Updates tests --- bookwyrm/templates/user_admin/user.html | 4 ++-- bookwyrm/tests/views/test_reports.py | 10 +++------- bookwyrm/tests/views/test_user_admin.py | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bookwyrm/templates/user_admin/user.html b/bookwyrm/templates/user_admin/user.html index a33c7110..46390650 100644 --- a/bookwyrm/templates/user_admin/user.html +++ b/bookwyrm/templates/user_admin/user.html @@ -3,8 +3,8 @@ {% load bookwyrm_tags %} {% load humanize %} -{% block title %}{{ username }}{% endblock %} -{% block header %}{{ username }}{% endblock %} +{% block title %}{{ user.username }}{% endblock %} +{% block header %}{{ user.username }}{% endblock %} {% block panel %}
diff --git a/bookwyrm/tests/views/test_reports.py b/bookwyrm/tests/views/test_reports.py index 1c56067a..55f81570 100644 --- a/bookwyrm/tests/views/test_reports.py +++ b/bookwyrm/tests/views/test_reports.py @@ -1,5 +1,4 @@ """ test for app action functionality """ -from unittest.mock import patch from django.template.response import TemplateResponse from django.test import TestCase from django.test.client import RequestFactory @@ -115,22 +114,19 @@ class ReportViews(TestCase): report.refresh_from_db() self.assertFalse(report.resolved) - def test_deactivate_user(self): + def test_suspend_user(self): """ toggle whether a user is able to log in """ self.assertTrue(self.rat.is_active) - report = models.Report.objects.create(reporter=self.local_user, user=self.rat) request = self.factory.post("") request.user = self.local_user request.user.is_superuser = True # de-activate - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - views.deactivate_user(request, report.id) + views.suspend_user(request, self.local_user.id) self.rat.refresh_from_db() self.assertFalse(self.rat.is_active) # re-activate - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): - views.deactivate_user(request, report.id) + views.suspend_user(request, self.local_user.id) self.rat.refresh_from_db() self.assertTrue(self.rat.is_active) diff --git a/bookwyrm/tests/views/test_user_admin.py b/bookwyrm/tests/views/test_user_admin.py index dd20c1b6..4bb83812 100644 --- a/bookwyrm/tests/views/test_user_admin.py +++ b/bookwyrm/tests/views/test_user_admin.py @@ -21,9 +21,9 @@ class UserAdminViews(TestCase): ) models.SiteSettings.objects.create() - def test_user_admin_page(self): + def test_user_admin_list_page(self): """ there are so many views, this just makes sure it LOADS """ - view = views.UserAdmin.as_view() + view = views.UserAdminList.as_view() request = self.factory.get("") request.user = self.local_user request.user.is_superuser = True @@ -31,3 +31,14 @@ class UserAdminViews(TestCase): self.assertIsInstance(result, TemplateResponse) result.render() self.assertEqual(result.status_code, 200) + + def test_user_admin_page(self): + """ there are so many views, this just makes sure it LOADS """ + view = views.UserAdmin.as_view() + request = self.factory.get("") + request.user = self.local_user + request.user.is_superuser = True + result = view(request, self.local_user) + self.assertIsInstance(result, TemplateResponse) + result.render() + self.assertEqual(result.status_code, 200) From 70a819951ef922c97d4a3f23ff8e970404e07b44 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 16:18:29 -0700 Subject: [PATCH 06/12] Fixes localization weirdness with dates --- bookwyrm/templates/book/publisher_info.html | 2 +- .../snippets/status/status_body.html | 2 +- bookwyrm/templatetags/bookwyrm_tags.py | 22 ------------------- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/bookwyrm/templates/book/publisher_info.html b/bookwyrm/templates/book/publisher_info.html index a16332c5..ca9265c5 100644 --- a/bookwyrm/templates/book/publisher_info.html +++ b/bookwyrm/templates/book/publisher_info.html @@ -39,7 +39,7 @@ {% endif %}

- {% with date=book.published_date|date:'M jS Y' publisher=book.publishers|join:', ' %} + {% with date=book.published_date|date:'M j Y' publisher=book.publishers|join:', ' %} {% if date or book.first_published_date %}

{% if not moderation_mode %}
- {{ related_status.published_date | post_date }} + {{ related_status.published_date|timesince }} {% include 'snippets/privacy-icons.html' with item=related_status %}
diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index b4dc517f..2fadb978 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -181,36 +181,6 @@ class TemplateTags(TestCase): uuid = bookwyrm_tags.get_uuid("hi") self.assertTrue(re.match(r"hi[A-Za-z0-9\-]", uuid)) - def test_time_since(self, _): - """ ultraconcise timestamps """ - self.assertEqual(bookwyrm_tags.time_since("bleh"), "") - - now = timezone.now() - self.assertEqual(bookwyrm_tags.time_since(now), "0s") - - seconds_ago = now - relativedelta(seconds=4) - self.assertEqual(bookwyrm_tags.time_since(seconds_ago), "4s") - - minutes_ago = now - relativedelta(minutes=8) - self.assertEqual(bookwyrm_tags.time_since(minutes_ago), "8m") - - hours_ago = now - relativedelta(hours=9) - self.assertEqual(bookwyrm_tags.time_since(hours_ago), "9h") - - days_ago = now - relativedelta(days=3) - self.assertEqual(bookwyrm_tags.time_since(days_ago), "3d") - - # I am not going to figure out how to mock dates tonight. - months_ago = now - relativedelta(months=5) - self.assertTrue( - re.match(r"[A-Z][a-z]{2} \d?\d", bookwyrm_tags.time_since(months_ago)) - ) - - years_ago = now - relativedelta(years=10) - self.assertTrue( - re.match(r"[A-Z][a-z]{2} \d?\d \d{4}", bookwyrm_tags.time_since(years_ago)) - ) - def test_get_markdown(self, _): """ mardown format data """ result = bookwyrm_tags.get_markdown("_hi_") From e1d24577d458ad883cac49ee4543537eae5971e9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 16:42:51 -0700 Subject: [PATCH 09/12] Fixes tests (again) --- bookwyrm/tests/views/test_reports.py | 4 ++-- bookwyrm/tests/views/test_user_admin.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bookwyrm/tests/views/test_reports.py b/bookwyrm/tests/views/test_reports.py index 55f81570..bce19993 100644 --- a/bookwyrm/tests/views/test_reports.py +++ b/bookwyrm/tests/views/test_reports.py @@ -122,11 +122,11 @@ class ReportViews(TestCase): request.user.is_superuser = True # de-activate - views.suspend_user(request, self.local_user.id) + views.suspend_user(request, self.rat.id) self.rat.refresh_from_db() self.assertFalse(self.rat.is_active) # re-activate - views.suspend_user(request, self.local_user.id) + views.suspend_user(request, self.rat.id) self.rat.refresh_from_db() self.assertTrue(self.rat.is_active) diff --git a/bookwyrm/tests/views/test_user_admin.py b/bookwyrm/tests/views/test_user_admin.py index 4bb83812..1884d000 100644 --- a/bookwyrm/tests/views/test_user_admin.py +++ b/bookwyrm/tests/views/test_user_admin.py @@ -38,7 +38,9 @@ class UserAdminViews(TestCase): request = self.factory.get("") request.user = self.local_user request.user.is_superuser = True - result = view(request, self.local_user) + + result = view(request, self.local_user.id) + self.assertIsInstance(result, TemplateResponse) result.render() self.assertEqual(result.status_code, 200) From 0a37b4665ca19f8bf9d722e51e1c9a4559e5bb3f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 17:16:14 -0700 Subject: [PATCH 10/12] Adds control for moderator to set user group --- bookwyrm/forms.py | 5 ++++ .../user_admin/user_moderation_actions.html | 25 +++++++++++++++++++ bookwyrm/views/user_admin.py | 14 +++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index 80b28310..ac5bd027 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -149,6 +149,11 @@ class LimitedEditUserForm(CustomForm): ] help_texts = {f: None for f in fields} +class UserGroupForm(CustomForm): + class Meta: + model = models.User + fields = ["groups"] + class TagForm(CustomForm): class Meta: diff --git a/bookwyrm/templates/user_admin/user_moderation_actions.html b/bookwyrm/templates/user_admin/user_moderation_actions.html index 311cbb4b..816e787a 100644 --- a/bookwyrm/templates/user_admin/user_moderation_actions.html +++ b/bookwyrm/templates/user_admin/user_moderation_actions.html @@ -14,4 +14,29 @@ {% endif %} + {% if user.local %} +
+
+ {% csrf_token %} + + {% if group_form.non_field_errors %} + {{ group_form.non_field_errors }} + {% endif %} + {% with group=user.groups.first %} +
+ +
+ {% for error in group_form.groups.errors %} +

{{ error | escape }}

+ {% endfor %} + {% endwith %} + + +
+ {% endif %} diff --git a/bookwyrm/views/user_admin.py b/bookwyrm/views/user_admin.py index 0aea74ed..3ea7016a 100644 --- a/bookwyrm/views/user_admin.py +++ b/bookwyrm/views/user_admin.py @@ -6,7 +6,7 @@ from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View -from bookwyrm import models +from bookwyrm import forms, models from bookwyrm.settings import PAGE_LENGTH @@ -69,4 +69,14 @@ class UserAdmin(View): def get(self, request, user): """ user view """ user = get_object_or_404(models.User, id=user) - return TemplateResponse(request, "user_admin/user.html", {"user": user}) + data = {"user": user, "group_form": forms.UserGroupForm()} + return TemplateResponse(request, "user_admin/user.html", data) + + def post(self, request, user): + """ update user group """ + user = get_object_or_404(models.User, id=user) + form = forms.UserGroupForm(request.POST, instance=user) + if form.is_valid(): + form.save() + data = {"user": user, "group_form": form} + return TemplateResponse(request, "user_admin/user.html", data) From 63509c954da2192058abb2cfcb88f9009fcbc9fc Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 18:12:55 -0700 Subject: [PATCH 11/12] Adds test for changing user group --- bookwyrm/forms.py | 1 + bookwyrm/tests/views/test_user_admin.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index ac5bd027..1f1f1a3b 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -149,6 +149,7 @@ class LimitedEditUserForm(CustomForm): ] help_texts = {f: None for f in fields} + class UserGroupForm(CustomForm): class Meta: model = models.User diff --git a/bookwyrm/tests/views/test_user_admin.py b/bookwyrm/tests/views/test_user_admin.py index 1884d000..4e1e106d 100644 --- a/bookwyrm/tests/views/test_user_admin.py +++ b/bookwyrm/tests/views/test_user_admin.py @@ -1,4 +1,5 @@ """ test for app action functionality """ +from django.contrib.auth.models import Group from django.template.response import TemplateResponse from django.test import TestCase from django.test.client import RequestFactory @@ -44,3 +45,24 @@ class UserAdminViews(TestCase): self.assertIsInstance(result, TemplateResponse) result.render() self.assertEqual(result.status_code, 200) + + def test_user_admin_page_post(self): + """ set the user's group """ + group = Group.objects.create(name="editor") + self.assertEqual( + list(self.local_user.groups.values_list("name", flat=True)), [] + ) + + view = views.UserAdmin.as_view() + request = self.factory.post("", {"groups": [group.id]}) + request.user = self.local_user + request.user.is_superuser = True + + result = view(request, self.local_user.id) + + self.assertIsInstance(result, TemplateResponse) + result.render() + + self.assertEqual( + list(self.local_user.groups.values_list("name", flat=True)), ["editor"] + ) From 2c013b677bb8a947840f1a7de38803cff1badcc1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Apr 2021 20:01:29 -0700 Subject: [PATCH 12/12] Patch broadcast in test --- bookwyrm/tests/views/test_user_admin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/tests/views/test_user_admin.py b/bookwyrm/tests/views/test_user_admin.py index 4e1e106d..b1e9d639 100644 --- a/bookwyrm/tests/views/test_user_admin.py +++ b/bookwyrm/tests/views/test_user_admin.py @@ -1,4 +1,5 @@ """ test for app action functionality """ +from unittest.mock import patch from django.contrib.auth.models import Group from django.template.response import TemplateResponse from django.test import TestCase @@ -58,7 +59,8 @@ class UserAdminViews(TestCase): request.user = self.local_user request.user.is_superuser = True - result = view(request, self.local_user.id) + with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"): + result = view(request, self.local_user.id) self.assertIsInstance(result, TemplateResponse) result.render()
{{ user.username }}{{ user.username }} {{ user.created_date }} {{ user.last_active_date }} {% if user.is_active %}{% trans "Active" %}{% else %}{% trans "Inactive" %}{% endif %}