From d076162aa629598caea8324cb5a9118b7e679e66 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 10 Apr 2021 10:09:36 -0700 Subject: [PATCH] Remove edit server mode It just doesn't really make sense --- bookwyrm/templates/settings/edit_server.html | 4 +- .../templates/settings/federated_server.html | 8 ---- bookwyrm/templates/settings/federation.html | 2 +- bookwyrm/tests/views/test_federation.py | 42 +++++++++++++++---- bookwyrm/urls.py | 11 ++--- bookwyrm/views/__init__.py | 2 +- bookwyrm/views/federation.py | 22 ++++------ 7 files changed, 49 insertions(+), 42 deletions(-) diff --git a/bookwyrm/templates/settings/edit_server.html b/bookwyrm/templates/settings/edit_server.html index dc0de7e8..6ae22789 100644 --- a/bookwyrm/templates/settings/edit_server.html +++ b/bookwyrm/templates/settings/edit_server.html @@ -3,13 +3,13 @@ {% block title %}{% trans "Add server" %}{% endblock %} {% block header %} -{% if server %}{{ server.server_name }}{% else %}{% trans "Add server" %}{% endif %} +{% trans "Add server" %} {% trans "Back to server list" %} {% endblock %} {% block panel %} -
+ {% csrf_token %}
diff --git a/bookwyrm/templates/settings/federated_server.html b/bookwyrm/templates/settings/federated_server.html index 520ab5dc..37baa4dc 100644 --- a/bookwyrm/templates/settings/federated_server.html +++ b/bookwyrm/templates/settings/federated_server.html @@ -11,14 +11,6 @@ {% trans "Back to server list" %} {% endblock %} -{% block edit-button %} - - - {% trans "Edit server" %} - - -{% endblock %} - {% block panel %}
diff --git a/bookwyrm/templates/settings/federation.html b/bookwyrm/templates/settings/federation.html index da5ed084..99afb541 100644 --- a/bookwyrm/templates/settings/federation.html +++ b/bookwyrm/templates/settings/federation.html @@ -5,7 +5,7 @@ {% block header %}{% trans "Federated Servers" %}{% endblock %} {% block edit-button %} - + {% trans "Add server" %} diff --git a/bookwyrm/tests/views/test_federation.py b/bookwyrm/tests/views/test_federation.py index 591bb62a..01504ddb 100644 --- a/bookwyrm/tests/views/test_federation.py +++ b/bookwyrm/tests/views/test_federation.py @@ -1,9 +1,10 @@ """ 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 -from bookwyrm import models, views +from bookwyrm import forms, models, views class FederationViews(TestCase): @@ -19,6 +20,16 @@ class FederationViews(TestCase): local=True, localname="mouse", ) + with patch("bookwyrm.models.user.set_remote_server.delay"): + self.remote_user = models.User.objects.create_user( + "rat", + "rat@rat.com", + "ratword", + local=False, + remote_id="https://example.com/users/rat", + inbox="https://example.com/users/rat/inbox", + outbox="https://example.com/users/rat/outbox", + ) models.SiteSettings.objects.create() def test_federation_page(self): @@ -48,6 +59,9 @@ class FederationViews(TestCase): def test_server_page_post(self): """ block and unblock a server """ server = models.FederatedServer.objects.create(server_name="hi.there.com") + self.remote_user.federated_server = server + self.remote_user.save() + self.assertEqual(server.status, "federated") view = views.FederatedServer.as_view() @@ -63,10 +77,10 @@ class FederationViews(TestCase): server.refresh_from_db() self.assertEqual(server.status, "federated") - def test_edit_view_get(self): + def test_add_view_get(self): """ there are so many views, this just makes sure it LOADS """ # create mode - view = views.EditFederatedServer.as_view() + view = views.AddFederatedServer.as_view() request = self.factory.get("") request.user = self.local_user request.user.is_superuser = True @@ -76,9 +90,19 @@ class FederationViews(TestCase): result.render() self.assertEqual(result.status_code, 200) - # edit mode - server = models.FederatedServer.objects.create(server_name="hi.there.com") - result = view(request, server=server.id) - self.assertIsInstance(result, TemplateResponse) - result.render() - self.assertEqual(result.status_code, 200) + def test_add_view_post_create(self): + """ create or edit a server """ + form = forms.ServerForm() + form.data["server_name"] = "remote.server" + form.data["software"] = "coolsoft" + + view = views.AddFederatedServer.as_view() + request = self.factory.post("", form.data) + request.user = self.local_user + request.user.is_superuser = True + + view(request, server=None) + server = models.FederatedServer.objects.get() + self.assertEqual(server.server_name, "remote.server") + self.assertEqual(server.software, "coolsoft") + self.assertEqual(server.status, "federated") diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 4cf6fd52..398caade 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -69,14 +69,9 @@ urlpatterns = [ name="settings-federated-server", ), re_path( - r"^settings/federation/edit/?$", - views.EditFederatedServer.as_view(), - name="settings-edit-federated-server", - ), - re_path( - r"^settings/federation/edit/(?P\d+)?$", - views.EditFederatedServer.as_view(), - name="settings-edit-federated-server", + r"^settings/federation/add/?$", + views.AddFederatedServer.as_view(), + name="settings-add-federated-server", ), re_path( r"^settings/invites/?$", views.ManageInvites.as_view(), name="settings-invites" diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 404c2f31..de001b31 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -5,7 +5,7 @@ from .block import Block, unblock from .books import Book, EditBook, ConfirmEditBook, Editions from .books import upload_cover, add_description, switch_edition, resolve_book from .directory import Directory -from .federation import Federation, FederatedServer, EditFederatedServer +from .federation import Federation, FederatedServer, AddFederatedServer from .feed import DirectMessage, Feed, Replies, Status from .follow import follow, unfollow from .follow import accept_follow_request, delete_follow_request diff --git a/bookwyrm/views/federation.py b/bookwyrm/views/federation.py index 83bed32d..e4ade554 100644 --- a/bookwyrm/views/federation.py +++ b/bookwyrm/views/federation.py @@ -30,8 +30,9 @@ class Federation(View): sort = request.GET.get("sort") sort_fields = ["created_date", "application_type", "server_name"] - if sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]: - servers = servers.order_by(sort) + if not sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]: + sort = "created_date" + servers = servers.order_by(sort) paginated = Paginator(servers, PAGE_LENGTH) @@ -43,24 +44,19 @@ class Federation(View): return TemplateResponse(request, "settings/federation.html", data) -class EditFederatedServer(View): +class AddFederatedServer(View): """ manually add a server """ - def get(self, request, server=None): + def get(self, request): """ add server form """ - if server: - server = get_object_or_404(models.FederatedServer, id=server) - data = {"form": forms.ServerForm(instance=server), "server": server} + data = {"form": forms.ServerForm()} return TemplateResponse(request, "settings/edit_server.html", data) - def post(self, request, server=None): + def post(self, request): """ add a server from the admin panel """ - if server: - server = get_object_or_404(models.FederatedServer, id=server) - - form = forms.ServerForm(request.POST, instance=server) + form = forms.ServerForm(request.POST) if not form.is_valid(): - data = {"form": form, "server": server} + data = {"form": form} return TemplateResponse(request, "settings/edit_server.html", data) server = form.save() return redirect("settings-federated-server", server.id)