diff --git a/docs/emails.rst b/docs/emails.rst index 0b990f1..de96419 100644 --- a/docs/emails.rst +++ b/docs/emails.rst @@ -305,3 +305,32 @@ Webhooks -------- Mandrill provides notification system which notifies your URL endpoint that some message status was changed. For this purpose pymess provides view ``pymess.webhooks.mandrill.MandrillWebhookView`` which you simply add to your ``django urls``. Every notification will mark message to be updated with the ``pull_emails_info`` command. + + +Migrations +---------- + +The library provides utilities to migrate e-mail templates into database. The e-mail bodies can be stored in the files with path defined in the setting ``EMAIL_HTML_DATA_DIRECTORY``. Every file should be named its the template slug. You can use the ``pymess.utils.migrations.SyncEmailTemplates`` migration helper to sync e-mail body as in the example:: + + # Django settings + EMAIL_HTML_DATA_DIRECTORY = '/data/emails + + # data/emails directory + data/emails + - set-pasword.html + - welcome.html + + # Migration + from django.db import migrations + from pymess.utils.migrations import SyncEmailTemplates + + + class Migration(migrations.Migration): + + dependencies = [ + ('communication', '0001_migration'), + ] + + operations = [ + migrations.RunPython(SyncEmailTemplates(('set-password', 'welcome))), # Body of the e-mails will be updated (e-mail templates must exist in the database) + ] diff --git a/docs/installation.rst b/docs/installation.rst index c2de63f..b42250e 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -61,9 +61,44 @@ SMS Country code that is set to the recipient if phone number doesn't contain another one. -.. attribute:: PYMESS_SMS_SENDER_BACKEND +.. attribute:: PYMESS_SMS_BACKENDS - Path to the SMS backend that will be used for sending SMS messages. Default value is ``'pymess.backend.sms.dummy.DummySMSBackend'``. + Path to the SMS backends that will be used for sending SMS messages. The default value is:: + + PYMESS_SMS_BACKENDS = { + 'default': { + 'backend': 'pymess.backend.sms.dummy.DummySMSBackend' + } + } + + It can be used more backends with different names (like ``DATABASES`` Django setting) + +.. attribute:: PYMESS_SMS_DEFAULT_SENDER_BACKEND_NAME + + Name of the default SMS sender backend. The default value is ``default``. + +.. attribute:: PYMESS_SMS_BACKEND_ROUTER + + Path to the router class which select SMS backend name according to a recipient value. The default value is ``'pymess.backend.routers.DefaultBackendRouter'`` which returns None (None value means the default backend name should be set). You can implement custom router with:: + + # sms_routers.py file + from pymess.backend.routers import BaseRouter + + class CustomRouter(BaseRouter): + + def get_backend_name(self, recipient): + return 'admin' if recipient in ADMIN_PHONES else None + + # django settings file + PYMESS_SMS_BACKEND_ROUTER = 'sms_routers.CustomRouter' + PYMESS_SMS_BACKENDS = { + 'default': { + 'backend': 'pymess.backend.sms.dummy.DummySMSBackend' + }, + 'admin': { + 'backend': 'path.to.the.SomeAdminBackend' + } + } .. attribute:: PYMESS_SMS_BATCH_SENDING @@ -81,6 +116,11 @@ SMS Defines maximum number of seconds to try to send a SMS message that ended in an ``ERROR_RETRY`` state. Default value is ``60 * 60`` (1 hour). +.. attribute:: PYMESS_SMS_RETRY_SENDING + + Setting defines if sending should be retried if fails. Works only together with batch sending. Default value is ``True``. + + E-MAIL ^^^^^^ @@ -112,9 +152,23 @@ E-MAIL List of HTML tags which cannot be used in the e-mail content. -.. attribute:: PYMESS_EMAIL_SENDER_BACKEND +.. attribute:: PYMESS_EMAIL_BACKENDS + + Path to the e-mail backends that will be used for sending E-mail messages. The default value is:: + + PYMESS_EMAIL_BACKENDS = { + 'default': { + 'backend': 'pymess.backend.emails.dummy.DummyEmailBackend' + } + } - Path to the E-mail backend that will be used for sending e-mail messages. Default value is ``'pymess.backend.emails.dummy.DummyEmailBackend'``. +.. attribute:: PYMESS_EMAIL_DEFAULT_SENDER_BACKEND_NAME + + Name of the default E-mail sender backend. The default value is ``default``. + +.. attribute:: PYMESS_EMAIL_BACKEND_ROUTER + + Path to the router class which select e-mail backend name according to a recipient value. The default value is ``'pymess.backend.routers.DefaultBackendRouter'`` which returns None (None value means the default backend name should be set). .. attribute:: PYMESS_EMAIL_BATCH_SENDING @@ -145,6 +199,10 @@ E-MAIL Path for storing e-mail attachments and contents (bodies). If changed after initial migration, existing files must be moved manually via data migration. +.. attribute:: PYMESS_EMAIL_RETRY_SENDING + + Setting defines if sending should be retried if fails. Works only together with batch sending. Default value is ``True``. + DIALER ^^^^^^ @@ -157,6 +215,24 @@ DIALER Path to the dialer backend that will be used for sending dialer messages. Default value is ``'pymess.backend.dialer.dummy.DummyDialerBackend'``. +.. attribute:: PYMESS_DIALER_BACKENDS + + Path to the dialer backends that will be used for sending dialer messages. The default value is:: + + PYMESS_DIALER_BACKENDS = { + 'default': { + 'backend': 'pymess.backend.dialer.dummy.DummyDialerBackend' + } + } + +.. attribute:: PYMESS_DIALER_DEFAULT_SENDER_BACKEND_NAME + + Name of the default dialer sender backend. The default value is ``default``. + +.. attribute:: PYMESS_DIALER_BACKEND_ROUTER + + Path to the router class which select dialer backend name according to a recipient value. The default value is ``'pymess.backend.routers.DefaultBackendRouter'`` which returns None (None value means the default backend name should be set). + .. attribute:: PYMESS_DIALER_BATCH_SENDING Because sending messages speed is dependent on the provider which can slow down your application speed, messages can be send in background with command ``send_messages_batch``. Default value is ``False``. @@ -181,6 +257,10 @@ DIALER Number of check attempts to get dialer message state. Default value is ``5`` +.. attribute:: PYMESS_DIALER_RETRY_SENDING + + Setting defines if sending should be retried if fails. Works only together with batch sending. Default value is ``True``. + Push notifications ^^^^^^^^^^^^^^^^^^ @@ -189,9 +269,23 @@ Push notifications If you want to use your own push notification template model you must set this setting with your custom push notification template model that extends ``pymess.models.push.AbstractPushNotificationMessage`` otherwise is used ``pymess.models.push.PushNotificationMessage``. -.. attribute:: PYMESS_PUSH_NOTIFICATION_SENDER_BACKEND +.. attribute:: PYMESS_PUSH_NOTIFICATION_BACKENDS + + Path to the push notification backends that will be used for sending dialer messages. The default value is:: + + PYMESS_PUSH_NOTIFICATION_BACKENDS = { + 'default': { + 'backend': 'pymess.backend.push.dummy.DummyPushNotificationBackend' + } + } + +.. attribute:: PYMESS_PUSH_NOTIFICATION_DEFAULT_SENDER_BACKEND_NAME + + Name of the default push notification sender backend. The default value is ``default``. - Path to the push notification backend that will be used for sending push notifications. Default value is ``'pymess.backend.push.dummy.DummyPushNotificationBackend'``. +.. attribute:: PYMESS_PUSH_NOTIFICATION_BACKEND_ROUTER + + Path to the router class which select push notification backend name according to a recipient value. The default value is ``'pymess.backend.routers.DefaultBackendRouter'`` which returns None (None value means the default backend name should be set). .. attribute:: PYMESS_PUSH_NOTIFICATION_BATCH_SENDING @@ -208,3 +302,8 @@ Push notifications .. attribute:: PYMESS_PUSH_NOTIFICATION_BATCH_MAX_SECONDS_TO_SEND Defines maximum number of seconds to try to send an push notification message that ended in an ``ERROR_RETRY`` state. Default value is ``60 * 60`` (1 hour). + +.. attribute:: PYMESS_PUSH_NOTIFICATION_RETRY_SENDING + + Setting defines if sending should be retried if fails. Works only together with batch sending. Default value is ``True``. + diff --git a/pymess/config.py b/pymess/config.py index d339433..dace18d 100644 --- a/pymess/config.py +++ b/pymess/config.py @@ -74,7 +74,7 @@ class ControllerType(Enum): 'EMAIL_PULL_INFO_DELAY_SECONDS': 60 * 60, # 1 hour 'EMAIL_PULL_INFO_MAX_TIMEOUT_FROM_SENT_SECONDS': 60 * 60 * 24 * 30, # 30 days 'EMAIL_RETRY_SENDING': True, - 'EMAIL_STORAGE_PATH' : 'pymess/emails', + 'EMAIL_STORAGE_PATH': 'pymess/emails', # Dialer configuration 'DIALER_BACKENDS': { diff --git a/pymess/utils/migrations.py b/pymess/utils/migrations.py index 04032dc..a6fceef 100644 --- a/pymess/utils/migrations.py +++ b/pymess/utils/migrations.py @@ -23,8 +23,10 @@ def __call__(self, apps, schema_editor): email_template_qs = email_template_class.objects.all() if self.template_slugs: email_template_qs = email_template_qs.filter(slug__in=self.template_slugs) - email_template_list = list(email_template_qs) - for email_template in email_template_list: + email_templates_found = list(email_template_qs) + if self.template_slugs: + email_templates_not_found = set(self.template_slugs) - {t.slug for t in email_templates_found} + assert not email_templates_not_found, f"Email templates {email_templates_not_found} were not found." + for email_template in email_templates_found: email_template.body = get_email_template_body_from_file(email_template.slug) - email_template_class.objects.bulk_update(email_template_list, ['body']) - + email_template_class.objects.bulk_update(email_templates_found, ['body']) diff --git a/pymess/version.py b/pymess/version.py index 4685410..224d93a 100644 --- a/pymess/version.py +++ b/pymess/version.py @@ -1,4 +1,4 @@ -VERSION = (0, 7, 5, 1) +VERSION = (0, 7, 6, 1) def get_version():