Skip to content

Commit

Permalink
feat(apis_relations): add obj and subj class filter to triple filters
Browse files Browse the repository at this point in the history
Closes: #668
  • Loading branch information
b1rger committed Mar 1, 2024
1 parent 5672ea9 commit 729209a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion apis_core/apis_relations/filtersets.py
Original file line number Diff line number Diff line change
@@ -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"]

Expand All @@ -20,9 +23,30 @@ 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 and name:
name, _ = name.split("_")
return queryset.filter(Q(**{f"{name}__self_contenttype__in": value}))
return queryset

0 comments on commit 729209a

Please sign in to comment.