mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Major improvements to Parts API
- Requires authentication (or read only) - Separated URL patterns - Better use of generics - Can add/edit parts and part categories
This commit is contained in:
parent
48f9914a49
commit
0772b8d780
@ -44,10 +44,10 @@ class PartCategoryBriefSerializer(serializers.ModelSerializer):
|
||||
class PartCategoryDetailSerializer(serializers.ModelSerializer):
|
||||
|
||||
# List of parts in this category
|
||||
parts = PartSerializer(many=True)
|
||||
parts = PartSerializer(many=True, read_only=True)
|
||||
|
||||
# List of child categories under this one
|
||||
children = PartCategoryBriefSerializer(many=True)
|
||||
children = PartCategoryBriefSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = PartCategory
|
||||
|
@ -1,19 +1,45 @@
|
||||
from django.conf.urls import url
|
||||
from django.conf.urls import url, include
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
# Single part detail
|
||||
url(r'^(?P<pk>[0-9]+)/$', views.PartDetail.as_view()),
|
||||
|
||||
# Part parameters list
|
||||
url(r'^(?P<pk>[0-9]+)/parameters/$', views.PartParameters.as_view()),
|
||||
""" URL patterns associated with part categories:
|
||||
/category -> List all top-level categories
|
||||
/category/<pk> -> Detail view of given category
|
||||
/category/new -> Create a new category
|
||||
"""
|
||||
categorypatterns = [
|
||||
|
||||
# Part category detail
|
||||
url(r'^category/(?P<pk>[0-9]+)/$', views.PartCategoryDetail.as_view()),
|
||||
|
||||
# List of top-level categories
|
||||
url(r'^category/$', views.PartCategoryList.as_view()),
|
||||
url(r'^$', views.PartCategoryList.as_view())
|
||||
]
|
||||
|
||||
""" URL patterns associated with a particular part:
|
||||
/part/<pk> -> Detail view of a given part
|
||||
/part/<pk>/parameters -> List parameters associated with a part
|
||||
"""
|
||||
partdetailpatterns = [
|
||||
# Single part detail
|
||||
url(r'^$', views.PartDetail.as_view()),
|
||||
|
||||
# View part parameters
|
||||
url(r'parameters/$', views.PartParameters.as_view())
|
||||
]
|
||||
|
||||
""" Top-level URL patterns for the Part app:
|
||||
/part/ -> List all parts
|
||||
/part/new -> Create a new part
|
||||
/part/<pk> -> (refer to partdetailpatterns)
|
||||
/part/category -> (refer to categorypatterns)
|
||||
"""
|
||||
urlpatterns = [
|
||||
# Individual part
|
||||
url(r'^(?P<pk>[0-9]+)/', include(partdetailpatterns)),
|
||||
|
||||
# Part categories
|
||||
url(r'^category/', views.PartCategoryList.as_view()),
|
||||
|
||||
# List of all parts
|
||||
url(r'^$', views.PartList.as_view())
|
||||
|
@ -1,42 +1,50 @@
|
||||
from rest_framework import generics
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
from .models import PartCategory, Part, PartParameter
|
||||
from .serializers import PartSerializer
|
||||
from .serializers import PartCategoryBriefSerializer
|
||||
from .serializers import PartCategoryDetailSerializer
|
||||
from .serializers import PartParameterSerializer
|
||||
|
||||
|
||||
class PartDetail(generics.RetrieveAPIView):
|
||||
|
||||
class PartDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
""" Return information on a single part
|
||||
"""
|
||||
queryset = Part.objects.all()
|
||||
serializer_class = PartSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class PartParameters(generics.ListAPIView):
|
||||
|
||||
class PartParameters(generics.ListCreateAPIView):
|
||||
""" Return all parameters associated with a particular part
|
||||
"""
|
||||
def get_queryset(self):
|
||||
part_id = self.kwargs['pk']
|
||||
return PartParameter.objects.filter(part=part_id)
|
||||
|
||||
serializer_class = PartParameterSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class PartList(generics.ListAPIView):
|
||||
class PartList(generics.ListCreateAPIView):
|
||||
|
||||
queryset = Part.objects.all()
|
||||
serializer_class = PartSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class PartCategoryDetail(generics.RetrieveAPIView):
|
||||
class PartCategoryDetail(generics.RetrieveUpdateAPIView):
|
||||
""" Return information on a single PartCategory
|
||||
"""
|
||||
queryset = PartCategory.objects.all()
|
||||
serializer_class = PartCategoryDetailSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class PartCategoryList(generics.ListAPIView):
|
||||
class PartCategoryList(generics.ListCreateAPIView):
|
||||
""" Return a list of all top-level part categories.
|
||||
Categories are considered "top-level" if they do not have a parent
|
||||
"""
|
||||
queryset = PartCategory.objects.filter(parent=None)
|
||||
serializer_class = PartCategoryDetailSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
Loading…
Reference in New Issue
Block a user