Changes field names in report model so the reporter is "user"

This is such an annoying change but it is objectively better. Just gotta
be real sure they didn't get mixed up anywhere along the way.
This commit is contained in:
Mouse Reeve 2024-01-02 14:38:35 -08:00
parent 5b229fa362
commit 5c0ade5346
16 changed files with 145 additions and 52 deletions

View file

@ -50,9 +50,11 @@ def password_reset_email(reset_code):
def moderation_report_email(report):
"""a report was created"""
data = email_data()
data["reporter"] = report.reporter.localname or report.reporter.username
if report.user:
data["reportee"] = report.user.localname or report.user.username
data["reporter"] = report.user.localname or report.user.username
if report.reported_user:
data["reportee"] = (
report.reported_user.localname or report.reported_user.username
)
data["report_link"] = report.remote_id
data["link_domain"] = report.links.exists()

View file

@ -44,7 +44,7 @@ class GoalForm(CustomForm):
class ReportForm(CustomForm):
class Meta:
model = models.Report
fields = ["user", "reporter", "status", "links", "note"]
fields = ["reported_user", "user", "status", "links", "note"]
class ReadThroughForm(CustomForm):

View file

@ -0,0 +1,18 @@
# Generated by Django 3.2.23 on 2024-01-02 22:16
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0192_auto_20240102_2156"),
]
operations = [
migrations.RenameField(
model_name="report",
old_name="user",
new_name="reported_user",
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 3.2.23 on 2024-01-02 22:17
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0193_rename_user_report_reported_user"),
]
operations = [
migrations.RenameField(
model_name="report",
old_name="reporter",
new_name="user",
),
]

View file

@ -109,9 +109,9 @@ def automod_users(reporter):
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,
user=reporter,
note=_("Automatically generated report"),
user=u,
reported_user=u,
)
for u in users
]
@ -143,9 +143,9 @@ def automod_statuses(reporter):
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,
user=reporter,
note=_("Automatically generated report"),
user=s.user,
reported_user=s.user,
status=s,
)
for s in statuses

View file

