Skip to content
Snippets Groups Projects
Verified Commit 2f49ce1b authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Refactor notification logic out of model

parent 94f56d9e
No related branches found
No related tags found
1 merge request!137Generalise notifications and implement SMS notifications
......@@ -7,9 +7,8 @@ from django.utils.translation import ugettext_lazy as _
from image_cropping import ImageCropField, ImageRatioField
from phonenumber_field.modelfields import PhoneNumberField
from .mailer import send_mail_with_template
from templated_email import send_templated_mail
from .mixins import ExtensibleModel
from .util.notifications import send_notification
from constance import config
......@@ -221,20 +220,8 @@ class Notification(models.Model):
return self.title
def save(self, **kwargs):
if not self.sent:
context = {
"notification": self,
"notification_user": self.person.adressing_name,
}
send_templated_mail(
template_name='notification',
from_email=config.MAIL_OUT,
recipient_list=[self.person.email],
context=context,
)
self.sent = True
super().save(**kwargs)
send_notification(self)
class Meta:
verbose_name = _("Notification")
......
......@@ -4,9 +4,58 @@ from django.conf import settings
from django.utils.translation import gettext_lazy as _
from constance import config
from templated_email import send_templated_mail
from ..models import Notification
from .core_helpers import celery_optional
def get_notification_choices():
def _send_notification_email(notification: Notification, template: str = "notification") -> None:
context = {
"notification": notification,
"notification_user": notification.person.adressing_name,
}
send_templated_mail(
template_name=template,
from_email=config.MAIL_OUT,
recipient_list=[notification.person.email],
context=context,
)
# Mapping of channel id to name and two functions:
# - Check for availability
# - Send notification through it
_CHANNELS_MAP = {
"email": (_("E-Mail"), lambda: config.get("MAIL_OUT", None), _send_notification_email),
"sms": (_("SMS"), lambda: settings.get("TWILIO_SID", None), _send_notification_sms),
}
@celery_optional
def send_notification(notification: Union[int, Notification], resend: bool = False) -> None:
""" Send a notification through enabled channels.
If resend is passed as True, the notification is sent even if it was
previously marked as sent.
"""
channels = config.get("NOTIFICATION_CHANNELS", [])
if isinstance(notification, int):
notification = Notification.objects.get(pk=notification)
if resend or not notification.sent:
for channel in channels:
name, check, send = _CHANNEL_MAPS[channel]
if check():
send(notification)
notification.sent = True
notification.save()
def get_notification_choices() -> list:
""" Return all available channels for notifications.
This gathers the channels that are technically available as per the
......@@ -15,13 +64,9 @@ def get_notification_choices():
"""
choices = []
if config.get("MAIL_OUT", None):
choices += ("email", _("E-Mail"))
if settings.get("TWILIO_SID", None):
choices += ("sms", _("SMS"))
for channel, (name, check, send) in _CHANNEL_MAPS:
if check():
choices += (channel, name)
return choices
get_notification_choices_lazy = lazy(get_notification_choices, tuple)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment