2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 04:26:44 +00:00

Further API improvements

This commit is contained in:
Oliver Walters 2017-04-14 13:29:24 +10:00
parent 22ac5f4f79
commit 15d27cafec
5 changed files with 49 additions and 44 deletions

View File

@ -18,22 +18,18 @@ categorypatterns = [
partparampatterns = [ partparampatterns = [
# Detail of a single part parameter # Detail of a single part parameter
url(r'^(?P<pk>[0-9]+)/$', views.PartParamDetail.as_view()), url(r'^(?P<pk>[0-9]+)/?$', views.PartParamDetail.as_view()),
# Parameters associated with a particular part # Parameters associated with a particular part
url(r'^\?[^/]*/$', views.PartParamList.as_view()), url(r'^\?*[^/]*/?$', views.PartParamList.as_view()),
# All part parameters
url(r'^$', views.PartParamList.as_view()),
] ]
parttemplatepatterns = [ parttemplatepatterns = [
# Detail of a single part field template # Detail of a single part field template
url(r'^(?P<pk>[0-9]+)/$', views.PartTemplateDetail.as_view()), url(r'^(?P<pk>[0-9]+)/?$', views.PartTemplateDetail.as_view()),
# List all part field templates # List all part field templates
url(r'^$', views.PartTemplateList.as_view()) url(r'^$', views.PartTemplateList.as_view())
] ]
""" Top-level URL patterns for the Part app: """ Top-level URL patterns for the Part app:
@ -44,7 +40,7 @@ parttemplatepatterns = [
""" """
urlpatterns = [ urlpatterns = [
# Individual part # Individual part
url(r'^(?P<pk>[0-9]+)/$', views.PartDetail.as_view()), url(r'^(?P<pk>[0-9]+)/?$', views.PartDetail.as_view()),
# Part categories # Part categories
url(r'^category/?', include(categorypatterns)), url(r'^category/?', include(categorypatterns)),

View File

@ -69,8 +69,10 @@ class PartList(generics.ListCreateAPIView):
def get_queryset(self): def get_queryset(self):
parts = Part.objects.all() parts = Part.objects.all()
params = self.request.query_params
cat_id = params.get('category', None)
cat_id = self.request.query_params.get('category', None)
if cat_id: if cat_id:
parts = parts.filter(category=cat_id) parts = parts.filter(category=cat_id)

View File

@ -35,16 +35,10 @@ class ProjectCategoryBriefSerializer(serializers.ModelSerializer):
class ProjectCategoryDetailSerializer(serializers.ModelSerializer): class ProjectCategoryDetailSerializer(serializers.ModelSerializer):
projects = ProjectSerializer(many=True, read_only=True)
children = ProjectCategoryBriefSerializer(many=True, read_only=True)
class Meta: class Meta:
model = ProjectCategory model = ProjectCategory
fields = ('pk', fields = ('pk',
'name', 'name',
'description', 'description',
'parent', 'parent',
'path', 'path')
'children',
'projects')

View File

@ -13,34 +13,30 @@ projectdetailpatterns = [
projectpartpatterns = [ projectpartpatterns = [
# Detail of a single project part # Detail of a single project part
url(r'^(?P<pk>[0-9]+)/$', views.ProjectPartDetail.as_view()), url(r'^(?P<pk>[0-9]+)/?$', views.ProjectPartDetail.as_view()),
# List project parts, with optional filters # List project parts, with optional filters
url(r'^\?*[^/]*/?$', views.ProjectPartsList.as_view()), url(r'^\?*[^/]*/?$', views.ProjectPartsList.as_view()),
] ]
projectcategorypatterns = [ projectcategorypatterns = [
# List of top-level project categories
url(r'^$', views.ProjectCategoryList.as_view()),
# Detail of a single project category # Detail of a single project category
url(r'^(?P<pk>[0-9]+)/$', views.ProjectCategoryDetail.as_view()), url(r'^(?P<pk>[0-9]+)/?$', views.ProjectCategoryDetail.as_view()),
# Create a new category # List of project categories, with filters
url(r'^new/$', views.NewProjectCategory.as_view()) url(r'^\?*[^/]*/?$', views.ProjectCategoryList.as_view()),
] ]
urlpatterns = [ urlpatterns = [
# Individual project URL # Individual project URL
url(r'^(?P<pk>[0-9]+)/', include(projectdetailpatterns)), url(r'^(?P<pk>[0-9]+)/?$', include(projectdetailpatterns)),
# List of all projects # List of all projects
url(r'^$', views.ProjectList.as_view()), url(r'^$', views.ProjectList.as_view()),
# Project parts # Project parts
url(r'^parts/', include(projectpartpatterns)), url(r'^parts/?', include(projectpartpatterns)),
# Project categories # Project categories
url(r'^category/', include(projectcategorypatterns)), url(r'^category/?', include(projectcategorypatterns)),
] ]

View File

@ -1,5 +1,6 @@
from rest_framework import generics, permissions from rest_framework import generics, permissions
from InvenTree.models import FilterChildren
from .models import ProjectCategory, Project, ProjectPart from .models import ProjectCategory, Project, ProjectPart
from .serializers import ProjectSerializer from .serializers import ProjectSerializer
from .serializers import ProjectCategoryDetailSerializer from .serializers import ProjectCategoryDetailSerializer
@ -16,21 +17,24 @@ class ProjectDetail(generics.RetrieveUpdateDestroyAPIView):
class ProjectList(generics.ListCreateAPIView): class ProjectList(generics.ListCreateAPIView):
""" List all projects """ List projects
""" """
queryset = Project.objects.all() def get_queryset(self):
projects = Project.objects.all()
params = self.request.query_params
cat_id = params.get('category', None)
if cat_id:
projects = projects.filter(category=cat_id)
return projects
serializer_class = ProjectSerializer serializer_class = ProjectSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class NewProjectCategory(generics.CreateAPIView):
""" Create a new Project Category
"""
serializer_class = ProjectCategoryDetailSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class ProjectCategoryDetail(generics.RetrieveUpdateAPIView): class ProjectCategoryDetail(generics.RetrieveUpdateAPIView):
""" Project details """ Project details
""" """
@ -41,29 +45,42 @@ class ProjectCategoryDetail(generics.RetrieveUpdateAPIView):
class ProjectCategoryList(generics.ListCreateAPIView): class ProjectCategoryList(generics.ListCreateAPIView):
""" Top-level project categories. """ List project categories
Projects are considered top-level if they do not have a parent
""" """
queryset = ProjectCategory.objects.filter(parent=None) def get_queryset(self):
params = self.request.query_params
categories = ProjectCategory.objects.all()
categories = FilterChildren(categories, params.get('parent', None))
return categories
serializer_class = ProjectCategoryDetailSerializer serializer_class = ProjectCategoryDetailSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class ProjectPartsList(generics.ListCreateAPIView): class ProjectPartsList(generics.ListCreateAPIView):
""" List all parts associated with a particular project """ List project parts
""" """
serializer_class = ProjectPartSerializer serializer_class = ProjectPartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def get_queryset(self): def get_queryset(self):
project_id = self.request.query_params.get('project', None) parts = ProjectPart.objects.all()
params = self.request.query_params
project_id = params.get('project', None)
if project_id: if project_id:
return ProjectPart.objects.filter(project=project_id) parts = parts.filter(project=project_id)
else:
return ProjectPart.objects.all() part_id = params.get('part', None)
if part_id:
parts = parts.filter(part=part_id)
return parts
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
# Ensure project link is set correctly # Ensure project link is set correctly