|
| 1 | +from django.db.models import Q |
| 2 | + |
1 | 3 | from django_filters import rest_framework as filters
|
| 4 | +from django_filters.constants import EMPTY_VALUES |
2 | 5 | from vng_api_common.filters import URLModelChoiceFilter
|
3 | 6 | from vng_api_common.filtersets import FilterSet
|
4 | 7 | from vng_api_common.utils import get_help_text
|
|
11 | 14 | )
|
12 | 15 |
|
13 | 16 |
|
| 17 | +class ObjectFilter(filters.BaseCSVFilter): |
| 18 | + """ |
| 19 | + Allow filtering of ENKELVOUDIGINFORMATIEOBJECTen by objects they are related |
| 20 | + to (through the OBJECTINFORMATIEOBJECT resource) |
| 21 | + """ |
| 22 | + |
| 23 | + def filter(self, qs, values): |
| 24 | + if values in EMPTY_VALUES: |
| 25 | + return qs |
| 26 | + |
| 27 | + if self.distinct: |
| 28 | + qs = qs.distinct() |
| 29 | + |
| 30 | + lookups = Q() |
| 31 | + for value in values: |
| 32 | + lookups |= Q(object=value) |
| 33 | + |
| 34 | + oios = ObjectInformatieObject.objects.filter(lookups) |
| 35 | + qs = self.get_method(qs)( |
| 36 | + canonical__id__in=list(oios.values_list("informatieobject__id", flat=True)) |
| 37 | + ) |
| 38 | + return qs |
| 39 | + |
| 40 | + |
14 | 41 | class EnkelvoudigInformatieObjectListFilter(FilterSet):
|
| 42 | + object = ObjectFilter( |
| 43 | + help_text=( |
| 44 | + "De URL van het gerelateerde object " |
| 45 | + "(zoals vastgelegd in de OBJECTINFORMATIEOBJECT resource). " |
| 46 | + "Meerdere waardes kunnen met komma's gescheiden worden." |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
15 | 50 | class Meta:
|
16 | 51 | model = EnkelvoudigInformatieObject
|
17 |
| - fields = ("identificatie", "bronorganisatie") |
| 52 | + fields = ("identificatie", "bronorganisatie", "object") |
18 | 53 |
|
19 | 54 |
|
20 | 55 | class EnkelvoudigInformatieObjectDetailFilter(FilterSet):
|
|
0 commit comments