From 901e7f69d436e66de2e664c645c9521e7bbbe9d1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 12 Apr 2021 14:23:51 -0700 Subject: [PATCH] Allow admins to upload domain blocklists Fixes #804 --- bookwyrm/templates/import.html | 2 +- bookwyrm/templates/settings/edit_server.html | 17 ++++- .../templates/settings/federated_server.html | 3 +- bookwyrm/templates/settings/federation.html | 2 +- .../templates/settings/server_blocklist.html | 67 +++++++++++++++++++ bookwyrm/urls.py | 5 ++ bookwyrm/views/__init__.py | 3 +- bookwyrm/views/federation.py | 35 ++++++++++ 8 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 bookwyrm/templates/settings/server_blocklist.html diff --git a/bookwyrm/templates/import.html b/bookwyrm/templates/import.html index a5405131..239b6914 100644 --- a/bookwyrm/templates/import.html +++ b/bookwyrm/templates/import.html @@ -26,7 +26,7 @@
- + {{ import_form.csv_file }}
diff --git a/bookwyrm/templates/settings/edit_server.html b/bookwyrm/templates/settings/edit_server.html index 6ae22789..c5702c84 100644 --- a/bookwyrm/templates/settings/edit_server.html +++ b/bookwyrm/templates/settings/edit_server.html @@ -9,6 +9,19 @@ {% block panel %} +
+ +
+
{% csrf_token %}
@@ -24,8 +37,8 @@
diff --git a/bookwyrm/templates/settings/federated_server.html b/bookwyrm/templates/settings/federated_server.html index 6996557d..14b78af0 100644 --- a/bookwyrm/templates/settings/federated_server.html +++ b/bookwyrm/templates/settings/federated_server.html @@ -1,6 +1,7 @@ {% extends 'settings/admin_layout.html' %} {% block title %}{{ server.server_name }}{% endblock %} {% load i18n %} +{% load bookwyrm_tags %} {% block header %} {{ server.server_name }} @@ -81,7 +82,7 @@ {% if server.notes %} -

{{ server.notes }}

+

{{ server.notes|to_markdown|safe }}

{% endif %} {% csrf_token %} diff --git a/bookwyrm/templates/settings/federation.html b/bookwyrm/templates/settings/federation.html index 99afb541..9eb0f8b1 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/templates/settings/server_blocklist.html b/bookwyrm/templates/settings/server_blocklist.html new file mode 100644 index 00000000..0de49acd --- /dev/null +++ b/bookwyrm/templates/settings/server_blocklist.html @@ -0,0 +1,67 @@ +{% extends 'settings/admin_layout.html' %} +{% load i18n %} +{% block title %}{% trans "Add server" %}{% endblock %} + +{% block header %} +{% trans "Import Blocklist" %} +{% trans "Back to server list" %} +{% endblock %} + +{% block panel %} + +
+ +
+ +{% if succeeded and not failed %} +

{% trans "Success!" %}

+{% elif succeeded or failed %} +
+ {% if succeeded %} +

{% trans "Successfully blocked:" %} {{ succeeded }}

+ {% endif %} +

{% trans "Failed:" %}

+
    + {% for item in failed %} +
  • +
    +{{ item }}
    +
    +
  • + {% endfor %} +
+
+{% endif %} + + + {% csrf_token %} +
+ + + +
+ + +
+ +{% endblock %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index c5c52800..cf3f877b 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -83,6 +83,11 @@ urlpatterns = [ views.AddFederatedServer.as_view(), name="settings-add-federated-server", ), + re_path( + r"^settings/federation/import/?$", + views.ImportServerBlocklist.as_view(), + name="settings-import-blocklist", + ), 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 d7bc4e13..9f8463b4 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -5,7 +5,8 @@ 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, AddFederatedServer +from .federation import Federation, FederatedServer +from .federation import AddFederatedServer, ImportServerBlocklist from .federation import block_server, unblock_server from .feed import DirectMessage, Feed, Replies, Status from .follow import follow, unfollow diff --git a/bookwyrm/views/federation.py b/bookwyrm/views/federation.py index f34f7d19..b9a7fa85 100644 --- a/bookwyrm/views/federation.py +++ b/bookwyrm/views/federation.py @@ -1,6 +1,8 @@ """ manage federated servers """ +import json from django.contrib.auth.decorators import login_required, permission_required from django.core.paginator import Paginator +from django.db import transaction from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator @@ -63,6 +65,39 @@ class AddFederatedServer(View): return redirect("settings-federated-server", server.id) +class ImportServerBlocklist(View): + """ manually add a server """ + + def get(self, request): + """ add server form """ + return TemplateResponse(request, "settings/server_blocklist.html") + + def post(self, request): + """ add a server from the admin panel """ + json_data = json.load( + request.FILES["json_file"] + ) + failed = [] + success_count = 0 + for item in json_data: + server_name = item.get("instance") + if not server_name: + failed.append(item) + continue + info_link = item.get("url") + + with transaction.atomic(): + server, _ = models.FederatedServer.objects.get_or_create( + server_name=server_name, + ) + server.notes = info_link + server.save() + server.block() + success_count += 1 + data = {"failed": failed, "succeeded": success_count} + return TemplateResponse(request, "settings/server_blocklist.html", data) + + @method_decorator(login_required, name="dispatch") @method_decorator( permission_required("bookwyrm.control_federation", raise_exception=True),