From 39bb490257741c24c5225fcdef1babd9446eca3e Mon Sep 17 00:00:00 2001
From: Skia
Date: Sun, 3 Oct 2021 17:20:43 +0200
Subject: [PATCH] com: fix weekmail for the case of non-existing email
addresses
If an email address is set as destination for the Weekmail, the SMTP may
refuse it, and `smtplib` will throw a `SMTPRecipientsRefused` error,
containing the list of refused addresses. This commit provides an
interface for the weekmail sender to quickly unsubscribe the faulty
users, so that the next try sending the weekmail can be performed
successfully.
---
com/templates/com/weekmail_preview.jinja | 36 ++++++++++++++++++------
com/views.py | 25 ++++++++++++----
2 files changed, 47 insertions(+), 14 deletions(-)
diff --git a/com/templates/com/weekmail_preview.jinja b/com/templates/com/weekmail_preview.jinja
index 0f160419..44c4da0a 100644
--- a/com/templates/com/weekmail_preview.jinja
+++ b/com/templates/com/weekmail_preview.jinja
@@ -7,15 +7,33 @@
{% block content %}
{% trans %}Back{% endtrans %}
-{% if request.GET['send'] %}
-{% trans %}Are you sure you want to send this weekmail?{% endtrans %}
-{% if request.LANGUAGE_CODE != settings.LANGUAGE_CODE[:2] %}
-{% trans %}Warning: you are sending the weekmail in another language than the default one!{% endtrans %}
-{% endif %}
-
+{% if bad_recipients %}
+
+
+ {% trans %}The following recipients were refused by the SMTP:{% endtrans %}
+
+
+ {% for r in bad_recipients.keys() %}
+ - {{ r }}
+ {% endfor %}
+
+
+
+
+{% else %}
+ {% if request.GET['send'] %}
+ {% trans %}Are you sure you want to send this weekmail?{% endtrans %}
+ {% if request.LANGUAGE_CODE != settings.LANGUAGE_CODE[:2] %}
+ {% trans %}Warning: you are sending the weekmail in another language than the default one!{% endtrans %}
+ {% endif %}
+
+ {% endif %}
{% endif %}
{{ weekmail_rendered|safe }}
diff --git a/com/views.py b/com/views.py
index 16752fe8..02ddb50d 100644
--- a/com/views.py
+++ b/com/views.py
@@ -39,6 +39,7 @@ from django.core.exceptions import PermissionDenied
from django import forms
from datetime import timedelta
+from smtplib import SMTPRecipientsRefused
from com.models import Sith, News, NewsDate, Weekmail, WeekmailArticle, Screen, Poster
from core.views import (
@@ -414,22 +415,35 @@ class NewsDetailView(CanViewMixin, DetailView):
# Weekmail
-class WeekmailPreviewView(ComTabsMixin, CanEditPropMixin, DetailView):
+class WeekmailPreviewView(ComTabsMixin, QuickNotifMixin, CanEditPropMixin, DetailView):
model = Weekmail
template_name = "com/weekmail_preview.jinja"
success_url = reverse_lazy("com:weekmail")
current_tab = "weekmail"
+ def dispatch(self, request, *args, **kwargs):
+ self.bad_recipients = []
+ return super(WeekmailPreviewView, self).dispatch(request, *args, **kwargs)
+
def post(self, request, *args, **kwargs):
self.object = self.get_object()
- try:
- if request.POST["send"] == "validate":
+ if request.POST["send"] == "validate":
+ try:
self.object.send()
return HttpResponseRedirect(
reverse("com:weekmail") + "?qn_weekmail_send_success"
)
- except:
- pass
+ except SMTPRecipientsRefused as e:
+ self.bad_recipients = e.recipients
+ elif request.POST["send"] == "clean":
+ try:
+ self.object.send() # This should fail
+ except SMTPRecipientsRefused as e:
+ users = User.objects.filter(email__in=e.recipients.keys())
+ for u in users:
+ u.preferences.receive_weekmail = False
+ u.preferences.save()
+ self.quick_notif_list += ["qn_success"]
return super(WeekmailPreviewView, self).get(request, *args, **kwargs)
def get_object(self, queryset=None):
@@ -439,6 +453,7 @@ class WeekmailPreviewView(ComTabsMixin, CanEditPropMixin, DetailView):
"""Add rendered weekmail"""
kwargs = super(WeekmailPreviewView, self).get_context_data(**kwargs)
kwargs["weekmail_rendered"] = self.object.render_html()
+ kwargs["bad_recipients"] = self.bad_recipients
return kwargs