2021-03-08 16:49:10 +00:00
|
|
|
""" manage federated servers """
|
2021-04-12 21:23:51 +00:00
|
|
|
import json
|
2021-01-30 00:02:59 +00:00
|
|
|
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-12 21:23:51 +00:00
|
|
|
from django.db import transaction
|
2021-04-05 22:38:32 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-01-30 00:02:59 +00:00
|
|
|
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-01-30 00:02:59 +00:00
|
|
|
|
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
|
2021-01-30 00:02:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable= no-self-use
|
2021-03-08 16:49:10 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2021-01-30 00:02:59 +00:00
|
|
|
@method_decorator(
|
2021-03-08 16:49:10 +00:00
|
|
|
permission_required("bookwyrm.control_federation", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
2021-01-30 00:02:59 +00:00
|
|
|
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-04-10 17:09:36 +00:00
|
|
|
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"
|
2021-04-10 17:09:36 +00:00
|
|
|
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-09-11 21:27:42 +00:00
|
|
|
page = paginated.get_page(request.GET.get("page"))
|
2021-04-07 20:06:29 +00:00
|
|
|
|
|
|
|
data = {
|
2021-09-11 21:27:42 +00:00
|
|
|
"servers": page,
|
|
|
|
"page_range": paginated.get_elided_page_range(
|
|
|
|
page.number, on_each_side=2, on_ends=1
|
|
|
|
),
|
2021-04-07 23:50:12 +00:00
|
|
|
"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
|
|
|
|
|
|
|
|
2021-04-10 17:09:36 +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
|
|
|
|
2021-04-10 17:09:36 +00:00
|
|
|
def get(self, request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add server form"""
|
2021-04-10 17:09:36 +00:00
|
|
|
data = {"form": forms.ServerForm()}
|
2021-04-07 20:06:29 +00:00
|
|
|
return TemplateResponse(request, "settings/edit_server.html", data)
|
|
|
|
|
2021-04-10 17:09:36 +00:00
|
|
|
def post(self, request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add a server from the admin panel"""
|
2021-04-10 17:09:36 +00:00
|
|
|
form = forms.ServerForm(request.POST)
|
2021-04-07 20:06:29 +00:00
|
|
|
if not form.is_valid():
|
2021-04-10 17:09:36 +00:00
|
|
|
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",
|
|
|
|
)
|
2021-04-12 21:23:51 +00:00
|
|
|
class ImportServerBlocklist(View):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""manually add a server"""
|
2021-04-12 21:23:51 +00:00
|
|
|
|
|
|
|
def get(self, request):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""add server form"""
|
2021-04-12 21:23:51 +00:00
|
|
|
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"])
|
2021-04-12 21:23:51 +00:00
|
|
|
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)
|