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
This commit is contained in:
Oliver 2018-05-05 00:51:17 +10:00
parent ee347c6165
commit 41e031d4b4
5 changed files with 35 additions and 5 deletions

View File

@ -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

View File

@ -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,
}

View File

@ -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<pk>\d+)/', PartDetail.as_view(), name='api-part-detail'),
url(r'^.*$', PartList.as_view(), name='api-part-list'),
]

View File

@ -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:

View File

@ -56,7 +56,7 @@ class StockItemSerializer(serializers.ModelSerializer):
'stocktake_date',
'stocktake_user',
'updated',
'quantity',
#'quantity',
]