|
1 |
| -from django.db.models import QuerySet |
| 1 | +from django.db.models import Case, QuerySet, Value, When |
| 2 | +from django.utils.translation import gettext_lazy as _ |
2 | 3 |
|
3 | 4 | from django_filters import FilterSet, NumberFilter, OrderingFilter, UUIDFilter
|
4 | 5 |
|
| 6 | +from ..constants import InternalStatus |
5 | 7 | from ..models import (
|
6 | 8 | DestructionList,
|
7 | 9 | DestructionListItem,
|
|
12 | 14 |
|
13 | 15 |
|
14 | 16 | class DestructionListItemFilterset(FilterSet):
|
| 17 | + destruction_list = UUIDFilter( |
| 18 | + field_name="destruction_list", |
| 19 | + method="filter_in_destruction_list", |
| 20 | + help_text=_( |
| 21 | + "Retrieve the items that are in a destruction list and " |
| 22 | + "order them based on processing status." |
| 23 | + ), |
| 24 | + ) |
| 25 | + |
15 | 26 | class Meta:
|
16 | 27 | model = DestructionListItem
|
17 |
| - fields = ("destruction_list",) |
| 28 | + fields = ("destruction_list", "status", "processing_status") |
| 29 | + |
| 30 | + def filter_in_destruction_list( |
| 31 | + self, queryset: QuerySet[DestructionListItem], name: str, value: str |
| 32 | + ) -> QuerySet[DestructionListItem]: |
| 33 | + return ( |
| 34 | + queryset.filter(destruction_list__uuid=value) |
| 35 | + .annotate( |
| 36 | + processing_status_index=Case( |
| 37 | + When(processing_status=InternalStatus.failed, then=Value(1)), |
| 38 | + When(processing_status=InternalStatus.processing, then=Value(2)), |
| 39 | + When(processing_status=InternalStatus.queued, then=Value(3)), |
| 40 | + When(processing_status=InternalStatus.new, then=Value(4)), |
| 41 | + When(processing_status=InternalStatus.succeeded, then=Value(5)), |
| 42 | + default=Value(1), |
| 43 | + ), |
| 44 | + ) |
| 45 | + .order_by("processing_status_index") |
| 46 | + ) |
18 | 47 |
|
19 | 48 |
|
20 | 49 | class DestructionListFilterset(FilterSet):
|
|
0 commit comments