From 966fb170ec5e91a7bda3a620f6a0b1d3afa0b2da 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_class and subj_class filter to triple filterset Closes: #668 --- apis_core/apis_relations/filtersets.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apis_core/apis_relations/filtersets.py b/apis_core/apis_relations/filtersets.py index 853cbba9f..ffc544190 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,19 @@ class Meta: class TripleFilterSet(GenericFilterSet): subj = CharFilter(method="subj_icontains") obj = CharFilter(method="obj_icontains") + subj_class = ModelMultipleChoiceFilter(label="Subj class", queryset=ContentType.objects.all(), method="class_in") + obj_class = ModelMultipleChoiceFilter(label="Obj class", queryset=ContentType.objects.all(), 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