Adds notifications

This commit is contained in:
Mouse Reeve 2022-02-24 13:20:18 -08:00
parent ad41f19dc5
commit 93f82fbf18
4 changed files with 46 additions and 15 deletions

View file

@ -13,6 +13,7 @@ def set_report_statuses(apps, schema_editor):
report.status = report.statuses.first()
report.save()
def set_reverse(apps, schema_editor):
"""copy over status fields"""
db_alias = schema_editor.connection.alias
@ -21,17 +22,24 @@ def set_reverse(apps, schema_editor):
for report in reports:
report.statuses.set(report.status)
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0138_automod'),
("bookwyrm", "0138_automod"),
]
operations = [
migrations.AddField(
model_name='report',
name='status',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='reports', to='bookwyrm.status'),
model_name="report",
name="status",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="reports",
to="bookwyrm.status",
),
),
migrations.RunPython(set_report_statuses, reverse_code=set_reverse),
]

View file

@ -6,12 +6,12 @@ from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0139_report_status'),
("bookwyrm", "0139_report_status"),
]
operations = [
migrations.RemoveField(
model_name='report',
name='statuses',
model_name="report",
name="statuses",
),
]

View file

@ -7,13 +7,18 @@ import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0140_remove_report_statuses'),
("bookwyrm", "0140_remove_report_statuses"),
]
operations = [
migrations.AlterField(
model_name='report',
name='status',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='bookwyrm.status'),
model_name="report",
name="status",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="bookwyrm.status",
),
),
]

View file

@ -57,8 +57,26 @@ def automod_task():
if not AutoMod.objects.exists():
return
reporter = AutoMod.objects.first().created_by
automod_users(reporter)
automod_statuses(reporter)
reports = automod_users(reporter) + automod_statuses(reporter)
if reports:
admins = User.objects.filter(
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
| models.Q(is_superuser=True)
).all()
notification_model = apps.get_model(
"bookwyrm", "Notification", require_ready=True
)
for admin in admins:
notification_model.objects.bulk_create(
[
notification_model(
user=admin,
related_report=r,
notification_type="REPORT",
)
for r in reports
]
)
def automod_users(reporter):
@ -76,7 +94,7 @@ def automod_users(reporter):
report_model = apps.get_model("bookwyrm", "Report", require_ready=True)
report_model.objects.bulk_create(
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,
@ -103,7 +121,7 @@ def automod_statuses(reporter):
)
report_model = apps.get_model("bookwyrm", "Report", require_ready=True)
report_model.objects.bulk_create(
return report_model.objects.bulk_create(
[
report_model(
reporter=reporter,