@ -1,5 +1,4 @@
""" flagged for moderation """
from django.core.exceptions import PermissionDenied
from django.db import models
from django.utils.translation import gettext_lazy as _
@ -30,15 +29,19 @@ class Report(ActivityMixin, BookWyrmModel):
activity_serializer = activitypub.Flag
reporter = fields.ForeignKey(
user = fields.ForeignKey(
"User",
related_name="reporter",
on_delete=models.PROTECT,
activitypub_field="actor",
)
note = fields.TextField(null=True, blank=True, activitypub_field="content")
user = fields.ForeignKey(
"User", on_delete=models.PROTECT, null=True, blank=True, activitypub_field="to"
reported_user = fields.ForeignKey(
"User",
related_name="reported_user",
on_delete=models.PROTECT,
null=True,
blank=True,
activitypub_field="to",
)
status = fields.ForeignKey(
"Status",
@ -50,11 +53,11 @@ class Report(ActivityMixin, BookWyrmModel):
links = fields.ManyToManyField("Link", blank=True)
resolved = models.BooleanField(default=False)
def raise_not_editable(self, viewer):
"""instead of user being the owner field, it's reporter"""
if self.reporter == viewer or viewer.has_perm("bookwyrm.moderate_user"):
return
raise PermissionDenied()
def get_recipients(self, software=None):
"""Send this to the public inbox of the offending instance"""
if self.user.local:
return None
return [self.user.shared_inbox or self.user.inbox]
def get_remote_id(self):
return f"https://{DOMAIN}/settings/reports/{self.id}"

View file

@ -6,5 +6,5 @@
{% endblock %}
{% block content %}
{% include "snippets/report_modal.html" with user=user active=True static=True id="report-modal" %}
{% include "snippets/report_modal.html" with reported_user=reported_user active=True static=True id="report-modal" %}
{% endblock %}

View file

@ -27,7 +27,7 @@
</summary>
<div class="box">
{% trans "Update on your report:" as dm_template %}
{% include 'snippets/create_status/status.html' with type="direct" uuid=1 mention=report.reporter prepared_content=dm_template no_script=True %}
{% include 'snippets/create_status/status.html' with type="direct" uuid=1 mention=report.user prepared_content=dm_template no_script=True %}
</div>
</details>
</div>
@ -56,10 +56,10 @@
</div>
{% endif %}
{% if report.user %}
{% include 'settings/users/user_info.html' with user=report.user %}
{% if report.reported_user %}
{% include 'settings/users/user_info.html' with reported_user=report.reported_user %}
{% include 'settings/users/user_moderation_actions.html' with user=report.user %}
{% include 'settings/users/user_moderation_actions.html' with reported_user=report.reported_user %}
{% endif %}
<div class="block content">
@ -70,8 +70,8 @@
<li class="mb-2">
<div class="is-flex">
<p class="mb-0 is-flex-grow-1">
{% blocktrans trimmed with user=report.reporter|username user_link=report.reporter.local_path %}
<a href="{{ user_link }}">{{ user}}</a> opened this report
{% blocktrans trimmed with reported_user=report.user|username user_link=report.user.local_path %}
<a href="{{ user_link }}">{{ reported_user}}</a> opened this report
{% endblocktrans %}
</p>
<span class="tag">{{ report.created_date }}</span>
@ -83,12 +83,12 @@
<div class="is-flex">
<p class="mb-0 is-flex-grow-1">
{% if comment.action_type == "comment" %}
{% blocktrans trimmed with user=comment.user|username user_link=comment.user.local_path %}
<a href="{{ user_link }}">{{ user}}</a> commented on this report:
{% blocktrans trimmed with reported_user=comment.reported_user|username user_link=comment.reported_user.local_path %}
<a href="{{ user_link }}">{{ reported_user}}</a> commented on this report:
{% endblocktrans %}
{% else %}
{% blocktrans trimmed with user=comment.user|username user_link=comment.user.local_path %}
<a href="{{ user_link }}">{{ user}}</a> took an action on this report:
{% blocktrans trimmed with reported_user=comment.reported_user|username user_link=comment.reported_user.local_path %}
<a href="{{ user_link }}">{{ reported_user}}</a> took an action on this report:
{% endblocktrans %}
<span class="has-text-weight-bold">
{{ comment.get_action_type_display }}

View file

@ -3,14 +3,14 @@
{% if report.status %}
{% blocktrans trimmed with report_id=report.id username=report.user|username %}
{% blocktrans trimmed with report_id=report.id username=report.reported_user|username %}
Report #{{ report_id }}: Status posted by @{{ username }}
{% endblocktrans %}
{% elif report.links.exists %}
{% if report.user %}
{% blocktrans trimmed with report_id=report.id username=report.user|username %}
{% if report.reported_user %}
{% blocktrans trimmed with report_id=report.id username=report.reported_user|username %}
Report #{{ report_id }}: Link added by @{{ username }}
{% endblocktrans %}
{% else %}
@ -21,7 +21,7 @@ Report #{{ report_id }}: Status posted by @{{ username }}
{% else %}
{% blocktrans trimmed with report_id=report.id username=report.user|username %}
{% blocktrans trimmed with report_id=report.id username=report.reported_user|username %}
Report #{{ report_id }}: User @{{ username }}
{% endblocktrans %}

View file

@ -21,7 +21,7 @@
{% block card-footer %}
<div class="card-footer-item">
<p>{% blocktrans with username=report.reporter|username path=report.reporter.local_path %}Reported by <a href="{{ path }}">@{{ username }}</a>{% endblocktrans %}</p>
<p>{% blocktrans with username=report.user|username path=report.user.local_path %}Reported by <a href="{{ path }}">@{{ username }}</a>{% endblocktrans %}</p>
</div>
<div class="card-footer-item">
{{ report.created_date | naturaltime }}

View file

@ -5,11 +5,11 @@
{% block modal-title %}
{% if status %}
{% blocktrans with username=user|username %}Report @{{ username }}'s status{% endblocktrans %}
{% blocktrans with username=reported_user|username %}Report @{{ username }}'s status{% endblocktrans %}
{% elif link %}
{% blocktrans with domain=link.domain.domain %}Report {{ domain }} link{% endblocktrans %}
{% else %}
{% blocktrans with username=user|username %}Report @{{ username }}{% endblocktrans %}
{% blocktrans with username=reported_user|username %}Report @{{ username }}{% endblocktrans %}
{% endif %}
{% endblock %}
@ -20,8 +20,8 @@
{% block modal-body %}
{% csrf_token %}
<input type="hidden" name="reporter" value="{{ request.user.id }}">
<input type="hidden" name="user" value="{{ user.id }}">
<input type="hidden" name="user" value="{{ request.reported_user.id }}">
<input type="hidden" name="reported_user" value="{{ reported_user.id }}">
{% if status_id %}
<input type="hidden" name="status" value="{{ status_id }}">
{% endif %}

View file

@ -0,0 +1,46 @@
""" testing models """
from unittest.mock import patch
from django.test import TestCase
from bookwyrm import models
class Relationship(TestCase):
"""following, blocking, stuff like that"""
@classmethod
def setUpTestData(self): # pylint: disable=bad-classmethod-argument, invalid-name
"""we need some users for this"""
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.com",
"ratword",
local=False,
remote_id="https://example.com/users/rat",
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
)
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
)
self.another_local_user = models.User.objects.create_user(
"bird", "bird@bird.com", "birdword", local=True, localname="bird"
)
self.local_user.remote_id = "http://local.com/user/mouse"
self.local_user.save(broadcast=False, update_fields=["remote_id"])
def test_report_local_user(self):
"""a report/flag within an instance"""
report = models.Report.objects.create(
user=self.local_user,
note="oh no bad",
reported_user=self.another_local_user,
)
activity = report.to_activity()
self.assertEqual(activity.type, "Flag")
self.assertEqual(activity.actor, self.local_user)
self.assertEqual(activity.to, self.another_local_user.remote_id)

View file

@ -61,7 +61,7 @@ class ReportViews(TestCase):
view = views.ReportsAdmin.as_view()
request = self.factory.get("")
request.user = self.local_user
models.Report.objects.create(reporter=self.local_user, user=self.rat)
models.Report.objects.create(user=self.local_user, reported_user=self.rat)
result = view(request)
self.assertIsInstance(result, TemplateResponse)
@ -73,7 +73,9 @@ class ReportViews(TestCase):
view = views.ReportAdmin.as_view()
request = self.factory.get("")
request.user = self.local_user
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
report = models.Report.objects.create(
user=self.local_user, reported_user=self.rat
)
result = view(request, report.id)
@ -86,7 +88,9 @@ class ReportViews(TestCase):
view = views.ReportAdmin.as_view()
request = self.factory.post("", {"note": "hi"})
request.user = self.local_user
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
report = models.Report.objects.create(
user=self.local_user, reported_user=self.rat
)
view(request, report.id)
@ -98,7 +102,9 @@ class ReportViews(TestCase):
def test_resolve_report(self):
"""toggle report resolution status"""
report = models.Report.objects.create(reporter=self.local_user, user=self.rat)
report = models.Report.objects.create(
user=self.local_user, reported_user=self.rat
)
self.assertFalse(report.resolved)
self.assertFalse(models.ReportAction.objects.exists())
request = self.factory.post("")
@ -112,7 +118,7 @@ class ReportViews(TestCase):
# check that the action was noted
self.assertTrue(
models.ReportAction.objects.filter(
report=report, action_type="resolve", user=self.local_user
report=report, action_type="resolve", reported_user=self.local_user
).exists()
)
@ -124,7 +130,7 @@ class ReportViews(TestCase):
# check that the action was noted
self.assertTrue(
models.ReportAction.objects.filter(
report=report, action_type="reopen", user=self.local_user
report=report, action_type="reopen", reported_user=self.local_user
).exists()
)

View file

@ -96,7 +96,7 @@ class UserAdminViews(TestCase):
)
report = models.Report.objects.create(
user=self.local_user, reporter=self.local_user
reported_user=self.local_user, user=self.local_user
)
view = views.UserAdmin.as_view()

