Filter builds by "overdue" status

This commit is contained in:
Oliver Walters 2020-12-15 23:27:59 +11:00
parent 802dd5174c
commit 51da26d21d
3 changed files with 15 additions and 6 deletions

View File

@ -73,6 +73,17 @@ class BuildList(generics.ListCreateAPIView):
else:
queryset = queryset.exclude(status__in=BuildStatus.ACTIVE_CODES)
# Filter by "overdue" status?
overdue = params.get('overdue', None)
if overdue is not None:
overdue = str2bool(overdue)
if overdue:
queryset = queryset.filter(Build.OVERDUE_FILTER)
else:
queryset = queryset.exclude(Build.OVERDUE_FILTER)
# Filter by associated part?
part = params.get('part', None)

View File

@ -14,7 +14,7 @@ from django.core.exceptions import ValidationError
from django.urls import reverse
from django.db import models, transaction
from django.db.models import Sum
from django.db.models import Sum, Q
from django.db.models.functions import Coalesce
from django.core.validators import MinValueValidator
@ -53,6 +53,8 @@ class Build(MPTTModel):
notes: Text notes
"""
OVERDUE_FILTER = Q(status__in=BuildStatus.ACTIVE_CODES) & ~Q(target_date=None) & Q(target_date__lte=datetime.now().date())
class Meta:
verbose_name = _("Build Order")
verbose_name_plural = _("Build Orders")

View File

@ -48,14 +48,10 @@ class BuildSerializer(InvenTreeModelSerializer):
# Annotate a boolean 'overdue' flag
# Construct a filter for finding overdue builds
today = datetime.datetime.now().date()
overdue = Q(status__in=BuildStatus.ACTIVE_CODES) & ~Q(target_date=None) & Q(target_date__lte=today)
queryset = queryset.annotate(
overdue=Case(
When(
overdue, then=Value(True, output_field=BooleanField()),
Build.OVERDUE_FILTER, then=Value(True, output_field=BooleanField()),
),
default=Value(False, output_field=BooleanField())
)