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.
This commit is contained in:
Skia 2021-10-03 17:20:43 +02:00
parent 7a7aad0503
commit 39bb490257
2 changed files with 47 additions and 14 deletions

View File

@ -7,15 +7,33 @@
{% block content %} {% block content %}
<a href="{{ url('com:weekmail') }}">{% trans %}Back{% endtrans %}</a> <a href="{{ url('com:weekmail') }}">{% trans %}Back{% endtrans %}</a>
{% if request.GET['send'] %} {% if bad_recipients %}
<p>{% trans %}Are you sure you want to send this weekmail?{% endtrans %}</p> <p>
{% if request.LANGUAGE_CODE != settings.LANGUAGE_CODE[:2] %} <span class="important">
<p><strong>{% trans %}Warning: you are sending the weekmail in another language than the default one!{% endtrans %}</strong></p> {% trans %}The following recipients were refused by the SMTP:{% endtrans %}
{% endif %} </span>
<form method="post" action=""> <ul>
{% csrf_token %} {% for r in bad_recipients.keys() %}
<button type="submit" name="send" value="validate">{% trans %}Send{% endtrans %}</button> <li>{{ r }}</li>
</form> {% endfor %}
</ul>
</p>
<form method="post" action="">
{% csrf_token %}
<button type="submit" name="send" value="clean">{% trans %}Clean subscribers{% endtrans %}</button>
</form>
{% else %}
{% if request.GET['send'] %}
<p>{% trans %}Are you sure you want to send this weekmail?{% endtrans %}</p>
{% if request.LANGUAGE_CODE != settings.LANGUAGE_CODE[:2] %}
<p><strong>{% trans %}Warning: you are sending the weekmail in another language than the default one!{% endtrans %}</strong></p>
{% endif %}
<form method="post" action="">
{% csrf_token %}
<button type="submit" name="send" value="validate">{% trans %}Send{% endtrans %}</button>
</form>
{% endif %}
{% endif %} {% endif %}
<hr> <hr>
{{ weekmail_rendered|safe }} {{ weekmail_rendered|safe }}

View File

@ -39,6 +39,7 @@ from django.core.exceptions import PermissionDenied
from django import forms from django import forms
from datetime import timedelta from datetime import timedelta
from smtplib import SMTPRecipientsRefused
from com.models import Sith, News, NewsDate, Weekmail, WeekmailArticle, Screen, Poster from com.models import Sith, News, NewsDate, Weekmail, WeekmailArticle, Screen, Poster
from core.views import ( from core.views import (
@ -414,22 +415,35 @@ class NewsDetailView(CanViewMixin, DetailView):
# Weekmail # Weekmail
class WeekmailPreviewView(ComTabsMixin, CanEditPropMixin, DetailView): class WeekmailPreviewView(ComTabsMixin, QuickNotifMixin, CanEditPropMixin, DetailView):
model = Weekmail model = Weekmail
template_name = "com/weekmail_preview.jinja" template_name = "com/weekmail_preview.jinja"
success_url = reverse_lazy("com:weekmail") success_url = reverse_lazy("com:weekmail")
current_tab = "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): def post(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
try: if request.POST["send"] == "validate":
if request.POST["send"] == "validate": try:
self.object.send() self.object.send()
return HttpResponseRedirect( return HttpResponseRedirect(
reverse("com:weekmail") + "?qn_weekmail_send_success" reverse("com:weekmail") + "?qn_weekmail_send_success"
) )
except: except SMTPRecipientsRefused as e:
pass 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) return super(WeekmailPreviewView, self).get(request, *args, **kwargs)
def get_object(self, queryset=None): def get_object(self, queryset=None):
@ -439,6 +453,7 @@ class WeekmailPreviewView(ComTabsMixin, CanEditPropMixin, DetailView):
"""Add rendered weekmail""" """Add rendered weekmail"""
kwargs = super(WeekmailPreviewView, self).get_context_data(**kwargs) kwargs = super(WeekmailPreviewView, self).get_context_data(**kwargs)
kwargs["weekmail_rendered"] = self.object.render_html() kwargs["weekmail_rendered"] = self.object.render_html()
kwargs["bad_recipients"] = self.bad_recipients
return kwargs return kwargs