mirror of
https://github.com/inventree/InvenTree.git
synced 2026-07-21 22:23:03 +00:00
Remove 'total_price' annotation
- Can be achieved using in-memory python methods
This commit is contained in:
@@ -80,7 +80,6 @@ class GeneralExtraLineList(SerializerContextMixin, DataExportViewMixin):
|
|||||||
queryset = super().get_queryset(*args, **kwargs)
|
queryset = super().get_queryset(*args, **kwargs)
|
||||||
|
|
||||||
queryset = queryset.prefetch_related('order')
|
queryset = queryset.prefetch_related('order')
|
||||||
queryset = self.serializer_class.annotate_queryset(queryset)
|
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
@@ -97,16 +96,6 @@ class GeneralExtraLineList(SerializerContextMixin, DataExportViewMixin):
|
|||||||
filterset_fields = ['order']
|
filterset_fields = ['order']
|
||||||
|
|
||||||
|
|
||||||
class GeneralExtraLineDetail:
|
|
||||||
"""General template for ExtraLine detail API classes."""
|
|
||||||
|
|
||||||
def get_queryset(self, *args, **kwargs):
|
|
||||||
"""Return the annotated queryset for this endpoint."""
|
|
||||||
queryset = super().get_queryset(*args, **kwargs)
|
|
||||||
|
|
||||||
return self.serializer_class.annotate_queryset(queryset)
|
|
||||||
|
|
||||||
|
|
||||||
class OrderCreateMixin:
|
class OrderCreateMixin:
|
||||||
"""Mixin class which handles order creation via API."""
|
"""Mixin class which handles order creation via API."""
|
||||||
|
|
||||||
@@ -755,7 +744,6 @@ class PurchaseOrderLineItemList(
|
|||||||
'reference',
|
'reference',
|
||||||
'SKU',
|
'SKU',
|
||||||
'IPN',
|
'IPN',
|
||||||
'total_price',
|
|
||||||
'target_date',
|
'target_date',
|
||||||
'order',
|
'order',
|
||||||
'status',
|
'status',
|
||||||
@@ -789,7 +777,7 @@ class PurchaseOrderExtraLineList(
|
|||||||
serializer_class = serializers.PurchaseOrderExtraLineSerializer
|
serializer_class = serializers.PurchaseOrderExtraLineSerializer
|
||||||
|
|
||||||
|
|
||||||
class PurchaseOrderExtraLineDetail(GeneralExtraLineDetail, RetrieveUpdateDestroyAPI):
|
class PurchaseOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a PurchaseOrderExtraLine object."""
|
"""API endpoint for detail view of a PurchaseOrderExtraLine object."""
|
||||||
|
|
||||||
queryset = models.PurchaseOrderExtraLine.objects.all()
|
queryset = models.PurchaseOrderExtraLine.objects.all()
|
||||||
@@ -1129,7 +1117,7 @@ class SalesOrderExtraLineList(
|
|||||||
serializer_class = serializers.SalesOrderExtraLineSerializer
|
serializer_class = serializers.SalesOrderExtraLineSerializer
|
||||||
|
|
||||||
|
|
||||||
class SalesOrderExtraLineDetail(GeneralExtraLineDetail, RetrieveUpdateDestroyAPI):
|
class SalesOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a SalesOrderExtraLine object."""
|
"""API endpoint for detail view of a SalesOrderExtraLine object."""
|
||||||
|
|
||||||
queryset = models.SalesOrderExtraLine.objects.all()
|
queryset = models.SalesOrderExtraLine.objects.all()
|
||||||
@@ -1847,7 +1835,7 @@ class ReturnOrderExtraLineList(
|
|||||||
serializer_class = serializers.ReturnOrderExtraLineSerializer
|
serializer_class = serializers.ReturnOrderExtraLineSerializer
|
||||||
|
|
||||||
|
|
||||||
class ReturnOrderExtraLineDetail(GeneralExtraLineDetail, RetrieveUpdateDestroyAPI):
|
class ReturnOrderExtraLineDetail(RetrieveUpdateDestroyAPI):
|
||||||
"""API endpoint for detail view of a ReturnOrderExtraLine object."""
|
"""API endpoint for detail view of a ReturnOrderExtraLine object."""
|
||||||
|
|
||||||
queryset = models.ReturnOrderExtraLine.objects.all()
|
queryset = models.ReturnOrderExtraLine.objects.all()
|
||||||
|
|||||||
@@ -342,19 +342,6 @@ class AbstractExtraLineSerializer(
|
|||||||
*extra_fields,
|
*extra_fields,
|
||||||
]
|
]
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def annotate_queryset(queryset):
|
|
||||||
"""Add annotations to the queryset.
|
|
||||||
|
|
||||||
- total_price: price * quantity, adjusted for the line discount
|
|
||||||
"""
|
|
||||||
return queryset.annotate(
|
|
||||||
total_price=ExpressionWrapper(
|
|
||||||
F('price') * F('quantity') * (1 - F('discount') / Decimal(100)),
|
|
||||||
output_field=models.DecimalField(),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
quantity = serializers.FloatField()
|
quantity = serializers.FloatField()
|
||||||
|
|
||||||
discount = InvenTreeDecimalField(required=False)
|
discount = InvenTreeDecimalField(required=False)
|
||||||
@@ -363,7 +350,9 @@ class AbstractExtraLineSerializer(
|
|||||||
|
|
||||||
price_currency = InvenTreeCurrencySerializer()
|
price_currency = InvenTreeCurrencySerializer()
|
||||||
|
|
||||||
total_price = serializers.FloatField(read_only=True)
|
total_price = InvenTreeMoneySerializer(
|
||||||
|
source='total_line_price', allow_null=True, read_only=True
|
||||||
|
)
|
||||||
|
|
||||||
project_code_label = common.filters.enable_project_label_filter()
|
project_code_label = common.filters.enable_project_label_filter()
|
||||||
|
|
||||||
@@ -610,7 +599,6 @@ class PurchaseOrderLineItemSerializer(
|
|||||||
def annotate_queryset(queryset):
|
def annotate_queryset(queryset):
|
||||||
"""Add some extra annotations to this queryset.
|
"""Add some extra annotations to this queryset.
|
||||||
|
|
||||||
- "total_price" = purchase_price * quantity
|
|
||||||
- "overdue" status (boolean field)
|
- "overdue" status (boolean field)
|
||||||
"""
|
"""
|
||||||
queryset = queryset.prefetch_related(
|
queryset = queryset.prefetch_related(
|
||||||
@@ -626,15 +614,6 @@ class PurchaseOrderLineItemSerializer(
|
|||||||
'part__manufacturer_part__manufacturer',
|
'part__manufacturer_part__manufacturer',
|
||||||
)
|
)
|
||||||
|
|
||||||
queryset = queryset.annotate(
|
|
||||||
total_price=ExpressionWrapper(
|
|
||||||
F('purchase_price')
|
|
||||||
* F('quantity')
|
|
||||||
* (1 - F('discount') / Decimal(100)),
|
|
||||||
output_field=models.DecimalField(),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
queryset = queryset.annotate(
|
queryset = queryset.annotate(
|
||||||
overdue=Case(
|
overdue=Case(
|
||||||
When(
|
When(
|
||||||
@@ -675,7 +654,9 @@ class PurchaseOrderLineItemSerializer(
|
|||||||
|
|
||||||
overdue = serializers.BooleanField(read_only=True, allow_null=True)
|
overdue = serializers.BooleanField(read_only=True, allow_null=True)
|
||||||
|
|
||||||
total_price = serializers.FloatField(read_only=True)
|
total_price = InvenTreeMoneySerializer(
|
||||||
|
source='total_line_price', allow_null=True, read_only=True
|
||||||
|
)
|
||||||
|
|
||||||
part_detail = OptionalField(
|
part_detail = OptionalField(
|
||||||
serializer_class=PartBriefSerializer,
|
serializer_class=PartBriefSerializer,
|
||||||
|
|||||||
@@ -3583,7 +3583,7 @@ class ExtraLineTotalPriceTest(InvenTreeAPITestCase):
|
|||||||
"""Unit tests for the 'total_price' field on ExtraLine API endpoints.
|
"""Unit tests for the 'total_price' field on ExtraLine API endpoints.
|
||||||
|
|
||||||
Covers PurchaseOrderExtraLine, SalesOrderExtraLine and ReturnOrderExtraLine,
|
Covers PurchaseOrderExtraLine, SalesOrderExtraLine and ReturnOrderExtraLine,
|
||||||
which all share the same 'total_price' annotation via AbstractExtraLineSerializer.
|
which all share the same 'total_price' field via AbstractExtraLineSerializer.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
fixtures = [
|
fixtures = [
|
||||||
|
|||||||
Reference in New Issue
Block a user