moviewyrm/bookwyrm/views/federation.py

151 lines
5.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
# 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
def get(self, request):
2021-04-26 16:15:42 +00:00
"""list of servers"""
servers = models.FederatedServer.objects
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"]
if not sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
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)
2021-04-07 20:06:29 +00:00
data = {
2021-04-19 22:01:20 +00:00
"servers": paginated.get_page(request.GET.get("page")),
"sort": sort,
"form": forms.ServerForm(),
2021-04-07 20:06:29 +00:00
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "settings/federation.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-04-07 20:06:29 +00:00
return TemplateResponse(request, "settings/edit_server.html", data)
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-04-07 20:06:29 +00:00
return TemplateResponse(request, "settings/edit_server.html", data)
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"""
return TemplateResponse(request, "settings/server_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}
return TemplateResponse(request, "settings/server_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()
),
}
return TemplateResponse(request, "settings/federated_server.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)