Skip to content

Commit

Permalink
BaseAbstractTemplate: add support for locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kubov committed Jul 22, 2024
1 parent 9be4566 commit cdb90f4
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/dialer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Dialer messages that are stored inside Django model class defined later are sent

Function has two required parameters ``recipient`` which is a phone number of the receiver and ``content``. Attribute ``content`` is a text message that will be read via 'text to speech' mechanism to the recipient. Attribute ``related_objects`` should contain a list of objects that you want to connect with the sent message (with generic relation). ``tag`` is string mark which is stored with the sent message. The last non required parameter ``**kwargs`` is extra data that will be stored inside dialer message model in field ``extra_data``.

.. function:: pymess.backend.dialer.send_template(recipient, slug, context_data, related_objects=None, tag=None, send_immediately=False)
.. function:: pymess.backend.dialer.send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, send_immediately=False)

The second function is used for sending prepared templates that are stored inside template model (class that extends ``pymess.models.dialer.AbstractDialerTemplate``). The first parameter ``recipient`` is phone number of the receiver, ``slug`` is key of the template, ``context_data`` is a dictionary that contains context data for rendering dialer message content from the template, ``related_objects`` should contains list of objects that you want to connect with the sent message and ``tag`` is string mark which is stored with the sent message.

Expand Down
2 changes: 1 addition & 1 deletion docs/emails.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Like SMS E-mail messages are stored inside Django model class and sent via backe

Parameter ``sender`` define source e-mail address of the message, you can specify the name of the sender and pre header with optional parameter ``sender_name`` and ``pre_header``. ``recipient`` is destination e-mail address. Subject and HTML content of the e-mail message is defined with ``subject`` and ``content`` parameters. Attribute ``related_objects`` should contain a list of objects that you want to connect with the send message (with generic relation). Optional parameter ``attachments`` should contains list of files that will be sent with the e-mail in format ``({file name}, {output stream with file content}, {content type})``. ``tag`` is string mark which is stored with the sent SMS message . The last non required parameter ``**email_kwargs`` is extra data that will be stored inside e-mail message model in field ``extra_data``.

.. function:: pymess.backend.emails.send_template(recipient, slug, context_data, related_objects=None, attachments=None, tag=None, send_immediately=False)
.. function:: pymess.backend.emails.send_template(recipient, slug, context_data, locale=None, related_objects=None, attachments=None, tag=None, send_immediately=False)

The second function is used for sending prepared templates that are stored inside template model (class that extends ``pymess.models.sms.AbstractEmailTemplate``). The first parameter ``recipient`` is e-mail address of the receiver, ``slug`` is key of the template, ``context_data`` is a dictionary that contains context data for rendering e-mail content from the template, ``related_objects`` should contains list of objects that you want to connect with the send message, ``attachments`` should contains list of files that will be send with the e-mail and ``tag`` is string mark which is stored with the sent SMS message.

Expand Down
8 changes: 4 additions & 4 deletions docs/push.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PUSH notifications are stored inside Django model class defined later, are sent

Function has two required parameters ``recipient`` which is an identifier of the receiver and ``content``. Attribute ``content`` is a text message that will be sent inside the push notification. Attribute ``related_objects`` should contain a list of objects that you want to connect with the sent message (with generic relation). ``tag`` is string mark which is stored with the sent message . The last non required parameter ``**push_nofification_kwargs`` is extra data that will be stored inside push notification model in field ``extra_data``.

.. function:: pymess.backend.push.send_template(recipient, slug, context_data, related_objects=None, tag=None, send_immediately=False)
.. function:: pymess.backend.push.send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, send_immediately=False)

The second function is used for sending prepared templates that are stored inside template model (class that extends ``pymess.models.push.AbstractPushNotificationTemplate``). The first parameter ``recipient`` is identifier of the receiver, ``slug`` is key of the template, ``context_data`` is a dictionary that contains context data for rendering push notification content from the template, ``related_objects`` should contains list of objects that you want to connect with the sent message and ``tag`` is string mark which is stored with the sent push notification message.

Expand Down Expand Up @@ -51,7 +51,7 @@ Models

.. attribute:: template_slug

If push was sent from the template, this attribute cointains key of the template.
If push was sent from the template, this attribute contains key of the template.

.. attribute:: template

Expand All @@ -69,11 +69,11 @@ Models

