From: Radek Czajka Date: Wed, 29 Jan 2020 09:04:48 +0000 (+0100) Subject: Sending test emails. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/4603878e7fc59d5cd25ec659d5012ddfe7b5152c?ds=inline Sending test emails. --- diff --git a/src/messaging/admin.py b/src/messaging/admin.py index 854079bee..558c34d8a 100644 --- a/src/messaging/admin.py +++ b/src/messaging/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin +from django.contrib import messages from django.utils.translation import ugettext_lazy as _ from . import models @@ -34,6 +35,21 @@ class EmailTemplateAdmin(admin.ModelAdmin): ]}), ] + def _test_email(self, request, obj): + if request.user.email: + obj.send_test_email(request.user.email) + messages.info(request, _('Test e-mail has been sent to %(email)s.') % {"email": request.user.email}) + else: + messages.warning(request, _('You have no email set. Test e-mail not sent.')) + + def response_add(self, request, obj): + self._test_email(request, obj) + return super().response_add(request, obj) + + def response_change(self, request, obj): + self._test_email(request, obj) + return super().response_change(request, obj) + admin.site.register(models.EmailTemplate, EmailTemplateAdmin) diff --git a/src/messaging/models.py b/src/messaging/models.py index 4f394c397..6f158e9a5 100644 --- a/src/messaging/models.py +++ b/src/messaging/models.py @@ -1,9 +1,11 @@ +from django.apps import apps from django.conf import settings from django.core.mail import send_mail from django.db import models from django.template import Template, Context from django.utils.translation import ugettext_lazy as _ from sentry_sdk import capture_exception +from .recipient import Recipient from .states import states @@ -51,7 +53,7 @@ class EmailTemplate(models.Model): return s raise ValueError('Unknown state', s.state) - def send(self, recipient, verbose=False, dry_run=False): + def send(self, recipient, verbose=False, dry_run=False, test=False): subject = Template(self.subject).render(Context(recipient.context)) body = Template(self.body).render(Context(recipient.context)) if verbose: @@ -62,12 +64,18 @@ class EmailTemplate(models.Model): except: capture_exception() else: - self.emailsent_set.create( - hash_value=recipient.hash_value, - email=recipient.email, - subject=subject, - body=body, - ) + if not test: + self.emailsent_set.create( + hash_value=recipient.hash_value, + email=recipient.email, + subject=subject, + body=body, + ) + + def send_test_email(self, email): + state = self.get_state()() + recipient = state.get_example_recipient(email) + self.send(recipient, test=True) class EmailSent(models.Model): diff --git a/src/messaging/states.py b/src/messaging/states.py index 36b5d05f1..44246e0f6 100644 --- a/src/messaging/states.py +++ b/src/messaging/states.py @@ -9,21 +9,40 @@ class State: context_fields = [] - def __init__(self, offset=0, time=None): + def __init__(self, time=None, min_days_since=None, max_days_since=None): self.time = time or now() - if isinstance(offset, int): - offset = timedelta(offset) - self.offset = offset + self.min_days_since = min_days_since + self.max_days_since = max_days_since def get_recipients(self): return [ - Recipient( + self.get_recipient(obj) + for obj in self.get_objects() + ] + + def get_recipient(self, obj): + return Recipient( hash_value=self.get_hash_value(obj), email=self.get_email(obj), context=self.get_context(obj), ) - for obj in self.get_objects() - ] + + def get_example_recipient(self, email): + return self.get_recipient( + self.get_example_object(email) + ) + + def get_example_object(self, email): + from club.models import Schedule + n = now() + return Schedule( + email=email, + key='xxxxxxxxx', + amount=100, + payed_at=n - timedelta(2), + started_at=n - timedelta(1), + expires_at=n + timedelta(1), + ) def get_objects(self): raise NotImplemented diff --git a/src/messaging/views.py b/src/messaging/views.py index be83d95f2..31debe180 100644 --- a/src/messaging/views.py +++ b/src/messaging/views.py @@ -13,8 +13,7 @@ def state_info(request, slug): else: return JsonResponse({}) - meta = state().get_objects().model._meta - + meta = state().get_example_object('').model._meta help_text = _('''Context:
{{ %(model_name)s }} – a %(verbose_name)s object.