moviewyrm/bookwyrm/views/federation.py

92 lines
3.2 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" manage federated servers """
from django.contrib.auth.decorators import login_required, permission_required
2021-03-28 20:08:24 +00:00
from django.core.paginator import Paginator
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-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-03-08 16:49:10 +00:00
""" what servers do we federate with """
def get(self, request):
2021-03-28 20:08:24 +00:00
""" list of servers """
try:
page = int(request.GET.get("page", 1))
except ValueError:
page = 1
servers = models.FederatedServer.objects.all()
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-03-28 20:36:05 +00:00
if sort in sort_fields + ["-{:s}".format(f) for f in sort_fields]:
servers = servers.order_by(sort)
2021-03-28 20:08:24 +00:00
paginated = Paginator(servers, PAGE_LENGTH)
2021-04-07 20:06:29 +00:00
data = {
"servers": paginated.page(page), "sort": sort, "form": forms.ServerForm()
}
2021-03-08 16:49:10 +00:00
return TemplateResponse(request, "settings/federation.html", data)
2021-03-28 19:34:55 +00:00
2021-04-07 20:06:29 +00:00
class EditFederatedServer(View):
""" manually add a server """
def get(self, request):
""" add server form """
data = {"form": forms.ServerForm()}
return TemplateResponse(request, "settings/edit_server.html", data)
def post(self, request):
""" add a server from the admin panel """
form = forms.ServerForm(request.POST)
if not form.is_valid():
data = {"form": form}
return TemplateResponse(request, "settings/edit_server.html", data)
server = form.save()
return redirect("settings-federated-server", server.id)
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):
""" views for handling a specific federated server """
def get(self, request, server):
""" load a server """
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
""" (un)block a server """
server = get_object_or_404(models.FederatedServer, id=server)
server.status = "blocked" if server.status == "federated" else "federated"
server.save()
return redirect("settings-federated-server", server.id)