Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(relations): limit the number of subj/obj models in filter #1425

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions apis_core/relations/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django_filters import CharFilter, MultipleChoiceFilter

Expand Down Expand Up @@ -42,11 +43,10 @@ class SubjObjClassFilter(MultipleChoiceFilter):
subjects and objects
"""

def __init__(self, *args, **kwargs):
def __init__(self, models, *args, **kwargs):
super().__init__(*args, **kwargs)
self.extra["choices"] = [
(item.id, item.name) for item in get_all_relation_subj_and_obj()
]
content_types = [ContentType.objects.get_for_model(model) for model in models]
self.extra["choices"] = [(item.id, item.name) for item in content_types]

def filter(self, qs, value: list[str] | None):
# value is the list of contenttypes ids
Expand Down Expand Up @@ -75,14 +75,6 @@ class RelationFilterSet(GenericFilterSet):
field_name="obj",
label="Object search",
)
subj_class = SubjObjClassFilter(
field_name="subj",
label="Subject class",
)
obj_class = SubjObjClassFilter(
field_name="obj",
label="Object class",
)

class Meta:
exclude = [
Expand All @@ -92,3 +84,18 @@ class Meta:
"obj_content_type",
]
form = GenericFilterSet.Meta.form

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if model := getattr(self.Meta, "model", False):
all_models = [ct.model_class() for ct in get_all_relation_subj_and_obj()]
subj_models = getattr(model, "subj_model", all_models)
obj_models = getattr(model, "obj_model", all_models)
if isinstance(subj_models, list):
self.filters["subj_class"] = SubjObjClassFilter(
field_name="subj", label="Subject class", models=subj_models
)
if isinstance(obj_models, list):
self.filters["obj_class"] = SubjObjClassFilter(
field_name="obj", label="Object Class", models=obj_models
)
Loading