-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into front/changes-ui-errors-message
- Loading branch information
Showing
2 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from django.contrib.admin import ModelAdmin, site | ||
from django.db.models.query import QuerySet | ||
from django.db.models import Model | ||
from rest_framework.request import Request | ||
from unidecode import unidecode | ||
from typing import Any, Type | ||
|
||
class SearchTool: | ||
""" | ||
Search for models by given model fields and a search string. | ||
""" | ||
def __init__(self, model: Type[Model]) -> None: | ||
self.model = model | ||
|
||
def filter_by_search_result(self, request: Request, search_str: str, search_fields: list[str]) -> QuerySet[Any]: | ||
unicode_search_str = unidecode(search_str).casefold() | ||
model_handler = ModelAdmin(self.model, site) | ||
model_handler.search_fields = search_fields | ||
|
||
values = self.model.objects.all() | ||
|
||
values, _ = model_handler.get_search_results( | ||
request, values, unicode_search_str) | ||
|
||
return values |