def action_url(self): return _get_object_link( self, field='action_object', url_only=True, absolute_url=True, )
def _get_target_url(self): related_obj = getattr(self, 'target') if isinstance(related_obj, load_model('config', 'Device')): return reverse('mk-device-status', args=[related_obj.id]) return _get_object_link(self, field='target', url_only=True, absolute_url=True)
def related_object(self, obj): target_object_url = _get_object_link(obj, field='target', html=False) if target_object_url.startswith('/admin/'): return format_html( '<a href="{url}" id="related-object-url">{content_type}: {name}</a>', url=target_object_url, content_type=obj.target_content_type.model, name=obj.target, ) return target_object_url
def message(self): if self.type: # setting links in notification object for message rendering self.actor_link = _get_object_link( self, field='actor', html=False, url_only=True, absolute_url=True ) self.action_link = _get_object_link( self, field='action_object', html=False, url_only=True, absolute_url=True, ) self.target_link = _get_object_link( self, field='target', html=False, url_only=True, absolute_url=True ) config = get_notification_configuration(self.type) try: if 'message' in config: md_text = config['message'].format(notification=self) else: md_text = render_to_string( config['message_template'], context=dict(notification=self) ).strip() except AttributeError as e: logger.error(e) md_text = ( 'Error while generating notification message,' ' notification data may have been deleted.' ) # clean up self.actor_link = self.action_link = self.target_link = None return mark_safe(markdown(md_text)) else: return self.description
def send_email_notification(sender, instance, created, **kwargs): # Abort if new notification is not created or # notification recipient opted out of email notifications if not created or not ( instance.recipient.notificationuser.email and instance.recipient.email ): return try: subject = instance.email_subject except NotificationException: # Do not send email if notification is malformed. return url = instance.data.get('url', '') if instance.data else None description = instance.message if url: target_url = url elif instance.target: target_url = _get_object_link( instance, field='target', html=False, url_only=True, absolute_url=True ) else: target_url = None if target_url: description += '\n\nFor more information see {0}.'.format(target_url) mail = EmailMultiAlternatives( subject=subject, body=strip_tags(description), from_email=settings.DEFAULT_FROM_EMAIL, to=[instance.recipient.email], ) if app_settings.OPENWISP_NOTIFICATION_HTML_EMAIL: html_message = render_to_string( app_settings.OPENWISP_NOTIFICATION_EMAIL_TEMPLATE, context=dict( OPENWISP_NOTIFICATION_EMAIL_LOGO=app_settings.OPENWISP_NOTIFICATION_EMAIL_LOGO, notification=instance, target_url=target_url, ), ) mail.attach_alternative(html_message, 'text/html') mail.send() # flag as emailed instance.emailed = True instance.save()
def target_url(self): return _get_object_link(self, field='target', url_only=True, absolute_url=True)
def target_object_link(self, obj): return _get_object_link(obj, field='target')
def action_object_object_link(self, obj): return _get_object_link(obj, field='action_object')
def actor_object_link(self, obj): return _get_object_link(obj, field='actor')
def get_config_error_notification_target_url(obj, field, absolute_url=True): url = _get_object_link(obj, field, absolute_url) return f'{url}#config-group'