make more generalised

This commit is contained in:
Matthias 2022-03-11 00:08:39 +01:00
parent df418c503e
commit c30e8d9b4e
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076

View File

@ -27,6 +27,58 @@ from part.models import Part
from users.models import Owner
class GeneralAdditionalLineItemList:
"""
General template for AdditionalLineItem API classes
"""
def get_serializer(self, *args, **kwargs):
try:
params = self.request.query_params
kwargs['order_detail'] = str2bool(params.get('order_detail', False))
except AttributeError:
pass
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
def get_queryset(self, *args, **kwargs):
queryset = super().get_queryset(*args, **kwargs)
queryset = queryset.prefetch_related(
'order',
)
return queryset
filter_backends = [
rest_filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
]
ordering_fields = [
'title',
'quantity',
'note',
'reference',
]
search_fields = [
'title',
'quantity',
'note',
'reference'
]
filter_fields = [
'order',
]
class POFilter(rest_filters.FilterSet):
"""
Custom API filters for the POList endpoint
@ -743,7 +795,7 @@ class SOLineItemList(generics.ListCreateAPIView):
]
class SOAdditionalLineItemList(generics.ListCreateAPIView):
class SOAdditionalLineItemList(GeneralAdditionalLineItemList, generics.ListCreateAPIView):
"""
API endpoint for accessing a list of SalesOrderAdditionalLineItem objects.
"""
@ -751,52 +803,6 @@ class SOAdditionalLineItemList(generics.ListCreateAPIView):
queryset = models.SalesOrderAdditionalLineItem.objects.all()
serializer_class = serializers.SOAdditionalLineItemSerializer
def get_serializer(self, *args, **kwargs):
try:
params = self.request.query_params
kwargs['order_detail'] = str2bool(params.get('order_detail', False))
except AttributeError:
pass
kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs)
def get_queryset(self, *args, **kwargs):
queryset = super().get_queryset(*args, **kwargs)
queryset = queryset.prefetch_related(
'order',
)
return queryset
filter_backends = [
rest_filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
]
ordering_fields = [
'title',
'quantity',
'note',
'reference',
]
search_fields = [
'title',
'quantity',
'note',
'reference'
]
filter_fields = [
'order',
]
class SOAdditionalLineItemDetail(generics.RetrieveUpdateDestroyAPIView):
""" API endpoint for detail view of a SalesOrderAdditionalLineItem object """