From 585758c93134f5c98c522b0300b06e6c5932c7a4 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Wed, 28 Feb 2024 20:53:48 +0100 Subject: [PATCH] feat(apis_relations): add obj and subj class filter to triple filters Closes: #668 --- apis_core/apis_relations/filtersets.py | 27 +++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/apis_core/apis_relations/filtersets.py b/apis_core/apis_relations/filtersets.py index 853cbba9f..ec0568704 100644 --- a/apis_core/apis_relations/filtersets.py +++ b/apis_core/apis_relations/filtersets.py @@ -1,5 +1,8 @@ from apis_core.generic.filtersets import GenericFilterSet, GenericFilterSetForm -from django_filters import CharFilter +from django_filters import CharFilter, ModelMultipleChoiceFilter +from django.contrib.contenttypes.models import ContentType +from apis_core.apis_relations.models import Property +from django.db.models import Q PROPERTY_EXCLUDES = ["self_contenttype", "name", "property_class_uri", "rootobject_ptr"] @@ -20,9 +23,31 @@ class Meta: class TripleFilterSet(GenericFilterSet): subj = CharFilter(method="subj_icontains") obj = CharFilter(method="obj_icontains") + subj_class = ModelMultipleChoiceFilter( + label="Subj class", + queryset=ContentType.objects.filter( + pk__in=Property.objects.all().values("subj_class") + ), + method="class_in", + ) + obj_class = ModelMultipleChoiceFilter( + label="Obj class", + queryset=ContentType.objects.filter( + pk__in=Property.objects.all().values("obj_class") + ), + method="class_in", + ) def subj_icontains(self, queryset, name, value): return queryset.filter(subj__name__icontains=value) def obj_icontains(self, queryset, name, value): return queryset.filter(obj__name__icontains=value) + + def class_in(self, queryset, name, value): + # value is the list of contenttypes + if value: + q = Q(**{f"{name}__in": value}) + properties = Property.objects.filter(q) + return queryset.filter(prop__in=properties) + return queryset