2021-03-08 23:49:44 +00:00
|
|
|
""" moderation via flagged posts and users """
|
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
2021-03-09 02:36:34 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2021-03-08 23:49:44 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views import View
|
2021-03-09 02:36:34 +00:00
|
|
|
from django.views.decorators.http import require_POST
|
2021-03-08 23:49:44 +00:00
|
|
|
|
2021-03-09 02:36:34 +00:00
|
|
|
from bookwyrm import forms, models
|
2021-03-08 23:49:44 +00:00
|
|
|
|
|
|
|
|
2021-03-09 01:16:59 +00:00
|
|
|
# pylint: disable=no-self-use
|
2021-03-08 23:49:44 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.moderate_user", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.moderate_post", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
|
|
|
class Reports(View):
|
|
|
|
""" list of reports """
|
|
|
|
|
2021-03-09 01:16:59 +00:00
|
|
|
def get(self, request):
|
2021-03-08 23:49:44 +00:00
|
|
|
""" view current reports """
|
2021-03-28 20:03:24 +00:00
|
|
|
filters = {}
|
|
|
|
|
2021-03-09 20:35:07 +00:00
|
|
|
resolved = request.GET.get("resolved") == "true"
|
2021-03-28 20:03:24 +00:00
|
|
|
server = request.GET.get("server")
|
|
|
|
if server:
|
2021-04-17 17:56:26 +00:00
|
|
|
filters["user__federated_server__server_name"] = server
|
|
|
|
username = request.GET.get("username")
|
|
|
|
if username:
|
|
|
|
filters["user__username__icontains"] = username
|
2021-03-28 20:03:24 +00:00
|
|
|
filters["resolved"] = resolved
|
2021-03-08 23:49:44 +00:00
|
|
|
data = {
|
2021-03-09 01:16:59 +00:00
|
|
|
"resolved": resolved,
|
2021-03-28 20:03:24 +00:00
|
|
|
"server": server,
|
|
|
|
"reports": models.Report.objects.filter(**filters),
|
2021-03-09 01:16:59 +00:00
|
|
|
}
|
2021-03-10 20:38:49 +00:00
|
|
|
return TemplateResponse(request, "moderation/reports.html", data)
|
2021-03-08 23:49:44 +00:00
|
|
|
|
|
|
|
|
2021-03-09 02:36:34 +00:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.moderate_user", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
|
|
|
@method_decorator(
|
|
|
|
permission_required("bookwyrm.moderate_post", raise_exception=True),
|
|
|
|
name="dispatch",
|
|
|
|
)
|
2021-03-08 23:49:44 +00:00
|
|
|
class Report(View):
|
|
|
|
""" view a specific report """
|
|
|
|
|
|
|
|
def get(self, request, report_id):
|
|
|
|
""" load a report """
|
2021-03-12 23:56:54 +00:00
|
|
|
data = {
|
|
|
|
"report": get_object_or_404(models.Report, id=report_id),
|
|
|
|
}
|
2021-03-10 20:38:49 +00:00
|
|
|
return TemplateResponse(request, "moderation/report.html", data)
|
2021-03-09 02:36:34 +00:00
|
|
|
|
2021-03-12 23:56:54 +00:00
|
|
|
def post(self, request, report_id):
|
|
|
|
""" comment on a report """
|
|
|
|
report = get_object_or_404(models.Report, id=report_id)
|
|
|
|
models.ReportComment.objects.create(
|
|
|
|
user=request.user,
|
|
|
|
report=report,
|
|
|
|
note=request.POST.get("note"),
|
|
|
|
)
|
|
|
|
return redirect("settings-report", report.id)
|
|
|
|
|
2021-03-12 18:22:03 +00:00
|
|
|
|
2021-03-12 19:13:53 +00:00
|
|
|
@login_required
|
|
|
|
@permission_required("bookwyrm_moderate_user")
|
|
|
|
def deactivate_user(_, report_id):
|
|
|
|
""" mark an account as inactive """
|
|
|
|
report = get_object_or_404(models.Report, id=report_id)
|
|
|
|
report.user.is_active = not report.user.is_active
|
|
|
|
report.user.save()
|
|
|
|
return redirect("settings-report", report.id)
|
|
|
|
|
|
|
|
|
2021-03-12 18:22:03 +00:00
|
|
|
@login_required
|
|
|
|
@permission_required("bookwyrm_moderate_post")
|
|
|
|
def resolve_report(_, report_id):
|
|
|
|
""" mark a report as (un)resolved """
|
|
|
|
report = get_object_or_404(models.Report, id=report_id)
|
|
|
|
report.resolved = not report.resolved
|
|
|
|
report.save()
|
|
|
|
if not report.resolved:
|
|
|
|
return redirect("settings-report", report.id)
|
|
|
|
return redirect("settings-reports")
|
|
|
|
|
2021-03-09 02:36:34 +00:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
@require_POST
|
|
|
|
def make_report(request):
|
|
|
|
""" a user reports something """
|
|
|
|
form = forms.ReportForm(request.POST)
|
|
|
|
if not form.is_valid():
|
2021-04-17 17:28:03 +00:00
|
|
|
raise ValueError(form.errors)
|
2021-03-09 02:36:34 +00:00
|
|
|
|
|
|
|
form.save()
|
|
|
|
return redirect(request.headers.get("Referer", "/"))
|