InvenTree/InvenTree/part/views.py

114 lines
3.3 KiB
Python
Raw Normal View History

2017-04-14 02:14:50 +00:00
# import django_filters
from rest_framework import generics, permissions
from InvenTree.models import FilterChildren
2017-04-14 01:46:18 +00:00
from .models import PartCategory, Part, PartParameter, PartParameterTemplate
2017-04-11 13:33:08 +00:00
from .serializers import PartSerializer
2017-04-14 05:25:10 +00:00
from .serializers import PartCategorySerializer
2017-04-11 13:33:08 +00:00
from .serializers import PartParameterSerializer
2017-04-14 01:46:18 +00:00
from .serializers import PartTemplateSerializer
2017-03-28 12:38:12 +00:00
class PartDetail(generics.RetrieveUpdateDestroyAPIView):
""" Return information on a single part
"""
queryset = Part.objects.all()
2017-04-11 13:33:08 +00:00
serializer_class = PartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
2017-04-11 13:33:08 +00:00
2017-04-14 01:46:18 +00:00
class PartParamList(generics.ListCreateAPIView):
""" Return all parameters associated with a particular part
"""
2017-04-11 13:33:08 +00:00
def get_queryset(self):
2017-04-14 01:46:18 +00:00
part_id = self.request.query_params.get('part', None)
2017-04-11 13:33:08 +00:00
2017-04-14 01:46:18 +00:00
if part_id:
return PartParameter.objects.filter(part=part_id)
else:
return PartParameter.objects.all()
serializer_class = PartParameterSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class PartParamDetail(generics.RetrieveUpdateDestroyAPIView):
""" Detail view of a single PartParameter
"""
queryset = PartParameter.objects.all()
2017-04-11 13:33:08 +00:00
serializer_class = PartParameterSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
2017-03-28 12:31:41 +00:00
2017-04-14 02:14:50 +00:00
"""
class PartFilter(django_filters.rest_framework.FilterSet):
min_stock = django_filters.NumberFilter(name="stock", lookup_expr="gte")
max_stock = django_filters.NumberFilter(name="stock", lookup_expr="lte")
class Meta:
model = Part
fields = ['stock']
"""
class PartList(generics.ListCreateAPIView):
2017-04-14 04:45:17 +00:00
""" List of parts, with optional filters
2017-04-14 02:14:50 +00:00
"""
def get_queryset(self):
parts = Part.objects.all()
2017-04-14 03:29:24 +00:00
params = self.request.query_params
cat_id = params.get('category', None)
2017-04-14 02:14:50 +00:00
if cat_id:
parts = parts.filter(category=cat_id)
return parts
2017-04-11 13:33:08 +00:00
serializer_class = PartSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
2017-03-28 12:31:41 +00:00
2017-04-14 01:46:18 +00:00
class PartCategoryDetail(generics.RetrieveUpdateDestroyAPIView):
2017-04-10 23:41:03 +00:00
""" Return information on a single PartCategory
"""
queryset = PartCategory.objects.all()
2017-04-14 05:25:10 +00:00
serializer_class = PartCategorySerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
2017-03-28 12:31:41 +00:00
class PartCategoryList(generics.ListCreateAPIView):
2017-04-10 23:41:03 +00:00
""" Return a list of all top-level part categories.
Categories are considered "top-level" if they do not have a parent
"""
def get_queryset(self):
params = self.request.query_params
categories = PartCategory.objects.all()
categories = FilterChildren(categories, params.get('parent', None))
return categories
2017-04-10 23:41:03 +00:00
queryset = PartCategory.objects.filter(parent=None)
2017-04-14 05:25:10 +00:00
serializer_class = PartCategorySerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
2017-04-14 01:46:18 +00:00
class PartTemplateDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = PartParameterTemplate.objects.all()
serializer_class = PartTemplateSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
class PartTemplateList(generics.ListCreateAPIView):
queryset = PartParameterTemplate.objects.all()
serializer_class = PartTemplateSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)