|
1 | 1 | import uuid |
2 | | -from typing import List, Optional |
| 2 | +from typing import Any, List, Optional |
3 | 3 |
|
4 | 4 | import strawberry |
5 | 5 | import strawberry_django |
6 | 6 | from strawberry import auto |
7 | 7 | from strawberry.types import Info |
8 | 8 | from strawberry_django.mutations import mutations |
| 9 | +from strawberry_django.pagination import OffsetPaginationInput |
9 | 10 |
|
10 | 11 | from api.models import Sector |
11 | | -from api.types.type_sector import TypeSector |
| 12 | +from api.types.type_sector import SectorFilter, SectorOrder, TypeSector |
12 | 13 | from api.utils.enums import DatasetStatus |
13 | 14 |
|
14 | 15 |
|
@@ -41,11 +42,38 @@ def sector(self, info: Info, id: uuid.UUID) -> Optional[TypeSector]: |
41 | 42 | except Sector.DoesNotExist: |
42 | 43 | raise ValueError(f"Sector with ID {id} does not exist.") |
43 | 44 |
|
44 | | - @strawberry_django.field |
45 | | - def active_sectors(self, info: Info) -> list[TypeSector]: |
| 45 | + @strawberry_django.field( |
| 46 | + filters=SectorFilter, |
| 47 | + pagination=True, |
| 48 | + order=SectorOrder, |
| 49 | + ) |
| 50 | + def active_sectors( |
| 51 | + self, |
| 52 | + info: Info, |
| 53 | + filters: Optional[SectorFilter] = strawberry.UNSET, |
| 54 | + pagination: Optional[OffsetPaginationInput] = strawberry.UNSET, |
| 55 | + order: Optional[SectorOrder] = strawberry.UNSET, |
| 56 | + ) -> list[TypeSector]: |
46 | 57 | """Get sectors with published datasets.""" |
47 | | - queryset = Sector.objects.filter(datasets__status=DatasetStatus.PUBLISHED) |
48 | | - return TypeSector.from_django_list(queryset) |
| 58 | + # Start with base queryset filtering for active sectors |
| 59 | + queryset = Sector.objects.filter( |
| 60 | + datasets__status=DatasetStatus.PUBLISHED |
| 61 | + ).distinct() |
| 62 | + |
| 63 | + # Apply filters if provided |
| 64 | + if filters is not strawberry.UNSET: |
| 65 | + queryset = strawberry_django.filters.apply(filters, queryset, info) |
| 66 | + |
| 67 | + # Apply ordering if provided |
| 68 | + if order is not strawberry.UNSET: |
| 69 | + queryset = strawberry_django.ordering.apply(order, queryset, info) |
| 70 | + |
| 71 | + # Apply pagination if provided |
| 72 | + if pagination is not strawberry.UNSET: |
| 73 | + # Apply pagination to the list |
| 74 | + queryset = strawberry_django.pagination.apply(pagination, queryset) |
| 75 | + |
| 76 | + return [TypeSector.from_django(instance) for instance in queryset] |
49 | 77 |
|
50 | 78 |
|
51 | 79 | @strawberry.type |
|
0 commit comments