Add links to all notifications that do not have a link (#3431)

* refactor link

* [FR] Error notifications should provide link to the error
Fixes #3367
This commit is contained in:
Matthias Mair 2022-07-31 03:23:07 +02:00 committed by GitHub
parent 742e579fa7
commit ae07247a82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -650,13 +650,15 @@ def after_error_logged(sender, instance: Error, created: bool, **kwargs):
users = get_user_model().objects.filter(is_staff=True)
link = InvenTree.helpers.construct_absolute_url(
reverse('admin:error_report_error_change', kwargs={'object_id': instance.pk})
)
context = {
'error': instance,
'name': _('Server Error'),
'message': _('An error has been logged by the server.'),
'link': InvenTree.helpers.construct_absolute_url(
reverse('admin:error_report_error_change', kwargs={'object_id': instance.pk})
)
'link': link
}
common.notifications.trigger_notification(

View File

@ -1,10 +1,12 @@
"""JSON serializers for common components."""
from django.urls import reverse
from rest_framework import serializers
from common.models import (InvenTreeSetting, InvenTreeUserSetting,
NotificationMessage)
from InvenTree.helpers import get_objectreference
from InvenTree.helpers import construct_absolute_url, get_objectreference
from InvenTree.serializers import InvenTreeModelSerializer
@ -157,7 +159,22 @@ class NotificationMessageSerializer(InvenTreeModelSerializer):
def get_target(self, obj):
"""Function to resolve generic object reference to target."""
return get_objectreference(obj, 'target_content_type', 'target_object_id')
target = get_objectreference(obj, 'target_content_type', 'target_object_id')
if 'link' not in target:
# Check if objekt has an absolute_url function
if hasattr(obj.target_object, 'get_absolute_url'):
target['link'] = obj.target_object.get_absolute_url()
else:
# check if user is staff - link to admin
request = self.context['request']
if request.user and request.user.is_staff:
meta = obj.target_object._meta
target['link'] = construct_absolute_url(reverse(
f'admin:{meta.db_table}_change',
kwargs={'object_id': obj.target_object_id}
))
return target
def get_source(self, obj):
"""Function to resolve generic object reference to source."""