2019-04-27 10:43:27 +00:00
|
|
|
"""
|
|
|
|
Provides a JSON API for the Company app
|
|
|
|
"""
|
|
|
|
|
2018-04-23 11:10:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2018-04-23 11:18:35 +00:00
|
|
|
from django_filters.rest_framework import DjangoFilterBackend
|
2018-04-23 09:36:46 +00:00
|
|
|
from rest_framework import filters
|
2020-10-05 13:36:55 +00:00
|
|
|
from rest_framework import generics
|
2018-04-23 11:10:13 +00:00
|
|
|
|
2019-05-18 10:24:09 +00:00
|
|
|
from django.conf.urls import url, include
|
2020-04-13 09:16:57 +00:00
|
|
|
from django.db.models import Q
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2019-05-23 12:44:37 +00:00
|
|
|
from InvenTree.helpers import str2bool
|
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
from .models import Company
|
2021-06-20 14:28:28 +00:00
|
|
|
from .models import ManufacturerPart, ManufacturerPartParameter
|
2019-05-18 08:04:25 +00:00
|
|
|
from .models import SupplierPart, SupplierPriceBreak
|
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
from .serializers import CompanySerializer
|
2021-06-20 14:28:28 +00:00
|
|
|
from .serializers import ManufacturerPartSerializer, ManufacturerPartParameterSerializer
|
2019-05-18 08:04:25 +00:00
|
|
|
from .serializers import SupplierPartSerializer, SupplierPriceBreakSerializer
|
2018-04-14 04:11:46 +00:00
|
|
|
|
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
class CompanyList(generics.ListCreateAPIView):
|
2019-04-27 10:43:27 +00:00
|
|
|
""" API endpoint for accessing a list of Company objects
|
|
|
|
|
|
|
|
Provides two methods:
|
|
|
|
|
|
|
|
- GET: Return list of objects
|
|
|
|
- POST: Create a new Company object
|
|
|
|
"""
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
serializer_class = CompanySerializer
|
|
|
|
queryset = Company.objects.all()
|
2020-05-02 05:23:28 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = CompanySerializer.annotate_queryset(queryset)
|
|
|
|
|
|
|
|
return queryset
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
2018-04-23 10:37:36 +00:00
|
|
|
filters.OrderingFilter,
|
2018-04-23 09:36:46 +00:00
|
|
|
]
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
filter_fields = [
|
2018-04-23 11:10:13 +00:00
|
|
|
'is_customer',
|
2020-04-13 02:16:42 +00:00
|
|
|
'is_manufacturer',
|
2018-04-23 11:10:13 +00:00
|
|
|
'is_supplier',
|
2020-04-13 02:16:42 +00:00
|
|
|
'name',
|
2018-04-23 09:36:46 +00:00
|
|
|
]
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
search_fields = [
|
2018-04-23 11:10:13 +00:00
|
|
|
'name',
|
|
|
|
'description',
|
2021-03-02 21:36:16 +00:00
|
|
|
'website',
|
2018-04-23 09:36:46 +00:00
|
|
|
]
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
ordering_fields = [
|
|
|
|
'name',
|
2021-03-02 21:36:16 +00:00
|
|
|
'parts_supplied',
|
|
|
|
'parts_manufactured',
|
2018-04-23 09:36:46 +00:00
|
|
|
]
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
ordering = 'name'
|
2018-04-14 04:11:46 +00:00
|
|
|
|
|
|
|
|
2019-04-25 14:10:34 +00:00
|
|
|
class CompanyDetail(generics.RetrieveUpdateDestroyAPIView):
|
2019-04-27 10:43:27 +00:00
|
|
|
""" API endpoint for detail of a single Company object """
|
2019-04-25 14:10:34 +00:00
|
|
|
|
|
|
|
queryset = Company.objects.all()
|
|
|
|
serializer_class = CompanySerializer
|
2020-05-02 05:36:48 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = CompanySerializer.annotate_queryset(queryset)
|
|
|
|
|
|
|
|
return queryset
|
2021-03-24 15:44:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManufacturerPartList(generics.ListCreateAPIView):
|
|
|
|
""" API endpoint for list view of ManufacturerPart object
|
|
|
|
|
|
|
|
- GET: Return list of ManufacturerPart objects
|
|
|
|
- POST: Create a new ManufacturerPart object
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = ManufacturerPart.objects.all().prefetch_related(
|
|
|
|
'part',
|
|
|
|
'manufacturer',
|
2021-04-02 15:13:57 +00:00
|
|
|
'supplier_parts',
|
2021-03-24 15:44:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
serializer_class = ManufacturerPartSerializer
|
|
|
|
|
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# Do we wish to include extra detail?
|
|
|
|
try:
|
2021-06-18 09:39:06 +00:00
|
|
|
params = self.request.query_params
|
2021-03-24 15:44:51 +00:00
|
|
|
|
2021-06-18 09:39:06 +00:00
|
|
|
kwargs['part_detail'] = str2bool(params.get('part_detail', None))
|
|
|
|
kwargs['manufacturer_detail'] = str2bool(params.get('manufacturer_detail', None))
|
|
|
|
kwargs['pretty'] = str2bool(params.get('pretty', None))
|
2021-03-24 15:44:51 +00:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-03-24 15:44:51 +00:00
|
|
|
kwargs['context'] = self.get_serializer_context()
|
|
|
|
|
|
|
|
return self.serializer_class(*args, **kwargs)
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
"""
|
|
|
|
Custom filtering for the queryset.
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
|
|
|
|
params = self.request.query_params
|
|
|
|
|
|
|
|
# Filter by manufacturer
|
2021-04-30 17:54:56 +00:00
|
|
|
manufacturer = params.get('manufacturer', None)
|
2021-03-24 15:44:51 +00:00
|
|
|
|
|
|
|
if manufacturer is not None:
|
|
|
|
queryset = queryset.filter(manufacturer=manufacturer)
|
|
|
|
|
|
|
|
# Filter by parent part?
|
|
|
|
part = params.get('part', None)
|
|
|
|
|
|
|
|
if part is not None:
|
|
|
|
queryset = queryset.filter(part=part)
|
|
|
|
|
|
|
|
# Filter by 'active' status of the part?
|
|
|
|
active = params.get('active', None)
|
|
|
|
|
|
|
|
if active is not None:
|
|
|
|
active = str2bool(active)
|
|
|
|
queryset = queryset.filter(part__active=active)
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
]
|
|
|
|
|
|
|
|
search_fields = [
|
|
|
|
'manufacturer__name',
|
|
|
|
'description',
|
|
|
|
'MPN',
|
2021-07-08 01:16:04 +00:00
|
|
|
'part__IPN',
|
2021-03-24 15:44:51 +00:00
|
|
|
'part__name',
|
|
|
|
'part__description',
|
|
|
|
]
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2019-04-25 14:10:34 +00:00
|
|
|
|
2021-03-24 15:44:51 +00:00
|
|
|
class ManufacturerPartDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
""" API endpoint for detail view of ManufacturerPart object
|
|
|
|
|
|
|
|
- GET: Retrieve detail view
|
|
|
|
- PATCH: Update object
|
|
|
|
- DELETE: Delete object
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = ManufacturerPart.objects.all()
|
|
|
|
serializer_class = ManufacturerPartSerializer
|
|
|
|
|
|
|
|
|
2021-06-20 14:28:28 +00:00
|
|
|
class ManufacturerPartParameterList(generics.ListCreateAPIView):
|
|
|
|
"""
|
|
|
|
API endpoint for list view of ManufacturerPartParamater model.
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = ManufacturerPartParameter.objects.all()
|
|
|
|
serializer_class = ManufacturerPartParameterSerializer
|
|
|
|
|
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# Do we wish to include any extra detail?
|
|
|
|
try:
|
|
|
|
params = self.request.query_params
|
|
|
|
|
|
|
|
optional_fields = [
|
|
|
|
'manufacturer_part_detail',
|
|
|
|
]
|
|
|
|
|
|
|
|
for key in optional_fields:
|
|
|
|
kwargs[key] = str2bool(params.get(key, None))
|
|
|
|
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
kwargs['context'] = self.get_serializer_context()
|
|
|
|
|
|
|
|
return self.serializer_class(*args, **kwargs)
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
"""
|
|
|
|
Custom filtering for the queryset
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
|
|
|
|
params = self.request.query_params
|
|
|
|
|
|
|
|
# Filter by manufacturer?
|
|
|
|
manufacturer = params.get('manufacturer', None)
|
|
|
|
|
|
|
|
if manufacturer is not None:
|
|
|
|
queryset = queryset.filter(manufacturer_part__manufacturer=manufacturer)
|
|
|
|
|
|
|
|
# Filter by part?
|
|
|
|
part = params.get('part', None)
|
|
|
|
|
|
|
|
if part is not None:
|
|
|
|
queryset = queryset.filter(manufacturer_part__part=part)
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
'name',
|
|
|
|
'value',
|
|
|
|
'units',
|
|
|
|
'manufacturer_part',
|
|
|
|
]
|
|
|
|
|
|
|
|
search_fields = [
|
|
|
|
'name',
|
|
|
|
'value',
|
|
|
|
'units',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ManufacturerPartParameterDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
"""
|
|
|
|
API endpoint for detail view of ManufacturerPartParameter model
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = ManufacturerPartParameter.objects.all()
|
|
|
|
serializer_class = ManufacturerPartParameterSerializer
|
|
|
|
|
|
|
|
|
2019-05-18 10:24:09 +00:00
|
|
|
class SupplierPartList(generics.ListCreateAPIView):
|
|
|
|
""" API endpoint for list view of SupplierPart object
|
|
|
|
|
|
|
|
- GET: Return list of SupplierPart objects
|
|
|
|
- POST: Create a new SupplierPart object
|
|
|
|
"""
|
|
|
|
|
2021-07-13 16:55:36 +00:00
|
|
|
queryset = SupplierPart.objects.all()
|
2020-04-13 08:50:59 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
|
2020-08-26 12:44:22 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
"""
|
|
|
|
Custom filtering for the queryset.
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
|
|
|
|
params = self.request.query_params
|
|
|
|
|
|
|
|
# Filter by manufacturer
|
|
|
|
manufacturer = params.get('manufacturer', None)
|
|
|
|
|
|
|
|
if manufacturer is not None:
|
2021-03-30 20:48:16 +00:00
|
|
|
queryset = queryset.filter(manufacturer_part__manufacturer=manufacturer)
|
2020-08-26 12:44:22 +00:00
|
|
|
|
|
|
|
# Filter by supplier
|
|
|
|
supplier = params.get('supplier', None)
|
|
|
|
|
|
|
|
if supplier is not None:
|
|
|
|
queryset = queryset.filter(supplier=supplier)
|
|
|
|
|
2020-04-13 08:50:59 +00:00
|
|
|
# Filter by EITHER manufacturer or supplier
|
2020-08-26 12:44:22 +00:00
|
|
|
company = params.get('company', None)
|
2020-04-13 08:50:59 +00:00
|
|
|
|
|
|
|
if company is not None:
|
2021-03-30 20:48:16 +00:00
|
|
|
queryset = queryset.filter(Q(manufacturer_part__manufacturer=company) | Q(supplier=company))
|
2019-05-20 22:19:16 +00:00
|
|
|
|
2020-08-26 12:44:22 +00:00
|
|
|
# Filter by parent part?
|
|
|
|
part = params.get('part', None)
|
|
|
|
|
|
|
|
if part is not None:
|
|
|
|
queryset = queryset.filter(part=part)
|
|
|
|
|
2021-03-30 22:08:33 +00:00
|
|
|
# Filter by manufacturer part?
|
|
|
|
manufacturer_part = params.get('manufacturer_part', None)
|
|
|
|
|
|
|
|
if manufacturer_part is not None:
|
|
|
|
queryset = queryset.filter(manufacturer_part=manufacturer_part)
|
|
|
|
|
2020-10-29 01:50:17 +00:00
|
|
|
# Filter by 'active' status of the part?
|
|
|
|
active = params.get('active', None)
|
|
|
|
|
|
|
|
if active is not None:
|
|
|
|
active = str2bool(active)
|
|
|
|
queryset = queryset.filter(part__active=active)
|
|
|
|
|
2020-04-13 09:16:57 +00:00
|
|
|
return queryset
|
|
|
|
|
2019-05-23 12:44:37 +00:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# Do we wish to include extra detail?
|
2020-04-13 10:36:51 +00:00
|
|
|
try:
|
2021-06-18 09:39:06 +00:00
|
|
|
params = self.request.query_params
|
|
|
|
kwargs['part_detail'] = str2bool(params.get('part_detail', None))
|
|
|
|
kwargs['supplier_detail'] = str2bool(params.get('supplier_detail', None))
|
2021-06-20 07:39:00 +00:00
|
|
|
kwargs['manufacturer_detail'] = str2bool(params.get('manufacturer_detail', None))
|
2021-06-18 09:39:06 +00:00
|
|
|
kwargs['pretty'] = str2bool(params.get('pretty', None))
|
2020-04-13 10:36:51 +00:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2019-05-23 12:44:37 +00:00
|
|
|
kwargs['context'] = self.get_serializer_context()
|
|
|
|
|
|
|
|
return self.serializer_class(*args, **kwargs)
|
|
|
|
|
2019-05-18 10:24:09 +00:00
|
|
|
serializer_class = SupplierPartSerializer
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
filters.SearchFilter,
|
|
|
|
filters.OrderingFilter,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
]
|
|
|
|
|
2019-05-24 11:33:45 +00:00
|
|
|
search_fields = [
|
|
|
|
'SKU',
|
|
|
|
'supplier__name',
|
2021-04-01 14:00:15 +00:00
|
|
|
'manufacturer_part__manufacturer__name',
|
2019-05-24 11:33:45 +00:00
|
|
|
'description',
|
2021-04-01 14:00:15 +00:00
|
|
|
'manufacturer_part__MPN',
|
2021-07-08 01:16:04 +00:00
|
|
|
'part__IPN',
|
2021-02-21 10:06:44 +00:00
|
|
|
'part__name',
|
|
|
|
'part__description',
|
2019-05-24 11:33:45 +00:00
|
|
|
]
|
|
|
|
|
2019-05-18 10:24:09 +00:00
|
|
|
|
|
|
|
class SupplierPartDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
""" API endpoint for detail view of SupplierPart object
|
|
|
|
|
|
|
|
- GET: Retrieve detail view
|
|
|
|
- PATCH: Update object
|
2021-03-30 20:48:16 +00:00
|
|
|
- DELETE: Delete object
|
2019-05-18 10:24:09 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = SupplierPart.objects.all()
|
|
|
|
serializer_class = SupplierPartSerializer
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SupplierPriceBreakList(generics.ListCreateAPIView):
|
|
|
|
""" API endpoint for list view of SupplierPriceBreak object
|
|
|
|
|
|
|
|
- GET: Retrieve list of SupplierPriceBreak objects
|
|
|
|
- POST: Create a new SupplierPriceBreak object
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = SupplierPriceBreak.objects.all()
|
|
|
|
serializer_class = SupplierPriceBreakSerializer
|
|
|
|
|
|
|
|
filter_backends = [
|
|
|
|
DjangoFilterBackend,
|
|
|
|
]
|
|
|
|
|
|
|
|
filter_fields = [
|
|
|
|
'part',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-06-30 04:14:31 +00:00
|
|
|
class SupplierPriceBreakDetail(generics.RetrieveUpdateDestroyAPIView):
|
|
|
|
"""
|
|
|
|
Detail endpoint for SupplierPriceBreak object
|
|
|
|
"""
|
|
|
|
|
|
|
|
queryset = SupplierPriceBreak.objects.all()
|
|
|
|
serializer_class = SupplierPriceBreakSerializer
|
|
|
|
|
|
|
|
|
2021-03-24 15:44:51 +00:00
|
|
|
manufacturer_part_api_urls = [
|
|
|
|
|
2021-06-20 14:28:28 +00:00
|
|
|
url(r'^parameter/', include([
|
|
|
|
url(r'^(?P<pk>\d+)/', ManufacturerPartParameterDetail.as_view(), name='api-manufacturer-part-parameter-detail'),
|
|
|
|
|
|
|
|
# Catch anything else
|
|
|
|
url(r'^.*$', ManufacturerPartParameterList.as_view(), name='api-manufacturer-part-parameter-list'),
|
|
|
|
])),
|
|
|
|
|
2021-03-24 15:44:51 +00:00
|
|
|
url(r'^(?P<pk>\d+)/?', ManufacturerPartDetail.as_view(), name='api-manufacturer-part-detail'),
|
|
|
|
|
|
|
|
# Catch anything else
|
|
|
|
url(r'^.*$', ManufacturerPartList.as_view(), name='api-manufacturer-part-list'),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-05-18 10:24:09 +00:00
|
|
|
supplier_part_api_urls = [
|
|
|
|
|
|
|
|
url(r'^(?P<pk>\d+)/?', SupplierPartDetail.as_view(), name='api-supplier-part-detail'),
|
|
|
|
|
|
|
|
# Catch anything else
|
2020-04-13 09:16:57 +00:00
|
|
|
url(r'^.*$', SupplierPartList.as_view(), name='api-supplier-part-list'),
|
2019-05-18 10:24:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
company_api_urls = [
|
2021-03-24 15:44:51 +00:00
|
|
|
url(r'^part/manufacturer/', include(manufacturer_part_api_urls)),
|
|
|
|
|
2020-04-13 09:16:57 +00:00
|
|
|
url(r'^part/', include(supplier_part_api_urls)),
|
2019-05-18 10:24:09 +00:00
|
|
|
|
2021-06-30 04:14:31 +00:00
|
|
|
# Supplier price breaks
|
|
|
|
url(r'^price-break/', include([
|
|
|
|
|
|
|
|
url(r'^(?P<pk>\d+)/?', SupplierPriceBreakDetail.as_view(), name='api-part-supplier-price-detail'),
|
|
|
|
url(r'^.*$', SupplierPriceBreakList.as_view(), name='api-part-supplier-price-list'),
|
2021-06-30 04:15:18 +00:00
|
|
|
])),
|
2018-04-14 04:11:46 +00:00
|
|
|
|
2019-04-25 14:10:34 +00:00
|
|
|
url(r'^(?P<pk>\d+)/?', CompanyDetail.as_view(), name='api-company-detail'),
|
|
|
|
|
2018-04-23 09:36:46 +00:00
|
|
|
url(r'^.*$', CompanyList.as_view(), name='api-company-list'),
|
2018-04-23 11:18:35 +00:00
|
|
|
]
|