From 41e031d4b41d918c5fa8f4a155ed81170dcd7eb7 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 5 May 2018 00:51:17 +1000 Subject: [PATCH] Draft API endpoint RUD class - RUD = Retrieve / Update / Destroy - When issuing an Update command, the validity is checked but the model object is only saved if the POST data has "_is_final": true --- InvenTree/InvenTree/serializers.py | 17 +++++++++++++++++ InvenTree/InvenTree/settings.py | 3 ++- InvenTree/part/api.py | 14 +++++++++++++- InvenTree/stock/api.py | 4 ++-- InvenTree/stock/serializers.py | 2 +- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 InvenTree/InvenTree/serializers.py diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py new file mode 100644 index 0000000000..36daafad1d --- /dev/null +++ b/InvenTree/InvenTree/serializers.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from rest_framework import serializers +from rest_framework import generics +from rest_framework import mixins + +class DraftRUDView(generics.RetrieveAPIView, generics.UpdateAPIView, generics.DestroyAPIView): + + def perform_update(self, serializer): + + ctx_data = serializer._context['request'].data + + if ctx_data.get('_is_final', False) in [True, u'true', u'True', 1]: + super(generics.UpdateAPIView, self).perform_update(serializer) + else: + pass \ No newline at end of file diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 14a4496f12..a8df6c6d5e 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -92,7 +92,8 @@ TEMPLATES = [ ] REST_FRAMEWORK = { - 'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler', + 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler' + # 'EXCEPTION_HANDLER': 'InvenTree.utils.api_exception_handler', # 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', # 'PAGE_SIZE': 50, } diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 2ae506ae94..b67b1dd8fb 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters -from rest_framework import generics, permissions +from rest_framework import generics, permissions, mixins from django.conf.urls import url, include @@ -15,6 +15,7 @@ from .serializers import SupplierPartSerializer from .serializers import CategorySerializer from InvenTree.views import TreeSerializer +from InvenTree.serializers import DraftRUDView class PartCategoryTree(TreeSerializer): @@ -52,6 +53,14 @@ class CategoryList(generics.ListCreateAPIView): ] +class PartDetail(DraftRUDView): + queryset = Part.objects.all() + serializer_class = PartSerializer + + permission_classes = [ + permissions.IsAuthenticatedOrReadOnly, + ] + class PartList(generics.ListCreateAPIView): queryset = Part.objects.all() @@ -137,5 +146,8 @@ part_api_urls = [ url(r'^supplier/?', SupplierPartList.as_view(), name='api-part-supplier-list'), url(r'^bom/?', BomList.as_view(), name='api-bom-list'), + + url(r'^(?P\d+)/', PartDetail.as_view(), name='api-part-detail'), + url(r'^.*$', PartList.as_view(), name='api-part-list'), ] diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index b754c8fc7a..cc222ba96a 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -11,14 +11,14 @@ from .serializers import StockItemSerializer, StockQuantitySerializer from .serializers import LocationSerializer from InvenTree.views import TreeSerializer - +from InvenTree.serializers import DraftRUDView class StockCategoryTree(TreeSerializer): title = 'Stock' model = StockLocation -class StockDetail(generics.RetrieveUpdateDestroyAPIView): +class StockDetail(DraftRUDView): """ get: diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index eb3217b524..54d4dbf883 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -56,7 +56,7 @@ class StockItemSerializer(serializers.ModelSerializer): 'stocktake_date', 'stocktake_user', 'updated', - 'quantity', + #'quantity', ]