2019-04-27 12:18:07 +00:00
|
|
|
"""
|
|
|
|
Provides a JSON API for the Part app
|
|
|
|
"""
|
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-23 11:18:35 +00:00
|
|
|
from django_filters.rest_framework import DjangoFilterBackend
|
2018-04-23 11:10:13 +00:00
|
|
|
from rest_framework import filters
|
2019-04-13 23:25:46 +00:00
|
|
|
from rest_framework import generics, permissions
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2019-04-14 01:59:07 +00:00
|
|
|
from django.db.models import Q
|
2018-05-04 13:54:57 +00:00
|
|
|
from django.conf.urls import url, include
|
2019-04-14 01:59:07 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-05-02 13:42:57 +00:00
|
|
|
from .models import Part, PartCategory, BomItem
|
2019-04-16 14:35:49 +00:00
|
|
|
from .models import SupplierPart, SupplierPriceBreak
|
2018-05-02 14:47:03 +00:00
|
|
|
|
2018-05-02 13:42:57 +00:00
|
|
|
from .serializers import PartSerializer, BomItemSerializer
|
2019-04-16 14:35:49 +00:00
|
|
|
from .serializers import SupplierPartSerializer, SupplierPriceBreakSerializer
|
2018-05-04 13:54:57 +00:00
|
|
|
from .serializers import CategorySerializer
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-28 13:22:12 +00:00
|
|
|
from InvenTree.views import TreeSerializer
|
|
|
|
|
2019-04-13 23:25:46 +00:00
|
|
|
|
2018-04-28 13:22:12 +00:00
|
|
|
class PartCategoryTree(TreeSerializer):
|
|
|
|
|
|
|
|
title = "Parts"
|
|
|
|
model = PartCategory
|
|
|
|
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-05-04 13:54:57 +00:00
|
|
|
class CategoryList(generics.ListCreateAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for accessing a list of PartCategory objects.
|
|
|
|
|
|
|
|
- GET: Return a list of PartCategory objects
|
|
|
|
- POST: Create a new PartCategory object
|
|
|
|
"""
|
|
|
|
|
2018-05-04 13:54:57 +00:00
|
|
|
queryset = PartCategory.objects.all()
|
|
|
|
serializer_class = CategorySerializer
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
2019-04-13 23:25:46 +00:00
|
|
|
# filters.SearchFilter,
|
2018-05-04 13:54:57 +00:00
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
'parent',
|
|
|
|
]
|
|
|
|
|
|
|
|
ordering_fields = [
|
|
|
|
'name',
|
|
|
|
]
|
|
|
|
|
|
|
|
ordering = 'name'
|
|
|
|
|
|
|
|
search_fields = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-26 15:03:11 +00:00
|
|
|
class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for detail view of a single PartCategory object """
|
2019-04-26 15:03:11 +00:00
|
|
|
serializer_class = CategorySerializer
|
|
|
|
queryset = PartCategory.objects.all()
|
|
|
|
|
|
|
|
|
2019-04-25 14:17:02 +00:00
|
|
|
class PartDetail(generics.RetrieveUpdateDestroyAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for detail view of a single Part object """
|
2018-05-04 14:51:17 +00:00
|
|
|
queryset = Part.objects.all()
|
|
|
|
serializer_class = PartSerializer
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
|
|
|
|
2019-04-13 23:25:46 +00:00
|
|
|
|
2018-04-14 04:11:46 +00:00
|
|
|
class PartList(generics.ListCreateAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for accessing a list of Part objects
|
|
|
|
|
|
|
|
- GET: Return list of objects
|
|
|
|
- POST: Create a new Part object
|
|
|
|
"""
|
2018-04-14 04:11:46 +00:00
|
|
|
|
|
|
|
serializer_class = PartSerializer
|
2018-04-15 15:02:17 +00:00
|
|
|
|
2019-04-14 01:59:07 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
|
|
|
|
# Does the user wish to filter by category?
|
|
|
|
cat_id = self.request.query_params.get('category', None)
|
|
|
|
|
2019-04-15 12:39:28 +00:00
|
|
|
# Start with all objects
|
|
|
|
parts_list = Part.objects.all()
|
|
|
|
|
2019-04-14 01:59:07 +00:00
|
|
|
if cat_id:
|
|
|
|
category = get_object_or_404(PartCategory, pk=cat_id)
|
|
|
|
|
|
|
|
# Filter by the supplied category
|
|
|
|
flt = Q(category=cat_id)
|
|
|
|
|
|
|
|
if self.request.query_params.get('include_child_categories', None):
|
|
|
|
childs = category.getUniqueChildren()
|
|
|
|
for child in childs:
|
2019-04-14 02:30:06 +00:00
|
|
|
# Ignore the top-level category (already filtered)
|
2019-04-17 22:33:12 +00:00
|
|
|
if str(child) == str(cat_id):
|
2019-04-14 02:30:06 +00:00
|
|
|
continue
|
2019-04-14 01:59:07 +00:00
|
|
|
flt |= Q(category=child)
|
|
|
|
|
2019-04-15 12:39:28 +00:00
|
|
|
parts_list = parts_list.filter(flt)
|
2019-04-14 02:30:06 +00:00
|
|
|
|
2019-04-15 12:39:28 +00:00
|
|
|
return parts_list
|
2019-04-14 01:59:07 +00:00
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
2018-04-14 04:19:03 +00:00
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
2018-04-14 04:19:03 +00:00
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
filter_fields = [
|
2019-04-15 14:01:15 +00:00
|
|
|
'buildable',
|
|
|
|
'consumable',
|
|
|
|
'trackable',
|
|
|
|
'purchaseable',
|
|
|
|
'salable',
|
2018-04-23 11:10:13 +00:00
|
|
|
]
|
2018-04-14 04:19:03 +00:00
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
ordering_fields = [
|
|
|
|
'name',
|
|
|
|
]
|
2018-04-14 04:19:03 +00:00
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
ordering = 'name'
|
2018-04-14 04:19:03 +00:00
|
|
|
|
2018-04-23 11:18:35 +00:00
|
|
|
search_fields = [
|
2019-04-16 22:19:40 +00:00
|
|
|
'$name',
|
2018-04-23 11:18:35 +00:00
|
|
|
'description',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-25 14:29:53 +00:00
|
|
|
class BomList(generics.ListCreateAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoing for accessing a list of BomItem objects
|
|
|
|
|
|
|
|
- GET: Return list of BomItem objects
|
|
|
|
- POST: Create a new BomItem object
|
|
|
|
"""
|
2018-05-02 13:42:57 +00:00
|
|
|
|
|
|
|
queryset = BomItem.objects.all()
|
|
|
|
serializer_class = BomItemSerializer
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
'part',
|
|
|
|
'sub_part'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-25 14:29:53 +00:00
|
|
|
class BomDetail(generics.RetrieveUpdateDestroyAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for detail view of a single BomItem object """
|
2019-04-25 14:29:53 +00:00
|
|
|
|
|
|
|
queryset = BomItem.objects.all()
|
|
|
|
serializer_class = BomItemSerializer
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-25 14:06:35 +00:00
|
|
|
class SupplierPartList(generics.ListCreateAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for list view of SupplierPart object
|
|
|
|
|
|
|
|
- GET: Return list of SupplierPart objects
|
|
|
|
- POST: Create a new SupplierPart object
|
|
|
|
"""
|
2018-05-02 14:47:03 +00:00
|
|
|
|
|
|
|
queryset = SupplierPart.objects.all()
|
|
|
|
serializer_class = SupplierPartSerializer
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
'part',
|
|
|
|
'supplier'
|
|
|
|
]
|
|
|
|
|
2018-05-04 13:54:57 +00:00
|
|
|
|
2019-04-25 13:56:03 +00:00
|
|
|
class SupplierPartDetail(generics.RetrieveUpdateDestroyAPIView):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" API endpoint for detail view of SupplierPart object
|
|
|
|
|
|
|
|
- GET: Retrieve detail view
|
|
|
|
- PATCH: Update object
|
|
|
|
- DELETE: Delete objec
|
|
|
|
"""
|
2019-04-25 13:56:03 +00:00
|
|
|
|
|
|
|
queryset = SupplierPart.objects.all()
|
|
|
|
serializer_class = SupplierPartSerializer
|
|
|
|
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-16 14:35:49 +00:00
|
|
|
class SupplierPriceBreakList(generics.ListCreateAPIView):
|
2019-04-27 15:09:48 +00:00
|
|
|
""" API endpoint for list view of SupplierPriceBreak object
|
2019-04-27 12:18:07 +00:00
|
|
|
|
|
|
|
- GET: Retrieve list of SupplierPriceBreak objects
|
|
|
|
- POST: Create a new SupplierPriceBreak object
|
|
|
|
"""
|
2019-04-16 14:35:49 +00:00
|
|
|
|
|
|
|
queryset = SupplierPriceBreak.objects.all()
|
|
|
|
serializer_class = SupplierPriceBreakSerializer
|
|
|
|
|
|
|
|
permission_classes = [
|
|
|
|
permissions.IsAuthenticatedOrReadOnly,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
'part',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-13 23:25:46 +00:00
|
|
|
cat_api_urls = [
|
2019-04-26 15:03:11 +00:00
|
|
|
|
|
|
|
url(r'^(?P<pk>\d+)/?', CategoryDetail.as_view(), name='api-part-category-detail'),
|
|
|
|
|
2018-05-04 13:54:57 +00:00
|
|
|
url(r'^$', CategoryList.as_view(), name='api-part-category-list'),
|
|
|
|
]
|
2018-05-02 14:47:03 +00:00
|
|
|
|
2019-04-25 13:56:03 +00:00
|
|
|
supplier_part_api_urls = [
|
|
|
|
|
|
|
|
url(r'^(?P<pk>\d+)/?', SupplierPartDetail.as_view(), name='api-supplier-part-detail'),
|
|
|
|
|
|
|
|
# Catch anything else
|
|
|
|
url(r'^.*$', SupplierPartList.as_view(), name='api-part-supplier-list'),
|
|
|
|
]
|
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
part_api_urls = [
|
2018-04-28 13:22:12 +00:00
|
|
|
url(r'^tree/?', PartCategoryTree.as_view(), name='api-part-tree'),
|
|
|
|
|
2018-05-04 13:54:57 +00:00
|
|
|
url(r'^category/', include(cat_api_urls)),
|
2019-04-26 12:36:44 +00:00
|
|
|
url(r'^supplier/', include(supplier_part_api_urls)),
|
2018-05-04 13:54:57 +00:00
|
|
|
|
2019-04-16 14:35:49 +00:00
|
|
|
url(r'^price-break/?', SupplierPriceBreakList.as_view(), name='api-part-supplier-price'),
|
2018-05-04 14:51:17 +00:00
|
|
|
|
|
|
|
url(r'^(?P<pk>\d+)/', PartDetail.as_view(), name='api-part-detail'),
|
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
url(r'^.*$', PartList.as_view(), name='api-part-list'),
|
|
|
|
]
|
2019-04-25 14:29:53 +00:00
|
|
|
|
|
|
|
bom_api_urls = [
|
|
|
|
# BOM Item Detail
|
2019-04-27 04:50:49 +00:00
|
|
|
url('^(?P<pk>\d+)/', BomDetail.as_view(), name='api-bom-detail'),
|
2019-04-25 14:29:53 +00:00
|
|
|
|
|
|
|
# Catch-all
|
|
|
|
url(r'^.*$', BomList.as_view(), name='api-bom-list'),
|
2019-04-26 13:34:15 +00:00
|
|
|
]
|