From 608057da6954bad38b873f30687df0f2350449d1 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 16 May 2020 23:36:03 +1000 Subject: [PATCH] Add option for user_detail in stockitemtestresult serializer --- InvenTree/stock/api.py | 10 ++++++++++ InvenTree/stock/serializers.py | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 342504b99a..0d257e606b 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -692,6 +692,16 @@ class StockItemTestResultList(generics.ListCreateAPIView): 'value', ] + def get_serializer(self, *args, **kwargs): + try: + kwargs['user_detail'] = str2bool(self.request.query_params.get('user_detail', False)) + except: + pass + + kwargs['context'] = self.get_serializer_context() + + return self.serializer_class(*args, **kwargs) + def perform_create(self, serializer): """ Create a new test result object. diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 3ab285baf9..5c33750fde 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -194,7 +194,7 @@ class LocationSerializer(InvenTreeModelSerializer): class StockItemAttachmentSerializer(InvenTreeModelSerializer): """ Serializer for StockItemAttachment model """ - def __init_(self, *args, **kwargs): + def __init__(self, *args, **kwargs): user_detail = kwargs.pop('user_detail', False) super().__init__(*args, **kwargs) @@ -221,6 +221,16 @@ class StockItemAttachmentSerializer(InvenTreeModelSerializer): class StockItemTestResultSerializer(InvenTreeModelSerializer): """ Serializer for the StockItemTestResult model """ + user_detail = UserSerializerBrief(source='user', read_only=True) + + def __init__(self, *args, **kwargs): + user_detail = kwargs.pop('user_detail', False) + + super().__init__(*args, **kwargs) + + if user_detail is not True: + self.fields.pop('user_detail') + class Meta: model = StockItemTestResult @@ -233,6 +243,7 @@ class StockItemTestResultSerializer(InvenTreeModelSerializer): 'attachment', 'notes', 'user', + 'user_detail', 'date' ]