mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 11:31:08 +00:00
Use BookWyrmModel as parent for admin models
This will make them more internally consistent and give them built-in permissions checking
This commit is contained in:
parent
b0236b95bd
commit
e51980bc12
7 changed files with 77 additions and 13 deletions
65
bookwyrm/migrations/0158_auto_20220919_1634.py
Normal file
65
bookwyrm/migrations/0158_auto_20220919_1634.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# Generated by Django 3.2.15 on 2022-09-19 16:34
|
||||||
|
|
||||||
|
import bookwyrm.models.fields
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.utils.timezone
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("bookwyrm", "0157_auto_20220909_2338"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="automod",
|
||||||
|
name="created_date",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True, default=django.utils.timezone.now
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="automod",
|
||||||
|
name="remote_id",
|
||||||
|
field=bookwyrm.models.fields.RemoteIdField(
|
||||||
|
max_length=255,
|
||||||
|
null=True,
|
||||||
|
validators=[bookwyrm.models.fields.validate_remote_id],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="automod",
|
||||||
|
name="updated_date",
|
||||||
|
field=models.DateTimeField(auto_now=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="emailblocklist",
|
||||||
|
name="remote_id",
|
||||||
|
field=bookwyrm.models.fields.RemoteIdField(
|
||||||
|
max_length=255,
|
||||||
|
null=True,
|
||||||
|
validators=[bookwyrm.models.fields.validate_remote_id],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="emailblocklist",
|
||||||
|
name="updated_date",
|
||||||
|
field=models.DateTimeField(auto_now=True),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="ipblocklist",
|
||||||
|
name="remote_id",
|
||||||
|
field=bookwyrm.models.fields.RemoteIdField(
|
||||||
|
max_length=255,
|
||||||
|
null=True,
|
||||||
|
validators=[bookwyrm.models.fields.validate_remote_id],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="ipblocklist",
|
||||||
|
name="updated_date",
|
||||||
|
field=models.DateTimeField(auto_now=True),
|
||||||
|
),
|
||||||
|
]
|
|
@ -8,13 +8,13 @@ from django.db.models import Q
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from bookwyrm.tasks import app
|
from bookwyrm.tasks import app
|
||||||
|
from .base_model import BookWyrmModel
|
||||||
from .user import User
|
from .user import User
|
||||||
|
|
||||||
|
|
||||||
class EmailBlocklist(models.Model):
|
class EmailBlocklist(BookWyrmModel):
|
||||||
"""blocked email addresses"""
|
"""blocked email addresses"""
|
||||||
|
|
||||||
created_date = models.DateTimeField(auto_now_add=True)
|
|
||||||
domain = models.CharField(max_length=255, unique=True)
|
domain = models.CharField(max_length=255, unique=True)
|
||||||
is_active = models.BooleanField(default=True)
|
is_active = models.BooleanField(default=True)
|
||||||
|
|
||||||
|
@ -29,10 +29,9 @@ class EmailBlocklist(models.Model):
|
||||||
return User.objects.filter(email__endswith=f"@{self.domain}")
|
return User.objects.filter(email__endswith=f"@{self.domain}")
|
||||||
|
|
||||||
|
|
||||||
class IPBlocklist(models.Model):
|
class IPBlocklist(BookWyrmModel):
|
||||||
"""blocked ip addresses"""
|
"""blocked ip addresses"""
|
||||||
|
|
||||||
created_date = models.DateTimeField(auto_now_add=True)
|
|
||||||
address = models.CharField(max_length=255, unique=True)
|
address = models.CharField(max_length=255, unique=True)
|
||||||
is_active = models.BooleanField(default=True)
|
is_active = models.BooleanField(default=True)
|
||||||
|
|
||||||
|
@ -42,7 +41,7 @@ class IPBlocklist(models.Model):
|
||||||
ordering = ("-created_date",)
|
ordering = ("-created_date",)
|
||||||
|
|
||||||
|
|
||||||
class AutoMod(models.Model):
|
class AutoMod(BookWyrmModel):
|
||||||
"""rules to automatically flag suspicious activity"""
|
"""rules to automatically flag suspicious activity"""
|
||||||
|
|
||||||
string_match = models.CharField(max_length=200, unique=True)
|
string_match = models.CharField(max_length=200, unique=True)
|
||||||
|
|
|
@ -34,7 +34,7 @@ class AutoMod(View):
|
||||||
"""add rule"""
|
"""add rule"""
|
||||||
form = forms.AutoModRuleForm(request.POST)
|
form = forms.AutoModRuleForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save(request)
|
||||||
form = forms.AutoModRuleForm()
|
form = forms.AutoModRuleForm()
|
||||||
|
|
||||||
data = automod_view_data()
|
data = automod_view_data()
|
||||||
|
@ -54,7 +54,7 @@ def schedule_automod_task(request):
|
||||||
return TemplateResponse(request, "settings/automod/rules.html", data)
|
return TemplateResponse(request, "settings/automod/rules.html", data)
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
schedule = form.save()
|
schedule = form.save(request)
|
||||||
PeriodicTask.objects.get_or_create(
|
PeriodicTask.objects.get_or_create(
|
||||||
interval=schedule,
|
interval=schedule,
|
||||||
name="automod-task",
|
name="automod-task",
|
||||||
|
|
|
@ -40,7 +40,7 @@ class EmailBlocklist(View):
|
||||||
return TemplateResponse(
|
return TemplateResponse(
|
||||||
request, "settings/email_blocklist/email_blocklist.html", data
|
request, "settings/email_blocklist/email_blocklist.html", data
|
||||||
)
|
)
|
||||||
form.save()
|
form.save(request)
|
||||||
|
|
||||||
data["form"] = forms.EmailBlocklistForm()
|
data["form"] = forms.EmailBlocklistForm()
|
||||||
return TemplateResponse(
|
return TemplateResponse(
|
||||||
|
|
|
@ -86,7 +86,7 @@ class AddFederatedServer(View):
|
||||||
return TemplateResponse(
|
return TemplateResponse(
|
||||||
request, "settings/federation/edit_instance.html", data
|
request, "settings/federation/edit_instance.html", data
|
||||||
)
|
)
|
||||||
server = form.save()
|
server = form.save(request)
|
||||||
return redirect("settings-federated-server", server.id)
|
return redirect("settings-federated-server", server.id)
|
||||||
|
|
||||||
|
|
||||||
|
@ -119,7 +119,7 @@ class ImportServerBlocklist(View):
|
||||||
server_name=server_name,
|
server_name=server_name,
|
||||||
)
|
)
|
||||||
server.notes = info_link
|
server.notes = info_link
|
||||||
server.save()
|
server.save(request)
|
||||||
server.block()
|
server.block()
|
||||||
success_count += 1
|
success_count += 1
|
||||||
data = {"failed": failed, "succeeded": success_count}
|
data = {"failed": failed, "succeeded": success_count}
|
||||||
|
@ -156,7 +156,7 @@ class FederatedServer(View):
|
||||||
"""update note"""
|
"""update note"""
|
||||||
server = get_object_or_404(models.FederatedServer, id=server)
|
server = get_object_or_404(models.FederatedServer, id=server)
|
||||||
server.notes = request.POST.get("notes")
|
server.notes = request.POST.get("notes")
|
||||||
server.save()
|
server.save(request)
|
||||||
return redirect("settings-federated-server", server.id)
|
return redirect("settings-federated-server", server.id)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ class IPBlocklist(View):
|
||||||
return TemplateResponse(
|
return TemplateResponse(
|
||||||
request, "settings/ip_blocklist/ip_blocklist.html", data
|
request, "settings/ip_blocklist/ip_blocklist.html", data
|
||||||
)
|
)
|
||||||
form.save()
|
form.save(request)
|
||||||
|
|
||||||
data["form"] = forms.IPBlocklistForm()
|
data["form"] = forms.IPBlocklistForm()
|
||||||
return TemplateResponse(
|
return TemplateResponse(
|
||||||
|
|
|
@ -88,6 +88,6 @@ class UserAdmin(View):
|
||||||
else:
|
else:
|
||||||
form = forms.UserGroupForm(request.POST, instance=user)
|
form = forms.UserGroupForm(request.POST, instance=user)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
form.save(request)
|
||||||
data = {"user": user, "group_form": form}
|
data = {"user": user, "group_form": form}
|
||||||
return TemplateResponse(request, "settings/users/user.html", data)
|
return TemplateResponse(request, "settings/users/user.html", data)
|
||||||
|
|
Loading…
Reference in a new issue