mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Further API improvements
This commit is contained in:
parent
22ac5f4f79
commit
15d27cafec
@ -18,22 +18,18 @@ categorypatterns = [
|
||||
|
||||
partparampatterns = [
|
||||
# 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
|
||||
url(r'^\?[^/]*/$', views.PartParamList.as_view()),
|
||||
|
||||
# All part parameters
|
||||
url(r'^$', views.PartParamList.as_view()),
|
||||
url(r'^\?*[^/]*/?$', views.PartParamList.as_view()),
|
||||
]
|
||||
|
||||
parttemplatepatterns = [
|
||||
# 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
|
||||
url(r'^$', views.PartTemplateList.as_view())
|
||||
|
||||
]
|
||||
|
||||
""" Top-level URL patterns for the Part app:
|
||||
@ -44,7 +40,7 @@ parttemplatepatterns = [
|
||||
"""
|
||||
urlpatterns = [
|
||||
# Individual part
|
||||
url(r'^(?P<pk>[0-9]+)/$', views.PartDetail.as_view()),
|
||||
url(r'^(?P<pk>[0-9]+)/?$', views.PartDetail.as_view()),
|
||||
|
||||
# Part categories
|
||||
url(r'^category/?', include(categorypatterns)),
|
||||
|
@ -69,8 +69,10 @@ class PartList(generics.ListCreateAPIView):
|
||||
|
||||
def get_queryset(self):
|
||||
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:
|
||||
parts = parts.filter(category=cat_id)
|
||||
|
||||
|
@ -35,16 +35,10 @@ class ProjectCategoryBriefSerializer(serializers.ModelSerializer):
|
||||
|
||||
class ProjectCategoryDetailSerializer(serializers.ModelSerializer):
|
||||
|
||||
projects = ProjectSerializer(many=True, read_only=True)
|
||||
|
||||
children = ProjectCategoryBriefSerializer(many=True, read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = ProjectCategory
|
||||
fields = ('pk',
|
||||
'name',
|
||||
'description',
|
||||
'parent',
|
||||
'path',
|
||||
'children',
|
||||
'projects')
|
||||
'path')
|
||||
|
@ -13,34 +13,30 @@ projectdetailpatterns = [
|
||||
|
||||
projectpartpatterns = [
|
||||
# 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
|
||||
url(r'^\?*[^/]*/?$', views.ProjectPartsList.as_view()),
|
||||
]
|
||||
|
||||
projectcategorypatterns = [
|
||||
# List of top-level project categories
|
||||
url(r'^$', views.ProjectCategoryList.as_view()),
|
||||
|
||||
# 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
|
||||
url(r'^new/$', views.NewProjectCategory.as_view())
|
||||
# List of project categories, with filters
|
||||
url(r'^\?*[^/]*/?$', views.ProjectCategoryList.as_view()),
|
||||
]
|
||||
|
||||
urlpatterns = [
|
||||
|
||||
# Individual project URL
|
||||
url(r'^(?P<pk>[0-9]+)/', include(projectdetailpatterns)),
|
||||
url(r'^(?P<pk>[0-9]+)/?$', include(projectdetailpatterns)),
|
||||
|
||||
# List of all projects
|
||||
url(r'^$', views.ProjectList.as_view()),
|
||||
|
||||
# Project parts
|
||||
url(r'^parts/', include(projectpartpatterns)),
|
||||
url(r'^parts/?', include(projectpartpatterns)),
|
||||
|
||||
# Project categories
|
||||
url(r'^category/', include(projectcategorypatterns)),
|
||||
url(r'^category/?', include(projectcategorypatterns)),
|
||||
]
|
||||
|
@ -1,5 +1,6 @@
|
||||
from rest_framework import generics, permissions
|
||||
|
||||
from InvenTree.models import FilterChildren
|
||||
from .models import ProjectCategory, Project, ProjectPart
|
||||
from .serializers import ProjectSerializer
|
||||
from .serializers import ProjectCategoryDetailSerializer
|
||||
@ -16,21 +17,24 @@ class ProjectDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
|
||||
|
||||
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
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class NewProjectCategory(generics.CreateAPIView):
|
||||
""" Create a new Project Category
|
||||
"""
|
||||
serializer_class = ProjectCategoryDetailSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class ProjectCategoryDetail(generics.RetrieveUpdateAPIView):
|
||||
""" Project details
|
||||
"""
|
||||
@ -41,29 +45,42 @@ class ProjectCategoryDetail(generics.RetrieveUpdateAPIView):
|
||||
|
||||
|
||||
class ProjectCategoryList(generics.ListCreateAPIView):
|
||||
""" Top-level project categories.
|
||||
Projects are considered top-level if they do not have a parent
|
||||
""" List project categories
|
||||
"""
|
||||
|
||||
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
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
|
||||
class ProjectPartsList(generics.ListCreateAPIView):
|
||||
""" List all parts associated with a particular project
|
||||
""" List project parts
|
||||
"""
|
||||
|
||||
serializer_class = ProjectPartSerializer
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
||||
|
||||
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:
|
||||
return ProjectPart.objects.filter(project=project_id)
|
||||
else:
|
||||
return ProjectPart.objects.all()
|
||||
parts = parts.filter(project=project_id)
|
||||
|
||||
part_id = params.get('part', None)
|
||||
if part_id:
|
||||
parts = parts.filter(part=part_id)
|
||||
|
||||
return parts
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
# Ensure project link is set correctly
|
||||
|
Loading…
Reference in New Issue
Block a user