def test_notification_template_rendering(notification_template): context = { 'short_message_var': 'foo', 'subject_var': 'bar', 'body_var': 'baz', 'html_body_var': 'foo <b>bar</b> baz', } rendered = render_notification_template(NotificationType.TEST, context, 'en') assert len(rendered) == 4 assert rendered[ 'short_message'] == "test short message, variable value: foo!" assert rendered['subject'] == "test subject, variable value: bar!" assert rendered['body'] == "test body, variable value: baz!" assert rendered[ 'html_body'] == "test <b>HTML</b> body, variable value: foo <b>bar</b> baz!" rendered = render_notification_template(NotificationType.TEST, context, 'fi') assert len(rendered) == 4 assert rendered[ 'short_message'] == "testilyhytviesti, muuttujan arvo: foo!" assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!" assert rendered['body'] == "testiruumis, muuttujan arvo: baz!" assert rendered[ 'html_body'] == "testi<b>hötömölö</b>ruumis, muuttujan arvo: foo <b>bar</b> baz!"
def user_post_save(sender, instance, created, **kwargs): # Send notifications only if notifications are enabled if not settings.ENABLE_NOTIFICATIONS: return if created: User = get_user_model() recipient_list = [ item[0] for item in User.objects.filter( is_superuser=True).values_list('email') ] notification_type = NotificationType.USER_CREATED context = {'user': instance} if len(recipient_list) == 0: logger.warning("No recipients for notification type '%s'" % notification_type, extra={'event': instance}) return try: rendered_notification = render_notification_template( notification_type, context) except NotificationTemplateException as e: logger.error(e, exc_info=True) return try: send_mail(rendered_notification['subject'], rendered_notification['body'], 'noreply@%s' % Site.objects.get_current().domain, recipient_list, html_message=rendered_notification['html_body']) except SMTPException as e: logger.error(e, exc_info=True, extra={'user': instance})
def _send_notification(self, notification_type, recipient_list, request=None): if len(recipient_list) == 0: logger.warning("No recipients for notification type '%s'" % notification_type, extra={'event': self}) return context = {'event': self} try: rendered_notification = render_notification_template( notification_type, context) except NotificationTemplateException as e: logger.error(e, exc_info=True, extra={'request': request}) return try: send_mail(rendered_notification['subject'], rendered_notification['body'], 'noreply@%s' % Site.objects.get_current().domain, recipient_list, html_message=rendered_notification['html_body']) except SMTPException as e: logger.error(e, exc_info=True, extra={ 'request': request, 'event': self })
def _send_notification(self, request=None): target_object = self.content_object target_model = target_object.__class__ assert target_model in COMMENTABLE_MODELS.values() if target_model == CateringOrder: catering_provider = target_object.get_provider() email = catering_provider.notification_email if catering_provider else None reserver = target_object.reservation.user notification_type = NotificationType.CATERING_ORDER_COMMENT_CREATED elif target_model == Reservation: unit = target_object.resource.unit email = unit.manager_email if unit.manager_email else None reserver = target_object.user notification_type = NotificationType.RESERVATION_COMMENT_CREATED context = self.get_notification_context(DEFAULT_LANG) try: rendered_notification = render_notification_template(notification_type, context, DEFAULT_LANG) except NotificationTemplateException as e: logger.error(e, exc_info=True, extra={'request': request}) return if email: send_respa_mail(email, rendered_notification['subject'], rendered_notification['body']) if self.created_by != reserver and reserver.email: send_respa_mail(reserver.email, rendered_notification['subject'], rendered_notification['body'])
def send_reservation_mail(self, notification_type, user=None): """ Stuff common to all reservation related mails. If user isn't given use self.user. """ if user: email_address = user.email else: if not (self.reserver_email_address or self.user): return email_address = self.reserver_email_address or self.user.email user = self.user language = user.get_preferred_language() if user else DEFAULT_LANG context = self.get_notification_context(language) try: rendered_notification = render_notification_template( notification_type, context, language) except NotificationTemplateException as e: logger.error(e, exc_info=True, extra={'user': user.uuid}) return send_respa_mail(email_address, rendered_notification['subject'], rendered_notification['body'])
def user_post_save(sender, instance, created, **kwargs): if created: User = get_user_model() org = Organization.objects.get(id='yksilo:2000') org.private_users.add(instance) org.save() recipient_list = [ item[0] for item in User.objects.filter( is_superuser=True).values_list('email') ] notification_type = NotificationType.USER_CREATED context = {'user': instance} if len(recipient_list) == 0: logger.warning("No recipients for notification type '%s'" % notification_type, extra={'event': instance}) return try: rendered_notification = render_notification_template( notification_type, context) except NotificationTemplateException as e: logger.error(e, exc_info=True) return try: send_mail(rendered_notification['subject'], rendered_notification['body'], 'noreply@%s' % Site.objects.get_current().domain, recipient_list, html_message=rendered_notification['html_body']) except SMTPException as e: logger.error(e, exc_info=True, extra={'user': instance})
def test_notification_template_rendering(notification_template): context = { 'short_message_var': 'foo', 'subject_var': 'bar', 'body_var': 'baz', 'html_body_var': 'foo <b>bar</b> baz', } rendered = render_notification_template(NotificationType.TEST, context, 'en') assert len(rendered) == 4 assert rendered['short_message'] == "test short message, variable value: foo!" assert rendered['subject'] == "test subject, variable value: bar!" assert rendered['body'] == "test body, variable value: baz!" assert rendered['html_body'] == "test <b>HTML</b> body, variable value: foo <b>bar</b> baz!" rendered = render_notification_template(NotificationType.TEST, context, 'fi') assert len(rendered) == 4 assert rendered['short_message'] == "testilyhytviesti, muuttujan arvo: foo!" assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!" assert rendered['body'] == "testiruumis, muuttujan arvo: baz!" assert rendered['html_body'] == "testi<b>hötömölö</b>ruumis, muuttujan arvo: foo <b>bar</b> baz!"
def _send_notification(self, notification_type, request=None): provider = self.get_provider() email = provider.notification_email if provider else None if not email: return context = self.get_notification_context(DEFAULT_LANG) try: rendered_notification = render_notification_template( notification_type, context, DEFAULT_LANG) except NotificationTemplateException as e: logger.error(e, exc_info=True, extra={'request': request}) return send_respa_mail(email, rendered_notification['subject'], rendered_notification['body'])
def test_notification_template_format_datetime(notification_template): notification_template.body_en = "{{ datetime|format_datetime('en') }}" notification_template.save() dt = datetime(2020, 2, 22, 12, 0, 0, 0, pytz.utc) context = { 'subject_var': 'bar', 'datetime': dt, 'html_body_var': 'foo <b>bar</b> baz', } timezone.activate(pytz.timezone('Europe/Helsinki')) rendered = render_notification_template(NotificationType.TEST, context, 'en') assert rendered['body'] == '22 Feb 2020 at 14:00'
def test_notification_template_rendering_empty_html_body( notification_template): context = { 'subject_var': 'bar', 'body_var': 'baz', 'html_body_var': 'foo <b>bar</b> baz', } activate('fi') notification_template.html_body = '' notification_template.save() rendered = render_notification_template(NotificationType.TEST, context, 'fi') assert len(rendered) == 3 assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!" assert rendered['body'] == "testiruumis, muuttujan arvo: baz!" assert rendered['html_body'] == ""
def test_notification_template_rendering_empty_html_body(notification_template): context = { 'short_message_var': 'foo', 'subject_var': 'bar', 'body_var': 'baz', 'html_body_var': 'foo <b>bar</b> baz', } with switch_language(notification_template, 'fi'): notification_template.html_body = '' notification_template.save() rendered = render_notification_template(NotificationType.TEST, context, 'fi') assert len(rendered) == 4 assert rendered['short_message'] == "testilyhytviesti, muuttujan arvo: foo!" assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!" assert rendered['body'] == "testiruumis, muuttujan arvo: baz!" assert rendered['html_body'] == ""
def test_notification_template_rendering_empty_html_body( notification_template): context = { 'short_message_var': 'foo', 'subject_var': 'bar', 'body_var': 'baz', 'html_body_var': 'foo <b>bar</b> baz', } with switch_language(notification_template, 'fi'): notification_template.html_body = '' notification_template.save() rendered = render_notification_template(NotificationType.TEST, context, 'fi') assert len(rendered) == 4 assert rendered[ 'short_message'] == "testilyhytviesti, muuttujan arvo: foo!" assert rendered['subject'] == "testiotsikko, muuttujan arvo: bar!" assert rendered['body'] == "testiruumis, muuttujan arvo: baz!" assert rendered['html_body'] == ""