diff --git a/InvenTree/build/api.py b/InvenTree/build/api.py index ffc8d97a92..d76c0a4b51 100644 --- a/InvenTree/build/api.py +++ b/InvenTree/build/api.py @@ -90,6 +90,13 @@ class BuildList(generics.ListCreateAPIView): if part is not None: queryset = queryset.filter(part=part) + # Filter by 'date range' + min_date = params.get('min_date', None) + max_date = params.get('max_date', None) + + if min_date is not None and max_date is not None: + queryset = Build.filterByDate(queryset, min_date, max_date) + return queryset def get_serializer(self, *args, **kwargs): diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index c3b399d820..10b3b00259 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -61,6 +61,37 @@ class Build(MPTTModel): verbose_name = _("Build Order") verbose_name_plural = _("Build Orders") + @staticmethod + def filterByDate(queryset, min_date, max_date): + """ + Filter by 'minimum and maximum date range' + + - Specified as min_date, max_date + - Both must be specified for filter to be applied + """ + + date_fmt = '%Y-%m-%d' # ISO format date string + + # Ensure that both dates are valid + try: + min_date = datetime.strptime(str(min_date), date_fmt).date() + max_date = datetime.strptime(str(max_date), date_fmt).date() + except (ValueError, TypeError): + # Date processing error, return queryset unchanged + return queryset + + # Order was completed within the specified range + completed = Q(status=BuildStatus.COMPLETE) & Q(completion_date__gte=min_date) & Q(completion_date__lte=max_date) + + # Order target date falls witin specified range + pending = Q(status__in=BuildStatus.ACTIVE_CODES) & ~Q(target_date=None) & Q(target_date__gte=min_date) & Q(target_date__lte=max_date) + + # TODO - Construct a queryset for "overdue" orders + + queryset = queryset.filter(completed | pending) + + return queryset + def __str__(self): prefix = getSetting("BUILDORDER_REFERENCE_PREFIX") diff --git a/InvenTree/build/templates/build/build_base.html b/InvenTree/build/templates/build/build_base.html index 1124dd16c0..9ca3fff818 100644 --- a/InvenTree/build/templates/build/build_base.html +++ b/InvenTree/build/templates/build/build_base.html @@ -88,11 +88,20 @@ src="{% static 'img/blank_image.png' %}"