예제 #1
0
    def get_message(self, email_message=False):
        if self.type:
            # setting links in notification object for message rendering
            self.actor_link = self.actor_url
            self.action_link = self.action_url
            self.target_link = (
                self.target_url if not email_message else self.redirect_view_url
            )

            config = get_notification_configuration(self.type)
            try:
                data = self.data or {}
                if 'message' in config:
                    md_text = config['message'].format(notification=self, **data)
                else:
                    md_text = render_to_string(
                        config['message_template'], context=dict(notification=self)
                    ).strip()
            except (AttributeError, KeyError) as e:
                from openwisp_notifications.tasks import delete_notification

                logger.error(e)
                delete_notification.delay(notification_id=self.pk)
                raise NotificationRenderException(
                    'Error in rendering notification message.'
                )
            # clean up
            self.actor_link = self.action_link = self.target_link = None
            return mark_safe(markdown(md_text))
        else:
            return self.description
    def _invalid_notification(self, pk, exception, error_message):
        from openwisp_notifications.tasks import delete_notification

        logger.error(exception)
        delete_notification.delay(notification_id=pk)
        if isinstance(exception, NotificationRenderException):
            raise exception
        raise NotificationRenderException(error_message)
예제 #3
0
    def email_subject(self):
        if self.type:
            try:
                config = get_notification_configuration(self.type)
                data = self.data or {}
                return config['email_subject'].format(
                    site=Site.objects.get_current(), notification=self, **data
                )
            except (AttributeError, KeyError) as e:
                from openwisp_notifications.tasks import delete_notification

                logger.error(e)
                delete_notification.delay(notification_id=self.pk)
                raise NotificationRenderException(
                    'Error while generating notification email'
                )
        elif self.data.get('email_subject', None):
            return self.data.get('email_subject')
        else:
            return self.message