Adds task

This commit is contained in:
Mouse Reeve 2022-02-24 12:15:31 -08:00
parent e837da37db
commit 3ce8b3390e
6 changed files with 68 additions and 29 deletions

View file

@ -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,
),
),
],
),
]

View file

@ -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
]
)

View file

@ -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 <em>not</em> being generated automatically, and you must manually trigger a scan." %}
</p>
<button class="button is-warning">{% trans "Run scan" %}</button>
<form name="run-scan" method="POST" action="{% url 'settings-automod-run' %}">
{% csrf_token %}
<button class="button is-warning">{% trans "Run scan" %}</button>
</form>
</div>
{% if success %}

View file

@ -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<rule_id>\d+)/delete?$", views.automod_delete, name="settings-automod-delete"),
re_path(
r"^settings/automod/(?P<rule_id>\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"

View file

@ -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

View file

@ -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")