diff --git a/breccia_mapper/settings.py b/breccia_mapper/settings.py index 2a8cbcd..ba9e2a2 100644 --- a/breccia_mapper/settings.py +++ b/breccia_mapper/settings.py @@ -157,6 +157,7 @@ THIRD_PARTY_APPS = [ 'django_countries', 'django_select2', 'rest_framework', + 'post_office', ] FIRST_PARTY_APPS = [ @@ -378,12 +379,15 @@ else: # Import customisation app settings if present +CUSTOMISATION_NAME = None TEMPLATE_NAME_INDEX = 'index.html' +TEMPLATE_WELCOME_EMAIL_NAME = 'welcome-email' try: from custom.settings import ( CUSTOMISATION_NAME, - TEMPLATE_NAME_INDEX + TEMPLATE_NAME_INDEX, + TEMPLATE_WELCOME_EMAIL_NAME ) logger.info("Loaded customisation app: %s", CUSTOMISATION_NAME) diff --git a/people/__init__.py b/people/__init__.py index e69de29..a6e61af 100644 --- a/people/__init__.py +++ b/people/__init__.py @@ -0,0 +1 @@ +default_app_config = 'people.apps.PeopleConfig' diff --git a/people/apps.py b/people/apps.py index 3eae75a..f549ed6 100644 --- a/people/apps.py +++ b/people/apps.py @@ -1,5 +1,63 @@ +import logging + from django.apps import AppConfig +from django.conf import settings +from django.core import serializers +from django.db.models.signals import post_save + +logger = logging.getLogger(__name__) + + +def load_welcome_template_fixture(fixture_path) -> bool: + """Load welcome email template from a JSON fixture.""" + try: + with open(fixture_path) as f: + for deserialized in serializers.deserialize('json', f): + if deserialized.object.name == settings.TEMPLATE_WELCOME_EMAIL_NAME: + deserialized.save() + logger.warning('Welcome email template \'%s\' loaded', deserialized.object.name) + return True + + return False + + except FileNotFoundError: + logger.warning('Email template fixture not found.') + return False + + +def send_welcome_email(sender, instance, **kwargs): + from post_office import models + + try: + instance.send_welcome_email() + + except models.EmailTemplate.DoesNotExist: + logger.warning('Welcome email template \'%s\' not found - attempting to load from fixtures', + settings.TEMPLATE_WELCOME_EMAIL_NAME) + + is_loaded = False + if settings.CUSTOMISATION_NAME: + # Customisation app present - try here first + is_loaded |= load_welcome_template_fixture( + settings.BASE_DIR.joinpath('custom', 'fixtures', 'email_templates.json') + ) + + # |= operator shortcuts - only try here if we don't already have it + is_loaded |= load_welcome_template_fixture( + settings.BASE_DIR.joinpath('people', 'fixtures', 'email_templates.json') + ) + + if is_loaded: + instance.send_welcome_email() + + else: + logger.error('Welcome email template \'%s\' not found', settings.TEMPLATE_WELCOME_EMAIL_NAME) class PeopleConfig(AppConfig): name = 'people' + + def ready(self) -> None: + # Activate signal handlers + post_save.connect(send_welcome_email, + sender='people.user') diff --git a/people/fixtures/email_templates.json b/people/fixtures/email_templates.json new file mode 100644 index 0000000..78a8fd2 --- /dev/null +++ b/people/fixtures/email_templates.json @@ -0,0 +1,16 @@ +[ + { + "model": "post_office.emailtemplate", + "fields": { + "name": "welcome-email", + "description": "Default welcome email template", + "created": "2020-04-27T12:13:30.448Z", + "last_updated": "2020-04-27T14:45:27.152Z", + "subject": "Welcome to {{settings.PROJECT_LONG_NAME}}", + "content": "Dear {{ user.get_full_name }},\r\n\r\nWelcome to {{ settings.PROJECT_LONG_NAME }}.\r\n\r\nThanks,\r\n\r\nThe {{ settings.PROJECT_LONG_NAME }} team", + "html_content": "