From 2a2dddf5f438cee2d35d30ffb630682b9cb3161a Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 17 Sep 2021 11:57:28 -0700 Subject: [PATCH] Adds IP blocklist model --- bookwyrm/middleware/__init__.py | 4 ++-- bookwyrm/models/__init__.py | 2 +- bookwyrm/models/antispam.py | 35 +++++++++++++++++++++++++++++++++ bookwyrm/models/site.py | 17 ---------------- 4 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 bookwyrm/models/antispam.py diff --git a/bookwyrm/middleware/__init__.py b/bookwyrm/middleware/__init__.py index 404ea976..03843c5a 100644 --- a/bookwyrm/middleware/__init__.py +++ b/bookwyrm/middleware/__init__.py @@ -1,3 +1,3 @@ """ look at all this nice middleware! """ -from timezone_middleware import TimezoneMiddleware -from ip_middleware import IPBlocklistMiddleware +from .timezone_middleware import TimezoneMiddleware +from .ip_middleware import IPBlocklistMiddleware diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index e8e1b696..4228371f 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -26,8 +26,8 @@ from .import_job import ImportJob, ImportItem from .site import SiteSettings, SiteInvite from .site import PasswordReset, InviteRequest -from .site import EmailBlocklist from .announcement import Announcement +from .antispam import EmailBlocklist, IPBlocklist cls_members = inspect.getmembers(sys.modules[__name__], inspect.isclass) activity_models = { diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py new file mode 100644 index 00000000..7a85bbcf --- /dev/null +++ b/bookwyrm/models/antispam.py @@ -0,0 +1,35 @@ +""" Lets try NOT to sell viagra """ +from django.db import models + +from .user import User + + +class EmailBlocklist(models.Model): + """blocked email addresses""" + + created_date = models.DateTimeField(auto_now_add=True) + domain = models.CharField(max_length=255, unique=True) + is_active = models.BooleanField(default=True) + + class Meta: + """default sorting""" + + ordering = ("-created_date",) + + @property + def users(self): + """find the users associated with this address""" + return User.objects.filter(email__endswith=f"@{self.domain}") + + +class IPBlocklist(models.Model): + """blocked ip addresses""" + + created_date = models.DateTimeField(auto_now_add=True) + address = models.CharField(max_length=255, unique=True) + is_active = models.BooleanField(default=True) + + class Meta: + """default sorting""" + + ordering = ("-created_date",) diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index 09732c2f..ee69a507 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -124,23 +124,6 @@ class PasswordReset(models.Model): return "https://{}/password-reset/{}".format(DOMAIN, self.code) -class EmailBlocklist(models.Model): - """blocked email addresses""" - - created_date = models.DateTimeField(auto_now_add=True) - domain = models.CharField(max_length=255, unique=True) - - class Meta: - """default sorting""" - - ordering = ("-created_date",) - - @property - def users(self): - """find the users associated with this address""" - return User.objects.filter(email__endswith=f"@{self.domain}") - - # pylint: disable=unused-argument @receiver(models.signals.post_save, sender=SiteSettings) def preview_image(instance, *args, **kwargs):