From b13f135d8e8ac2b7ed8a3a2485c8e42665b1f395 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 14 Apr 2017 11:16:49 +1000 Subject: [PATCH] Project API improvements - Added DetailView for ProjectPart - Improved URL structure --- InvenTree/project/urls.py | 14 ++++++++++++-- InvenTree/project/views.py | 19 ++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/InvenTree/project/urls.py b/InvenTree/project/urls.py index 1dd0fba554..4fc57c753f 100644 --- a/InvenTree/project/urls.py +++ b/InvenTree/project/urls.py @@ -9,9 +9,17 @@ from . import views projectdetailpatterns = [ # Single project detail url(r'^$', views.ProjectDetail.as_view()), +] + +projectpartpatterns = [ + # Detail of a single project part + url(r'^(?P[0-9]+)/$', views.ProjectPartDetail.as_view()), # Parts associated with a project - url(r'^parts/$', views.ProjectPartsList.as_view()), + url(r'^\?[^/]*/$', views.ProjectPartsList.as_view()), + + # All project parts + url(r'^$', views.ProjectPartsList.as_view()), ] projectcategorypatterns = [ @@ -23,7 +31,6 @@ projectcategorypatterns = [ # Create a new category url(r'^new/$', views.NewProjectCategory.as_view()) - ] urlpatterns = [ @@ -34,6 +41,9 @@ urlpatterns = [ # List of all projects url(r'^$', views.ProjectList.as_view()), + # Project parts + url(r'^parts/', include(projectpartpatterns)), + # Project categories url(r'^category/', include(projectcategorypatterns)), ] diff --git a/InvenTree/project/views.py b/InvenTree/project/views.py index af9992cf11..7c3518ee33 100644 --- a/InvenTree/project/views.py +++ b/InvenTree/project/views.py @@ -58,10 +58,23 @@ class ProjectPartsList(generics.ListCreateAPIView): permission_classes = (permissions.IsAuthenticatedOrReadOnly,) def get_queryset(self): - project_id = self.kwargs['pk'] - return ProjectPart.objects.filter(project=project_id) + project_id = self.request.query_params.get('project', None) + + if project_id: + return ProjectPart.objects.filter(project=project_id) + else: + return ProjectPart.objects.all() def create(self, request, *args, **kwargs): # Ensure project link is set correctly - request.data['project'] = self.kwargs['pk'] + request.data['project'] = self.request.query_params.get('project', None) return super(ProjectPartsList, self).create(request, *args, **kwargs) + + +class ProjectPartDetail(generics.RetrieveUpdateDestroyAPIView): + """ Detail for a single project part + """ + + queryset = ProjectPart.objects.all() + serializer_class = ProjectPartSerializer + permission_classes = (permissions.IsAuthenticatedOrReadOnly,)