|
1 | 1 | from typing import Type, Optional |
2 | 2 | from graphene import Field, Int, List, NonNull, ResolveInfo, Boolean |
3 | | -from django.db.models import QuerySet, Model, Q |
| 3 | +from django.db.models import QuerySet, Model |
4 | 4 | from graphene_django import DjangoObjectType |
5 | 5 | from django_filters import FilterSet, CharFilter, BooleanFilter |
6 | 6 |
|
|
21 | 21 | from user.types.UserType import UserType |
22 | 22 | from user.models import User |
23 | 23 | from source.types.SourceImageType import SourceImageType |
| 24 | +from graphql_app.utils import search_filter |
24 | 25 | from source.utils import source_contributor_ids |
25 | 26 |
|
26 | 27 | class SourceFilter(FilterSet): |
27 | 28 | search = CharFilter(method="search_sources") |
28 | 29 | is_public = BooleanFilter(field_name="is_public") |
29 | 30 |
|
| 31 | + _search_fields = ['name', 'medieval_title', 'reference', 'description_text'] |
| 32 | + |
| 33 | + class Meta: |
| 34 | + model = Source |
| 35 | + fields = ['is_public'] |
| 36 | + |
30 | 37 | def search_sources(self, queryset: QuerySet[Source], name: str, value: str) -> QuerySet[Source]: |
31 | | - """Filter sources by by searching through the name, reference, or description.""" |
32 | | - return queryset.filter( |
33 | | - Q(name__icontains=value) |
34 | | - | Q(medieval_title__icontains=value) |
35 | | - | Q(reference__icontains=value) |
36 | | - | Q(description_text__icontains=value) |
37 | | - ) |
| 38 | + """Filter sources by searching through the name, medieval title, reference, or description.""" |
| 39 | + return queryset.filter(search_filter(value, self._search_fields)) |
38 | 40 |
|
39 | 41 |
|
40 | 42 | class SourceType(DjangoObjectType): |
|
0 commit comments