Skip to content

Commit

Permalink
Add custom SearchFilter to account for the language fields (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Dec 18, 2023
1 parent 263fa92 commit 799115b
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 8 deletions.
8 changes: 8 additions & 0 deletions rdmo/conditions/tests/test_viewset_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['condition']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/conditions/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django_filters.rest_framework import DjangoFilterBackend

from rdmo.core.exports import XMLResponse
from rdmo.core.filters import SearchFilter
from rdmo.core.permissions import HasModelPermission, HasObjectPermission
from rdmo.core.utils import is_truthy, render_to_format
from rdmo.core.views import ChoicesViewSet
Expand Down
30 changes: 30 additions & 0 deletions rdmo/core/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from django.db.models import Q
from django.utils.translation import get_language

from rest_framework.filters import BaseFilterBackend

from rdmo.core.utils import get_languages


class SearchFilter(BaseFilterBackend):

def filter_queryset(self, request, queryset, view):
if view.detail:
return queryset

search = request.GET.get('search')
if search:
q = Q()

if 'uri' in view.search_fields:
q |= Q(uri__contains=search)

for lang_code, lang_string, lang_field in get_languages():
if lang_code == get_language():
for search_field in ['title', 'text']:
if search_field in view.search_fields:
q |= Q(**{f'{search_field}_{lang_field}__contains': search})

queryset = queryset.filter(q)

return queryset
8 changes: 8 additions & 0 deletions rdmo/domain/tests/test_viewset_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['attribute']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/domain/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db import models

from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django_filters.rest_framework import DjangoFilterBackend

from rdmo.core.exports import XMLResponse
from rdmo.core.filters import SearchFilter
from rdmo.core.permissions import HasModelPermission, HasObjectPermission
from rdmo.core.utils import render_to_csv, render_to_format

Expand Down
8 changes: 8 additions & 0 deletions rdmo/options/tests/test_viewset_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['option']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
8 changes: 8 additions & 0 deletions rdmo/options/tests/test_viewset_optionsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['optionset', 'option']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/options/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from django.db import models

from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django_filters.rest_framework import DjangoFilterBackend

from rdmo.core.exports import XMLResponse
from rdmo.core.filters import SearchFilter
from rdmo.core.permissions import HasModelPermission, HasObjectPermission
from rdmo.core.utils import is_truthy, render_to_format
from rdmo.core.views import ChoicesViewSet
Expand Down
8 changes: 8 additions & 0 deletions rdmo/questions/tests/test_viewset_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['catalog', 'section', 'page', 'questionset', 'question']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
8 changes: 8 additions & 0 deletions rdmo/questions/tests/test_viewset_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['page', 'questionset', 'question']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
8 changes: 8 additions & 0 deletions rdmo/questions/tests/test_viewset_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['question']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
8 changes: 8 additions & 0 deletions rdmo/questions/tests/test_viewset_questionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['questionset', 'question']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
8 changes: 8 additions & 0 deletions rdmo/questions/tests/test_viewset_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['section', 'page', 'questionset', 'question']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
2 changes: 1 addition & 1 deletion rdmo/questions/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.db import models

from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
Expand All @@ -10,6 +9,7 @@

from rdmo.core.constants import VALUE_TYPE_CHOICES
from rdmo.core.exports import XMLResponse
from rdmo.core.filters import SearchFilter
from rdmo.core.permissions import HasModelPermission, HasObjectPermission
from rdmo.core.utils import is_truthy, render_to_format
from rdmo.core.views import ChoicesViewSet
Expand Down
8 changes: 8 additions & 0 deletions rdmo/tasks/tests/test_viewset_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['task']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
4 changes: 2 additions & 2 deletions rdmo/tasks/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db import models

from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django_filters.rest_framework import DjangoFilterBackend

from rdmo.core.exports import XMLResponse
from rdmo.core.filters import SearchFilter
from rdmo.core.permissions import HasModelPermission, HasObjectPermission
from rdmo.core.utils import is_truthy, render_to_format

Expand All @@ -26,7 +26,7 @@ class TaskViewSet(ModelViewSet):
.order_by('uri')

filter_backends = (SearchFilter, DjangoFilterBackend)
search_fields = ('uri', )
search_fields = ('uri', 'title')
filterset_fields = (
'uri',
'uri_prefix',
Expand Down
8 changes: 8 additions & 0 deletions rdmo/views/tests/test_viewset_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def test_export(db, client, username, password, export_format):
assert child.tag in ['view']


def test_export_search(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['export']) + 'xml/?search=bar'
response = client.get(url)
assert response.status_code == status_map['list']['editor'], response.content


@pytest.mark.parametrize('username,password', users)
def test_detail(db, client, username, password):
client.login(username=username, password=password)
Expand Down
4 changes: 2 additions & 2 deletions rdmo/views/viewsets.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db import models

from rest_framework.decorators import action
from rest_framework.filters import SearchFilter
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from django_filters.rest_framework import DjangoFilterBackend

from rdmo.core.exports import XMLResponse
from rdmo.core.filters import SearchFilter
from rdmo.core.permissions import HasModelPermission, HasObjectPermission
from rdmo.core.utils import render_to_format

Expand All @@ -25,7 +25,7 @@ class ViewViewSet(ModelViewSet):
.order_by('uri')

filter_backends = (SearchFilter, DjangoFilterBackend)
search_fields = ('uri', )
search_fields = ('uri', 'title')
filterset_fields = (
'uri',
'uri_prefix',
Expand Down

0 comments on commit 799115b

Please sign in to comment.