From 0b36d58623425c77d14ff6831bc7fb820c5b03d0 Mon Sep 17 00:00:00 2001 From: Caio Felipe Date: Tue, 13 Aug 2024 11:26:28 -0300 Subject: [PATCH] refactor(search-tool): separa o buscador principal da view (#212) * api(views): update view to use search tool class * utils(search): separate search admin tool * fix(views): remove useless imports * fix(utils/search): add trailing whitespace * remove trailing space --- api/api/views/views.py | 24 +++++++++++------------- api/utils/search.py | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 api/utils/search.py diff --git a/api/api/views/views.py b/api/api/views/views.py index c10f749..353f087 100644 --- a/api/api/views/views.py +++ b/api/api/views/views.py @@ -1,7 +1,5 @@ -from unidecode import unidecode -from ..models import Class, Discipline +from ..models import Discipline -from django.contrib import admin from django.db.models.query import QuerySet from rest_framework.decorators import APIView @@ -13,6 +11,7 @@ from utils.sessions import get_current_year_and_period, get_next_period from utils.schedule_generator import ScheduleGenerator from utils.db_handler import get_best_similarities_by_name, filter_disciplines_by_teacher, filter_disciplines_by_year_and_period, filter_disciplines_by_code +from utils.search import SearchTool from .. import serializers from api.swagger import Errors @@ -37,17 +36,16 @@ def treat_string(self, string: str | None) -> str | None: return string def filter_disciplines(self, request: request.Request, name: str) -> QuerySet[Discipline]: - unicode_name = unidecode(name).casefold() - model_handler = admin.ModelAdmin(Discipline, admin.site) - model_handler.search_fields = [ - 'unicode_name', 'code'] - - disciplines = Discipline.objects.all() - - disciplines, _ = model_handler.get_search_results( - request, disciplines, unicode_name) + search_handler = SearchTool(Discipline) + search_fields = ['unicode_name', 'code'] + + result = search_handler.filter_by_search_result( + request = request, + search_str = name, + search_fields = search_fields + ) - return disciplines + return result def retrieve_disciplines_by_similarity(self, request: request.Request, name: str) -> QuerySet[Discipline]: disciplines = self.filter_disciplines(request, name) diff --git a/api/utils/search.py b/api/utils/search.py new file mode 100644 index 0000000..c8fc4e6 --- /dev/null +++ b/api/utils/search.py @@ -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