-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add management command to send emails
- Loading branch information
1 parent
84067f3
commit 801cb51
Showing
2 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import argparse | ||
import sys | ||
|
||
from django.core.exceptions import ImproperlyConfigured | ||
from django.core.management import BaseCommand | ||
from django.template import engines | ||
from django.template.backends.django import DjangoTemplates | ||
from django.urls import resolve, reverse | ||
|
||
from desecapi.models import User | ||
|
||
|
||
def _get_default_template_backend(): | ||
# Ad-hoc implementation of https://github.com/django/django/pull/15944 | ||
for backend in engines.all(): | ||
if isinstance(backend, DjangoTemplates): | ||
return backend | ||
raise ImproperlyConfigured("No DjangoTemplates backend is configured.") | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Reach out to users with an email. Takes email template on stdin.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('email', nargs='*', help='User(s) to contact, identified by their email addresses') | ||
parser.add_argument('--contentfile', nargs='?', type=argparse.FileType('r'), default=sys.stdin, | ||
help='File to take email content from. Defaults to stdin.') | ||
parser.add_argument('--reason', nargs='?', default='change-outreach-preference', | ||
help='Kind of message to send. Choose from reasons given in serializers.py. Defaults to ' | ||
'newsletter with unsubscribe link (reason: change-outreach-preference).') | ||
parser.add_argument('--subject', nargs='?', default=None, help='Subject, default according to "reason".') | ||
|
||
def handle(self, *args, **options): | ||
reason = options['reason'] | ||
path = reverse(f'v1:confirm-{reason}', args=['code']) | ||
serializer_class = resolve(path).func.cls.serializer_class | ||
|
||
content = options['contentfile'].read().strip() | ||
if not content and options['contentfile'].name != '/dev/null': | ||
raise RuntimeError('Empty content only allowed from /dev/null') | ||
|
||
try: | ||
subject = '[deSEC] ' + options['subject'] | ||
except TypeError: | ||
subject = None | ||
|
||
base_file = f'emails/{reason}/content.txt' | ||
template_code = ('{%% extends "%s" %%}' % base_file) | ||
if content: | ||
template_code += '{% block content %}' + content + '{% endblock %}' | ||
template = _get_default_template_backend().from_string(template_code) | ||
|
||
if options['email']: | ||
users = User.objects.filter(email__in=options['email']) | ||
elif content: | ||
users = User.objects.filter(outreach_preference=True) | ||
else: | ||
raise RuntimeError('To send default content, specify recipients explicitly.') | ||
|
||
for user in users: | ||
action = serializer_class.Meta.model(user=user) | ||
serializer_class(action).save(subject=subject, template=template) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters