Project API improvements

- Added DetailView for ProjectPart
- Improved URL structure
This commit is contained in:
Oliver Walters 2017-04-14 11:16:49 +10:00
parent 04524d38ab
commit b13f135d8e
2 changed files with 28 additions and 5 deletions

View File

@ -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<pk>[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)),
]

View File

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