.. attribute:: backend

Field contains path to the push backend that was used for sending of the push notifiaction.
Field contains path to the push backend that was used for sending of the push notification.

.. attribute:: error

If error was raised during sending of the push notifiaction this field contains text description of the error.
If error was raised during sending of the push notification this field contains text description of the error.

.. attribute:: extra_data

Expand Down
6 changes: 3 additions & 3 deletions docs/sms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
SMS
===

SMS messages that are stored inside Django model class defined later, are sent via SMS backend. There are implemented several SMS backends, every backed uses differend SMS service like twillio or AWS SNS. For sending SMS message you can use function ``pymess.backend.sms.send`` or ``pymwess.backend.sms.send_template``.
SMS messages that are stored inside Django model class defined later, are sent via SMS backend. There are implemented several SMS backends, every backed uses different SMS service like twillio or AWS SNS. For sending SMS message you can use function ``pymess.backend.sms.send`` or ``pymwess.backend.sms.send_template``.

.. function:: pymess.backend.sms.send(recipient, content, related_objects=None, tag=None, send_immediately=False, **kwargs)

Function has two required parameters ``recipient`` which is a phone number of the receiver and ``content``. Attribute ``content`` is a text message that will be sent inside the SMS body. If setting ``PYMESS_SMS_USE_ACCENT`` is set to ``False``, accent in the content will be replaced by appropriate ascii characters. Attribute ``related_objects`` should contain a list of objects that you want to connect with the sent message (with generic relation). ``tag`` is string mark which is stored with the sent SMS message . The last non required parameter ``**sms_kwargs`` is extra data that will be stored inside SMS message model in field ``extra_data``.

.. function:: pymess.backend.sms.send_template(recipient, slug, context_data, related_objects=None, tag=None, send_immediately=False)
.. function:: pymess.backend.sms.send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, send_immediately=False)

The second function is used for sending prepared templates that are stored inside template model (class that extends ``pymess.models.sms.AbstractSMSTemplate``). The first parameter ``recipient`` is phone number of the receiver, ``slug`` is key of the template, ``context_data`` is a dictionary that contains context data for rendering SMS content from the template, ``related_objects`` should contains list of objects that you want to connect with the sent message and ``tag`` is string mark which is stored with the sent SMS message.

Expand Down Expand Up @@ -47,7 +47,7 @@ Models

.. attribute:: template_slug

If SMS was sent from the template, this attribute cointains key of the template.
If SMS was sent from the template, this attribute contains key of the template.

.. attribute:: template

Expand Down
4 changes: 2 additions & 2 deletions pymess/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def get_retry_sending(self):
raise NotImplementedError


