From 8717d8a675409d11e38723509a24ca28f3599374 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 8 Sep 2021 15:30:18 -0700 Subject: [PATCH] Create new email domain block entries --- bookwyrm/migrations/0090_emailblocklist.py | 23 ++++++++----------- bookwyrm/models/site.py | 9 ++++++-- bookwyrm/templates/settings/domain_form.html | 17 +++++++++----- .../templates/settings/email_blocklist.html | 20 ++++++++++++++-- bookwyrm/views/email_blocklist.py | 14 +++++++++++ 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/bookwyrm/migrations/0090_emailblocklist.py b/bookwyrm/migrations/0090_emailblocklist.py index 57e27989a..2592e329e 100644 --- a/bookwyrm/migrations/0090_emailblocklist.py +++ b/bookwyrm/migrations/0090_emailblocklist.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.4 on 2021-09-08 21:37 +# Generated by Django 3.2.4 on 2021-09-08 22:21 from django.db import migrations, models @@ -6,24 +6,19 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ("bookwyrm", "0089_user_show_suggested_users"), + ('bookwyrm', '0089_user_show_suggested_users'), ] operations = [ migrations.CreateModel( - name="EmailBlocklist", + name='EmailBlocklist', fields=[ - ( - "id", - models.AutoField( - auto_created=True, - primary_key=True, - serialize=False, - verbose_name="ID", - ), - ), - ("created_date", models.DateTimeField(auto_now_add=True)), - ("domain", models.CharField(max_length=255)), + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_date', models.DateTimeField(auto_now_add=True)), + ('domain', models.CharField(max_length=255, unique=True)), ], + options={ + 'ordering': ('-created_date',), + }, ), ] diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index e3a657cc7..d9289b148 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -127,12 +127,17 @@ class EmailBlocklist(models.Model): """blocked email addresses""" created_date = models.DateTimeField(auto_now_add=True) - domain = models.CharField(max_length=255) + 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""" - User.objects.filter(email__endswith=f"@{self.domain}") + return User.objects.filter(email__endswith=f"@{self.domain}") # pylint: disable=unused-argument diff --git a/bookwyrm/templates/settings/domain_form.html b/bookwyrm/templates/settings/domain_form.html index e40a0d542..0919791e9 100644 --- a/bookwyrm/templates/settings/domain_form.html +++ b/bookwyrm/templates/settings/domain_form.html @@ -8,13 +8,18 @@ {% block form %}
{% csrf_token %} -
- - {{ form.domain }} - {% for error in form.domain.errors %} -

{{ error | escape }}

- {% endfor %} + +
+
+
@
+
+
+ {{ form.domain }} +
+ {% for error in form.domain.errors %} +

{{ error | escape }}

+ {% endfor %}
diff --git a/bookwyrm/templates/settings/email_blocklist.html b/bookwyrm/templates/settings/email_blocklist.html index 54779854a..e43b20adc 100644 --- a/bookwyrm/templates/settings/email_blocklist.html +++ b/bookwyrm/templates/settings/email_blocklist.html @@ -1,5 +1,7 @@ {% extends 'settings/layout.html' %} {% load i18n %} +{% load humanize %} + {% block title %}{% trans "Email Blocklist" %}{% endblock %} {% block header %}{% trans "Email Blocklist" %}{% endblock %} @@ -17,13 +19,14 @@ {% trans "When someone tries to register with an email from this domain, no account will be created. The registration process will appear to have worked." %}

- +
{% url 'settings-federation' as url %} + @@ -31,17 +34,30 @@ {% for domain in domains %} + {% endfor %}
{% trans "Domain" as text %} {% include 'snippets/table-sort-header.html' with field="server_name" sort=sort text=text %} {% trans "Options" %}
{{ domain.domain }} + + {% with user_count=domain.users.count %} + {% blocktrans trimmed count conter=user_count with display_count=user_count|intcomma %} + {{ display_count }} user + {% plural %} + {{ display_count }} users + {% endblocktrans %} + {% endwith %} + {% trans "Delete" as button_text %} + {% trans "Block users" as button_text %} +
-{% include 'snippets/pagination.html' with page=servers path=request.path %} {% endblock %} diff --git a/bookwyrm/views/email_blocklist.py b/bookwyrm/views/email_blocklist.py index 156b86277..36717f4a5 100644 --- a/bookwyrm/views/email_blocklist.py +++ b/bookwyrm/views/email_blocklist.py @@ -22,3 +22,17 @@ class EmailBlocklist(View): "form": forms.EmailBlocklistForm(), } return TemplateResponse(request, "settings/email_blocklist.html", data) + + def post(self, request): + """create a new domain block""" + form = forms.EmailBlocklistForm(request.POST) + data = { + "domains": models.EmailBlocklist.objects.order_by("-created_date").all(), + "form": form, + } + if not form.is_valid(): + return TemplateResponse(request, "settings/email_blocklist.html", data) + form.save() + + data["form"] = forms.EmailBlocklistForm() + return TemplateResponse(request, "settings/email_blocklist.html", data)