diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py index fd86306627..f5b03fe9b4 100644 --- a/InvenTree/InvenTree/helpers.py +++ b/InvenTree/InvenTree/helpers.py @@ -696,3 +696,43 @@ def clean_decimal(number): return Decimal(0) return clean_number.quantize(Decimal(1)) if clean_number == clean_number.to_integral() else clean_number.normalize() + + +def get_objectreference(obj, type_ref: str = 'content_type', object_ref: str = 'object_id'): + """lookup method for the GenericForeignKey fields + + Attributes: + - obj: object that will be resolved + - type_ref: field name for the contenttype field in the model + - object_ref: field name for the object id in the model + + Example implementation in the serializer: + ``` + target = serializers.SerializerMethodField() + def get_target(self, obj): + return get_objectreference(obj, 'target_content_type', 'target_object_id') + ``` + + The method name must always be the name of the field prefixed by 'get_' + """ + model_cls = getattr(obj, type_ref) + obj_id = getattr(obj, object_ref) + + # check if references are set -> return nothing if not + if model_cls is None or obj_id is None: + return None + + # resolve referenced data into objects + model_cls = model_cls.model_class() + item = model_cls.objects.get(id=obj_id) + url_fnc = getattr(item, 'get_absolute_url', None) + + # create output + ret = {} + if url_fnc: + ret['link'] = url_fnc() + return { + 'name': str(item), + 'model': str(model_cls._meta.verbose_name), + **ret + } diff --git a/InvenTree/common/serializers.py b/InvenTree/common/serializers.py index fb73f8be13..e4fdf46c4c 100644 --- a/InvenTree/common/serializers.py +++ b/InvenTree/common/serializers.py @@ -6,6 +6,7 @@ JSON serializers for common components from __future__ import unicode_literals from InvenTree.serializers import InvenTreeModelSerializer +from InvenTree.helpers import get_objectreference from rest_framework import serializers @@ -102,7 +103,9 @@ class NotificationMessageSerializer(SettingsSerializer): Serializer for the InvenTreeUserSetting model """ - #content_object = serializers.PrimaryKeyRelatedField(read_only=True) + target = serializers.SerializerMethodField() + + source = serializers.SerializerMethodField() user = serializers.PrimaryKeyRelatedField(read_only=True) @@ -120,11 +123,18 @@ class NotificationMessageSerializer(SettingsSerializer): read = serializers.BooleanField() + def get_target(self, obj): + return get_objectreference(obj, 'target_content_type', 'target_object_id') + + def get_source(self, obj): + return get_objectreference(obj, 'source_content_type', 'source_object_id') + class Meta: model = NotificationMessage fields = [ 'pk', - #'content_object', + 'target', + 'source', 'user', 'category', 'name',