summaryrefslogtreecommitdiffstats
path: root/users/views/admin/reports.py
blob: 61870687ba9b109e03789df5c62d66e5c95a8a74 (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
69
70
71
72
73
74
75
76
77
78
79
80
from django import forms
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.generic import FormView, ListView

from users.decorators import admin_required
from users.models import Identity, Report


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

    template_name = "admin/reports.html"
    paginate_by = 30

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

    def get_queryset(self):
        reports = Report.objects.select_related(
            "subject_post", "subject_identity"
        ).order_by("created")
        if not self.all:
            reports = reports.filter(resolved__isnull=True)
        return reports


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

    template_name = "admin/report_view.html"
    extra_context = {
        "section": "reports",
    }

    class form_class(forms.Form):
        notes = forms.CharField(widget=forms.Textarea, required=False)

    def dispatch(self, request, id, *args, **kwargs):
        self.report = get_object_or_404(Report, id=id)
        return super().dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        if "limit" in request.POST:
            self.report.subject_identity.restriction = Identity.Restriction.limited
            self.report.subject_identity.save()
        if "block" in request.POST:
            self.report.subject_identity.restriction = Identity.Restriction.blocked
            self.report.subject_identity.save()
        if "valid" in request.POST:
            self.report.resolved = timezone.now()
            self.report.valid = True
            self.report.moderator = self.request.identity
            self.report.save()
        if "invalid" in request.POST:
            self.report.resolved = timezone.now()
            self.report.valid = False
            self.report.moderator = self.request.identity
            self.report.save()
        return super().post(request, *args, **kwargs)

    def get_initial(self):
        return {"notes": self.report.notes}

    def form_valid(self, form):
        self.report.notes = form.cleaned_data["notes"]
        self.report.save()
        return redirect(".")

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