from django.contrib import admin
+from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from . import models
]}),
]
+ 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)
+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
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:
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):
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
else:
return JsonResponse({})
- meta = state().get_objects().model._meta
-
+ meta = state().get_example_object('').model._meta
help_text = _('''Context:<br>
<code>{{ %(model_name)s }}</code> – a <a href="%(docs_url)s">%(verbose_name)s</a> object.<br>