summaryrefslogtreecommitdiffstats
path: root/users/views/admin/federation.py
blob: 0611b9e9f082e82f75526893f6d3b0435e513006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from django import forms
from django.db import models
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
from django.views.generic import FormView, ListView

from users.decorators import admin_required
from users.models import Domain


@method_decorator(admin_required, name="dispatch")
class FederationRoot(ListView):

    template_name = "admin/federation.html"
    paginate_by = 50

    def get(self, request, *args, **kwargs):
        self.query = request.GET.get("query")
        self.extra_context = {
            "section": "federation",
            "query": self.query or "",
        }
        return super().get(request, *args, **kwargs)

    def get_queryset(self):
        domains = (
            Domain.objects.filter(local=False)
            .annotate(num_users=models.Count("identities"))
            .order_by("domain")
        )
        if self.query:
            domains = domains.filter(domain__icontains=self.query)
        return domains


@method_decorator(admin_required, name="dispatch")
class FederationEdit(FormView):

    template_name = "admin/federation_edit.html"
    extra_context = {"section": "federation"}

    class form_class(forms.Form):
        blocked = forms.BooleanField(
            help_text="If this domain is blocked from interacting with this server.\nAll incoming posts from this domain will be irrecoverably dropped.",
            widget=forms.Select(choices=[(True, "Blocked"), (False, "Not Blocked")]),
            required=False,
        )

    def dispatch(self, request, domain):
        self.domain = get_object_or_404(
            Domain.objects.filter(local=False), domain=domain
        )
        return super().dispatch(request)

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context["domain"] = self.domain
        return context

    def form_valid(self, form):
        self.domain.blocked = form.cleaned_data["blocked"]
        self.domain.save()
        return redirect(Domain.urls.root_federation)

    def get_initial(self):
        return {
            "blocked": self.domain.blocked,
        }