Skip to content

Commit

Permalink
feat: search by metainfo fields
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Jul 1, 2022
1 parent d915d9c commit be26f41
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
15 changes: 12 additions & 3 deletions emeis/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
from emeis.core.models import Scope, User


class MonolingualSearchFilter(filters.SearchFilter):
class EmeisSearchFilter(filters.SearchFilter):
"""Custom search filter for Emeis.
This adds two things:
- "monolingual" search support via EMEIS_FORCE_MODEL_LOCALE
- search support for EMEIS_META_FIELDS
"""

multilingual_search_fields = []

Expand All @@ -23,8 +29,11 @@ def get_search_fields(self, view, request):

model_name = view.queryset.model.__name__.lower()

meta_fields = [f"metainfo__{f}" for f in settings.EMEIS_META_FIELDS]

if model_name not in settings.EMEIS_FORCE_MODEL_LOCALE:
return super().get_search_fields(view, request)
default_fields = super().get_search_fields(view, request)
return list(default_fields) + meta_fields if default_fields else meta_fields

forced_lang = settings.EMEIS_FORCE_MODEL_LOCALE[model_name]

Expand All @@ -33,7 +42,7 @@ def get_search_fields(self, view, request):
if field in view.multilingual_search_fields
else field
for field in view.search_fields
]
] + meta_fields


class UserFilterset(FilterSet):
Expand Down
22 changes: 22 additions & 0 deletions emeis/core/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,25 @@ def test_search_monolingual(
resp = admin_client.get(reverse("role-list"), {"filter[search]": search_term})

assert len(resp.json()["data"]) == expect_result


@pytest.mark.parametrize(
"search_term, expect_result",
[
("foobar", 1),
("bar", 2),
("buzzybuzz", 0),
],
)
def test_search_metainfo(
settings, admin_client, user_factory, search_term, expect_result
):
settings.EMEIS_META_FIELDS = ["position"]
user_factory(metainfo={"position": "bar"})
user_factory(metainfo={"position": "baz"})
user_factory(metainfo={"blubb": "bar"})
user_factory(metainfo={"position": "Foobar"})

resp = admin_client.get(reverse("user-list"), {"filter[search]": search_term})

assert len(resp.json()["data"]) == expect_result
9 changes: 5 additions & 4 deletions emeis/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class UserViewSet(BaseViewset):

filterset_class = filters.UserFilterset

search_fields = (
search_fields = [
"username",
"first_name",
"last_name",
Expand All @@ -65,7 +65,8 @@ class UserViewSet(BaseViewset):
"email",
"=zip",
"city",
)
]

multilingual_search_fields = [
"city",
"acls__role__name",
Expand Down Expand Up @@ -187,7 +188,7 @@ class ACLViewSet(BaseViewset):
serializer_class = serializers.ACLSerializer
queryset = models.ACL.objects.all()
filterset_fields = ("user", "scope", "role")
search_fields = (
search_fields = [
"scope__name",
"scope__description",
"role__name",
Expand All @@ -197,7 +198,7 @@ class ACLViewSet(BaseViewset):
"user__last_name",
"=user__zip",
"user__city",
)
]

multilingual_search_fields = [
"scope__name",
Expand Down
2 changes: 1 addition & 1 deletion emeis/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def parse_languages(languages):
"rest_framework_json_api.filters.QueryParameterValidationFilter",
"emeis.core.filters.EmeisOrderingFilter",
"rest_framework_json_api.django_filters.DjangoFilterBackend",
"emeis.core.filters.MonolingualSearchFilter",
"emeis.core.filters.EmeisSearchFilter",
),
"SEARCH_PARAM": "filter[search]",
"ORDERING_PARAM": "sort",
Expand Down

0 comments on commit be26f41

Please sign in to comment.