def send_template(recipient, slug, context_data, related_objects=None, tag=None, template_model=None, **kwargs):
def send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, template_model=None, **kwargs):
"""
Helper for building and sending message from a template.
:param recipient: email or phone number of the recipient
Expand All @@ -284,7 +284,7 @@ def send_template(recipient, slug, context_data, related_objects=None, tag=None,

assert template_model is not None, _l('template_model cannot be None')

return template_model.objects.get(slug=slug).send(
return template_model.objects.get(slug=slug, locale=locale).send(
recipient,
context_data,
related_objects=related_objects,
Expand Down
13 changes: 7 additions & 6 deletions pymess/backend/dialer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _update_dialer_states(self, messages):
raise NotImplementedError('Check dialer state is not supported with the backend')


def send_template(recipient, slug, context_data, related_objects=None, tag=None, send_immediately=False):
def send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, send_immediately=False):
"""
Helper for building and sending dialer message from a template.
:param recipient: phone number of the recipient
Expand All @@ -120,11 +120,12 @@ def send_template(recipient, slug, context_data, related_objects=None, tag=None,
:return: dialer message object or None if template cannot be sent
"""
return _send_template(
recipient,
slug,
context_data,
related_objects,
tag,
recipient=recipient,
slug=slug,
context_data=context_data,
locale=locale,
related_objects=related_objects,
tag=tag,
template_model=get_dialer_template_model(),
send_immediately=send_immediately
)
Expand Down
5 changes: 3 additions & 2 deletions pymess/backend/emails/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def pull_message_info(self, message):
raise NotImplementedError


def send_template(recipient, slug, context_data, related_objects=None, attachments=None, tag=None,
send_immediately=False):
def send_template(recipient, slug, context_data, locale=None, related_objects=None, attachments=None,
tag=None, send_immediately=False):
"""
Helper for building and sending e-mail message from a template.
:param recipient: e-mail address of the receiver
Expand All @@ -111,6 +111,7 @@ def send_template(recipient, slug, context_data, related_objects=None, attachmen
recipient=recipient,
slug=slug,
context_data=context_data,
locale=locale,
related_objects=related_objects,
tag=tag,
template_model=get_email_template_model(),
Expand Down
5 changes: 3 additions & 2 deletions pymess/backend/push/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def get_retry_sending(self):
return settings.PUSH_NOTIFICATION_RETRY_SENDING and is_turned_on_push_notification_batch_sending()


def send_template(recipient, slug, context_data, related_objects=None, tag=None, send_immediately=None):
def send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, send_immediately=None):
"""
Helper for building and sending push notification message from a template.
:param recipient: push notification recipient
:param slug: slug of a push notifiaction template
:param slug: slug of a push notification template
:param context_data: dict of data that will be sent to the template renderer
:param related_objects: list of related objects that will be linked with the push notification using generic
relation
Expand All @@ -67,6 +67,7 @@ def send_template(recipient, slug, context_data, related_objects=None, tag=None,
recipient=recipient,
slug=slug,
context_data=context_data,
locale=locale,
related_objects=related_objects,
tag=tag,
template_model=get_push_notification_template_model(),
Expand Down
3 changes: 2 additions & 1 deletion pymess/backend/sms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_retry_sending(self):
return settings.SMS_RETRY_SENDING and is_turned_on_sms_batch_sending()


def send_template(recipient, slug, context_data, related_objects=None, tag=None, send_immediately=False):
def send_template(recipient, slug, context_data, locale=None, related_objects=None, tag=None, send_immediately=False):
"""
Helper for building and sending SMS message from a template.
:param recipient: phone number of the recipient
Expand All @@ -130,6 +130,7 @@ def send_template(recipient, slug, context_data, related_objects=None, tag=None,
return _send_template(
recipient=recipient,
slug=slug,
locale=locale,
context_data=context_data,
related_objects=related_objects,
tag=tag,
Expand Down
27 changes: 20 additions & 7 deletions pymess/management/commands/sync_emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ class Command(BaseCommand):
Every e-mail body is named with e-mail slug and stored as a HTML file.
"""

def handle(self, *args, **options):
verbosity = int(options.get('verbosity'))
if verbosity > 0:
self.stdout.write('Syncing e-mails')
def _sync_for_locale(self, base_directory, locale):
locale_directory = os.path.join(base_directory, locale) if locale else base_directory

directory = os.path.join(settings.EMAIL_HTML_DATA_DIRECTORY)
files_with_email_html_body = [
filename for filename in os.listdir(directory) if filename.endswith('.html')
filename for filename in os.listdir(locale_directory) if filename.endswith('.html')
]
for filename in files_with_email_html_body:
try:
template_obj = get_email_template_model().objects.get(slug=filename[:-5])
with codecs.open(os.path.join(directory, filename), 'r', encoding='utf-8-sig') as file:
with codecs.open(os.path.join(locale_directory, filename), 'r', encoding='utf-8-sig') as file:
template_obj.change_and_save(body=file.read())
except ObjectDoesNotExist:
if verbosity > 0:
Expand All @@ -34,3 +31,19 @@ def handle(self, *args, **options):
self.stdout.write(
self.style.SUCCESS('Synced "{}" e-mail templates'.format(len(files_with_email_html_body)))
)

def handle(self, *args, **options):
verbosity = int(options.get('verbosity'))
if verbosity > 0:
self.stdout.write('Syncing e-mails')

directory = os.path.join(settings.EMAIL_HTML_DATA_DIRECTORY)
# All dirs in the root directory represent a locale.
# e.g., `$EMAIL_HTML_DATA_DIRECTORY/cs/`, `$EMAIL_HTML_DATA_DIRECTORY/sk/`.
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
if os.path.isdir(file_path):
self._sync_for_locale(directory, filename)

# The `$EMAIL_HTML_DATA_DIRECTORY/` directory itself represents the locale of None value.
self._sync_for_locale(directory, None)
Loading

0 comments on commit cdb90f4

Please sign in to comment.