From bd17458f37ea9f746b7a73bec6b370c92997b351 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 16 May 2022 21:40:10 +1000 Subject: [PATCH] Add metadata endpoints for SalesOrder and PurchaseOrder models --- InvenTree/order/api.py | 4 ++-- InvenTree/order/test_api.py | 32 ++++++++++++++++++++++++++++++++ InvenTree/part/test_part.py | 2 +- InvenTree/plugin/models.py | 10 +++++----- InvenTree/plugin/serializers.py | 4 +--- InvenTree/stock/api.py | 5 ++--- 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/InvenTree/order/api.py b/InvenTree/order/api.py index 2f307fd1a1..966a01eb93 100644 --- a/InvenTree/order/api.py +++ b/InvenTree/order/api.py @@ -354,7 +354,7 @@ class PurchaseOrderMetadata(generics.RetrieveUpdateAPIView): def get_serializer(self, *args, **kwargs): return MetadataSerializer(models.PurchaseOrder, *args, **kwargs) - + queryset = models.PurchaseOrder.objects.all() @@ -932,7 +932,7 @@ class SalesOrderMetadata(generics.RetrieveUpdateAPIView): def get_serializer(self, *args, **kwargs): return MetadataSerializer(models.SalesOrder, *args, **kwargs) - + queryset = models.SalesOrder.objects.all() diff --git a/InvenTree/order/test_api.py b/InvenTree/order/test_api.py index 2ac7689434..76aa8670a4 100644 --- a/InvenTree/order/test_api.py +++ b/InvenTree/order/test_api.py @@ -306,6 +306,22 @@ class PurchaseOrderTest(OrderTest): self.assertEqual(po.status, PurchaseOrderStatus.PLACED) + def test_po_metadata(self): + url = reverse('api-po-metadata', kwargs={'pk': 1}) + + self.patch( + url, + { + 'metadata': { + 'yam': 'yum', + } + }, + expected_code=200 + ) + + order = models.PurchaseOrder.objects.get(pk=1) + self.assertEqual(order.get_metadata('yam'), 'yum') + class PurchaseOrderReceiveTest(OrderTest): """ @@ -875,6 +891,22 @@ class SalesOrderTest(OrderTest): self.assertEqual(so.status, SalesOrderStatus.CANCELLED) + def test_so_metadata(self): + url = reverse('api-so-metadata', kwargs={'pk': 1}) + + self.patch( + url, + { + 'metadata': { + 'xyz': 'abc', + } + }, + expected_code=200 + ) + + order = models.SalesOrder.objects.get(pk=1) + self.assertEqual(order.get_metadata('xyz'), 'abc') + class SalesOrderAllocateTest(OrderTest): """ diff --git a/InvenTree/part/test_part.py b/InvenTree/part/test_part.py index e36d929cfe..f1bfcab40a 100644 --- a/InvenTree/part/test_part.py +++ b/InvenTree/part/test_part.py @@ -199,7 +199,7 @@ class PartTest(TestCase): with self.assertRaises(ValidationError): part_2.validate_unique() - def test_metadata(self): + def test_attributes(self): self.assertEqual(self.r1.name, 'R_2K2_0805') self.assertEqual(self.r1.get_absolute_url(), '/part/3/') diff --git a/InvenTree/plugin/models.py b/InvenTree/plugin/models.py index d2d263d14c..4db66a30fd 100644 --- a/InvenTree/plugin/models.py +++ b/InvenTree/plugin/models.py @@ -45,16 +45,16 @@ class MetadataMixin(models.Model): Args: key: String key for requesting metadata. e.g. if a plugin is accessing the metadata, the plugin slug should be used - + Returns: Python dict object containing requested metadata. If no matching metadata is found, returns None """ if self.metadata is None: return backup_value - + return self.metadata.get(key, backup_value) - + def set_metadata(self, key: str, data, commit=True): """ Save the provided metadata under the provided key. @@ -68,9 +68,9 @@ class MetadataMixin(models.Model): if self.metadata is None: # Handle a null field value self.metadata = {} - + self.metadata[key] = data - + if commit: self.save() diff --git a/InvenTree/plugin/serializers.py b/InvenTree/plugin/serializers.py index e4ca703d25..d03b892eb3 100644 --- a/InvenTree/plugin/serializers.py +++ b/InvenTree/plugin/serializers.py @@ -15,8 +15,6 @@ from django.utils import timezone from rest_framework import serializers -from InvenTree.helpers import str2bool - from plugin.models import PluginConfig, PluginSetting, NotificationUserSetting from common.serializers import GenericReferencedSettingSerializer @@ -37,7 +35,7 @@ class MetadataSerializer(serializers.ModelSerializer): fields = [ 'metadata', ] - + def update(self, instance, data): if self.partial: diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index b917c6d8ac..9f6c3b9191 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -1384,7 +1384,7 @@ class LocationMetadata(generics.RetrieveUpdateAPIView): def get_serializer(self, *args, **kwargs): return MetadataSerializer(StockLocation, *args, **kwargs) - + queryset = StockLocation.objects.all() @@ -1412,8 +1412,7 @@ stock_api_urls = [ re_path(r'^.*$', LocationDetail.as_view(), name='api-location-detail'), ])), - - + re_path(r'^.*$', StockLocationList.as_view(), name='api-location-list'), ])),