moviewyrm/bookwyrm/views/reports.py

98 lines
3 KiB
Python
Raw Normal View History

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-09 20:35:07 +00:00
resolved = request.GET.get("resolved") == "true"
2021-03-08 23:49:44 +00:00
data = {
2021-03-09 01:16:59 +00:00
"resolved": resolved,
"reports": models.Report.objects.filter(resolved=resolved),
}
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),
}
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():
print(form.errors)
return redirect(request.headers.get("Referer", "/"))
form.save()
return redirect(request.headers.get("Referer", "/"))