Add "overdue" flag to serializer

- Also allow filtering by overdue status in the API
This commit is contained in:
Oliver Walters 2021-01-14 17:33:24 +11:00
parent 834d9ec9a1
commit 4d73aab090
2 changed files with 26 additions and 0 deletions

View File

@ -80,6 +80,17 @@ class POList(generics.ListCreateAPIView):
else: else:
queryset = queryset.exclude(status__in=PurchaseOrderStatus.OPEN) queryset = queryset.exclude(status__in=PurchaseOrderStatus.OPEN)
# Filter by 'overdue' status
overdue = params.get('overdue', None)
if overdue is not None:
overdue = str2bool(overdue)
if overdue:
queryset = queryset.filter(PurchaseOrder.OVERDUE_FILTER)
else:
queryset = queryset.exclude(PurchaseOrder.OVERDUE_FILTER)
# Special filtering for 'status' field # Special filtering for 'status' field
status = params.get('status', None) status = params.get('status', None)

View File

@ -40,12 +40,24 @@ class POSerializer(InvenTreeModelSerializer):
def annotate_queryset(queryset): def annotate_queryset(queryset):
""" """
Add extra information to the queryset Add extra information to the queryset
- Number of liens in the PurchaseOrder
- Overdue status of the PurchaseOrder
""" """
queryset = queryset.annotate( queryset = queryset.annotate(
line_items=SubqueryCount('lines') line_items=SubqueryCount('lines')
) )
queryset = queryset.annotate(
overdue=Case(
When(
PurchaseOrder.OVERDUE_FILTER, then=Value(True, output_field=BooleanField()),
),
default=Value(False, output_field=BooleanField())
)
)
return queryset return queryset
supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True) supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
@ -54,6 +66,8 @@ class POSerializer(InvenTreeModelSerializer):
status_text = serializers.CharField(source='get_status_display', read_only=True) status_text = serializers.CharField(source='get_status_display', read_only=True)
overdue = serializers.BooleanField(required=False, read_only=True)
class Meta: class Meta:
model = PurchaseOrder model = PurchaseOrder
@ -65,6 +79,7 @@ class POSerializer(InvenTreeModelSerializer):
'description', 'description',
'line_items', 'line_items',
'link', 'link',
'overdue',
'reference', 'reference',
'supplier', 'supplier',
'supplier_detail', 'supplier_detail',