moviewyrm/bookwyrm/views/admin/federation.py

178 lines
6.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" manage federated servers """
import json
from django.contrib.auth.decorators import login_required, permission_required
2021-03-28 20:08:24 +00:00
from django.core.paginator import Paginator
from django.db import transaction
2021-04-05 22:38:32 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
2021-04-10 18:06:28 +00:00
from django.views.decorators.http import require_POST
2021-04-07 18:52:13 +00:00
from bookwyrm import forms, models
2021-03-28 20:08:24 +00:00
from bookwyrm.settings import PAGE_LENGTH
2022-02-28 21:09:43 +00:00
from bookwyrm.models.user import get_or_create_remote_server
# pylint: disable= no-self-use
2021-03-08 16:49:10 +00:00
@method_decorator(login_required, name="dispatch")
@method_decorator(
2021-03-08 16:49:10 +00:00
permission_required("bookwyrm.control_federation", raise_exception=True),
name="dispatch",
)
class Federation(View):
2021-04-26 16:15:42 +00:00
"""what servers do we federate with"""
2021-03-08 16:49:10 +00:00
2021-09-11 21:41:52 +00:00
def get(self, request, status="federated"):
2021-04-26 16:15:42 +00:00
"""list of servers"""
2021-09-11 21:41:52 +00:00
servers = models.FederatedServer.objects.filter(status=status)
2021-03-28 20:36:05 +00:00
2021-03-28 20:41:25 +00:00
sort = request.GET.get("sort")
sort_fields = ["created_date", "application_type", "server_name"]
2021-09-27 17:21:46 +00:00
# pylint: disable=consider-using-f-string
if not sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
2021-09-12 17:02:15 +00:00
sort = "-created_date"
servers = servers.order_by(sort)
2021-03-28 20:36:05 +00:00
2021-03-28 20:08:24 +00:00
paginated = Paginator(servers, PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page"))
2021-04-07 20:06:29 +00:00
data = {
"federated_count": models.FederatedServer.objects.filter(
status="federated"
).count(),
"blocked_count": models.FederatedServer.objects.filter(
status="blocked"
).count(),
"servers": page,
"page_range": paginated.get_elided_page_range(
page.number, on_each_side=2, on_ends=1
),
"sort": sort,
"form": forms.ServerForm(),
2021-04-07 20:06:29 +00:00
}
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/federation/instance_list.html", data)
2021-03-28 19:34:55 +00:00
class AddFederatedServer(View):
2021-04-26 16:15:42 +00:00
"""manually add a server"""
2021-04-07 20:06:29 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""add server form"""
data = {"form": forms.ServerForm()}
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/federation/edit_instance.html", data)
2021-04-07 20:06:29 +00:00
def post(self, request):
2021-04-26 16:15:42 +00:00
"""add a server from the admin panel"""
form = forms.ServerForm(request.POST)
2021-04-07 20:06:29 +00:00
if not form.is_valid():
data = {"form": form}
2021-09-28 19:39:52 +00:00
return TemplateResponse(
request, "settings/federation/edit_instance.html", data
)
2021-04-07 20:06:29 +00:00
server = form.save()
return redirect("settings-federated-server", server.id)
2021-04-12 22:08:10 +00:00
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.control_federation", raise_exception=True),
name="dispatch",
)
class ImportServerBlocklist(View):
2021-04-26 16:15:42 +00:00
"""manually add a server"""
def get(self, request):
2021-04-26 16:15:42 +00:00
"""add server form"""
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/federation/instance_blocklist.html")
def post(self, request):
2021-04-26 16:15:42 +00:00
"""add a server from the admin panel"""
2021-04-12 22:09:50 +00:00
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}
2021-09-28 19:39:52 +00:00
return TemplateResponse(
request, "settings/federation/instance_blocklist.html", data
)
2021-03-28 19:34:55 +00:00
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.control_federation", raise_exception=True),
name="dispatch",
)
class FederatedServer(View):
2021-04-26 16:15:42 +00:00
"""views for handling a specific federated server"""
2021-03-28 19:34:55 +00:00
def get(self, request, server):
2021-04-26 16:15:42 +00:00
"""load a server"""
2021-03-28 19:34:55 +00:00
server = get_object_or_404(models.FederatedServer, id=server)
users = server.user_set
data = {
"server": server,
"users": users,
"reports": models.Report.objects.filter(user__in=users.all()),
"followed_by_us": users.filter(followers__local=True),
"followed_by_them": users.filter(following__local=True),
"blocked_by_us": models.UserBlocks.objects.filter(
user_subject__in=users.all()
),
}
2021-09-28 19:39:52 +00:00
return TemplateResponse(request, "settings/federation/instance.html", data)
2021-04-05 22:38:32 +00:00
def post(self, request, server): # pylint: disable=unused-argument
2021-04-26 16:15:42 +00:00
"""update note"""
2021-04-05 22:38:32 +00:00
server = get_object_or_404(models.FederatedServer, id=server)
2021-04-11 17:36:40 +00:00
server.notes = request.POST.get("notes")
server.save()
2021-04-05 22:38:32 +00:00
return redirect("settings-federated-server", server.id)
2021-04-10 18:06:28 +00:00
2021-04-11 17:36:40 +00:00
@login_required
@require_POST
@permission_required("bookwyrm.control_federation", raise_exception=True)
# pylint: disable=unused-argument
def block_server(request, server):
2021-04-26 16:15:42 +00:00
"""block a server"""
2021-04-11 17:36:40 +00:00
server = get_object_or_404(models.FederatedServer, id=server)
server.block()
return redirect("settings-federated-server", server.id)
2021-04-10 18:06:28 +00:00
@login_required
@require_POST
@permission_required("bookwyrm.control_federation", raise_exception=True)
# pylint: disable=unused-argument
def unblock_server(request, server):
2021-04-26 16:15:42 +00:00
"""unblock a server"""
2021-04-10 18:06:28 +00:00
server = get_object_or_404(models.FederatedServer, id=server)
server.unblock()
return redirect("settings-federated-server", server.id)
2022-02-28 21:09:43 +00:00
@login_required
@require_POST
@permission_required("bookwyrm.control_federation", raise_exception=True)
# pylint: disable=unused-argument
def refresh_server(request, server):
"""unblock a server"""
server = get_object_or_404(models.FederatedServer, id=server)
get_or_create_remote_server(server.server_name, refresh=True)
return redirect("settings-federated-server", server.id)