Skip to content

Commit

Permalink
feat(apis_relations): add obj_class and subj_class filter to triple f…
Browse files Browse the repository at this point in the history
…ilterset

Closes: #668
  • Loading branch information
b1rger committed Feb 28, 2024
1 parent 3c37cc3 commit 966fb17
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 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,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

0 comments on commit 966fb17

Please sign in to comment.