diff --git a/bookwyrm/migrations/0138_automod.py b/bookwyrm/migrations/0138_automod.py index 8b0369efa..691ea94bb 100644 --- a/bookwyrm/migrations/0138_automod.py +++ b/bookwyrm/migrations/0138_automod.py @@ -8,18 +8,32 @@ import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ - ('bookwyrm', '0137_alter_sitesettings_allow_registration'), + ("bookwyrm", "0137_alter_sitesettings_allow_registration"), ] operations = [ migrations.CreateModel( - name='AutoMod', + name="AutoMod", fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('string_match', models.CharField(max_length=200, unique=True)), - ('flag_users', models.BooleanField(default=True)), - ('flag_statuses', models.BooleanField(default=True)), - ('created_by', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("string_match", models.CharField(max_length=200, unique=True)), + ("flag_users", models.BooleanField(default=True)), + ("flag_statuses", models.BooleanField(default=True)), + ( + "created_by", + models.ForeignKey( + on_delete=django.db.models.deletion.PROTECT, + to=settings.AUTH_USER_MODEL, + ), + ), ], ), ] diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py index 43b524f63..e0d1aa4ef 100644 --- a/bookwyrm/models/antispam.py +++ b/bookwyrm/models/antispam.py @@ -71,22 +71,25 @@ def automod_users(reporter): users = User.objects.filter( reduce(operator.or_, (Q(**f) for f in filters)), is_active=True, - report__isnull=True, # don't flag users that already have reports + report__isnull=True, # don't flag users that already have reports ).values_list("id", flat=True) report_model = apps.get_model("bookwyrm", "Report", require_ready=True) - report_model.objects.bulk_create([ - report_model( - reporter=reporter, - note=_("Automatically generated report"), - user=u, - ) for u in users - ]) + report_model.objects.bulk_create( + [ + report_model( + reporter=reporter, + note=_("Automatically generated report"), + user=u, + ) + for u in users + ] + ) def automod_statuses(reporter): - """ check statues for moderation flags """ + """check statues for moderation flags""" status_rules = AutoMod.objects.filter(flag_statuses=True).values_list( "string_match", flat=True ) @@ -96,15 +99,18 @@ def automod_statuses(reporter): statuses = status_model.objects.filter( reduce(operator.or_, (Q(**f) for f in filters)), deleted=False, - report__isnull=True, # don't flag statuses that already have reports + report__isnull=True, # don't flag statuses that already have reports ) report_model = apps.get_model("bookwyrm", "Report", require_ready=True) - report_model.objects.bulk_create([ - report_model( - reporter=reporter, - note=_("Automatically generated report"), - user=s.user, - statuses=[s.id], - ) for s in statuses - ]) + report_model.objects.bulk_create( + [ + report_model( + reporter=reporter, + note=_("Automatically generated report"), + user=s.user, + statuses=[s.id], + ) + for s in statuses + ] + ) diff --git a/bookwyrm/templates/settings/automod/rules.html b/bookwyrm/templates/settings/automod/rules.html index 7555df2d6..d913d5964 100644 --- a/bookwyrm/templates/settings/automod/rules.html +++ b/bookwyrm/templates/settings/automod/rules.html @@ -17,7 +17,10 @@ {% trans "Auto-moderation rules will create reports for any user or status with fields matching the provided string." %} {% trans "At this time, reports are not being generated automatically, and you must manually trigger a scan." %}

- +
+ {% csrf_token %} + +
{% if success %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 3c37eef55..d2caa76ea 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -216,7 +216,12 @@ urlpatterns = [ ), # auto-moderation rules re_path(r"^settings/automod/?$", views.AutoMod.as_view(), name="settings-automod"), - re_path(r"^settings/automod/(?P\d+)/delete?$", views.automod_delete, name="settings-automod-delete"), + re_path( + r"^settings/automod/(?P\d+)/delete?$", + views.automod_delete, + name="settings-automod-delete", + ), + re_path(r"^settings/automod/run?$", views.run_automod, name="settings-automod-run"), # moderation re_path( r"^settings/reports/?$", views.ReportsAdmin.as_view(), name="settings-reports" diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index f80b24507..76e9ff024 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -2,7 +2,7 @@ # site admin from .admin.announcements import Announcements, Announcement from .admin.announcements import EditAnnouncement, delete_announcement -from .admin.automod import AutoMod, automod_delete +from .admin.automod import AutoMod, automod_delete, run_automod from .admin.dashboard import Dashboard from .admin.federation import Federation, FederatedServer from .admin.federation import AddFederatedServer, ImportServerBlocklist diff --git a/bookwyrm/views/admin/automod.py b/bookwyrm/views/admin/automod.py index a62ed0ca2..d9901d01c 100644 --- a/bookwyrm/views/admin/automod.py +++ b/bookwyrm/views/admin/automod.py @@ -42,12 +42,23 @@ class AutoMod(View): } return TemplateResponse(request, "settings/automod/rules.html", data) + @require_POST @permission_required("bookwyrm.moderate_user", raise_exception=True) @permission_required("bookwyrm.moderate_post", raise_exception=True) # pylint: disable=unused-argument def automod_delete(request, rule_id): - """ Remove a rule """ + """Remove a rule""" rule = get_object_or_404(models.AutoMod, id=rule_id) rule.delete() return redirect("settings-automod") + + +@require_POST +@permission_required("bookwyrm.moderate_user", raise_exception=True) +@permission_required("bookwyrm.moderate_post", raise_exception=True) +# pylint: disable=unused-argument +def run_automod(request): + """run scan""" + models.automod_task.delay() + return redirect("settings-automod")