View file

@ -115,8 +115,8 @@ class NotificationViews(TestCase):
def test_notifications_page_report(self):
"""import completed notification"""
report = models.Report.objects.create(
user=self.another_user,
reporter=self.local_user,
reported_user=self.another_user,
user=self.local_user,
)
notification = models.Notification.objects.create(
user=self.local_user,

View file

@ -77,15 +77,15 @@ class ReportViews(TestCase):
def test_make_report(self):
"""a user reports another user"""
form = forms.ReportForm()
form.data["reporter"] = self.local_user.id
form.data["user"] = self.rat.id
form.data["user"] = self.local_user.id
form.data["reported_user"] = self.rat.id
request = self.factory.post("", form.data)
request.user = self.local_user
views.Report.as_view()(request)
report = models.Report.objects.get()
self.assertEqual(report.reporter, self.local_user)
self.assertEqual(report.user, self.local_user)
self.assertEqual(report.user, self.rat)
def test_report_link(self):
@ -99,8 +99,8 @@ class ReportViews(TestCase):
domain.save()
form = forms.ReportForm()
form.data["reporter"] = self.local_user.id
form.data["user"] = self.rat.id
form.data["user"] = self.local_user.id
form.data["reported_user"] = self.rat.id
form.data["links"] = link.id
request = self.factory.post("", form.data)
request.user = self.local_user