forked from DGNum/gestioCOF
Drop type hints in shared.views.autocomplete
This commit is contained in:
parent
a259b04d9c
commit
b8cd5f1da5
1 changed files with 10 additions and 19 deletions
|
@ -1,20 +1,13 @@
|
|||
from typing import Generic, Iterable, Type, TypeVar
|
||||
|
||||
from dal import autocomplete # type: ignore
|
||||
from django.db.models import Model, Q
|
||||
|
||||
M = TypeVar("M", bound=Model)
|
||||
from dal import autocomplete
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
class ModelSearch(Generic[M]):
|
||||
class ModelSearch:
|
||||
"""Basic search engine for models based on filtering.
|
||||
|
||||
As the type hints indicate, the class is generic with respect to the model. This
|
||||
means that the ``search`` method only returns instances of the model specified as
|
||||
the ``model`` class attribute in subclasses.
|
||||
|
||||
The ``search_fields`` attributes indicates which fields to search in during the
|
||||
search.
|
||||
The class should be configured through its ``model`` class attribute: the ``search``
|
||||
method will return a queryset of instances of this model. The ``search_fields``
|
||||
attributes indicates which fields to search in.
|
||||
|
||||
Example:
|
||||
|
||||
|
@ -28,12 +21,10 @@ class ModelSearch(Generic[M]):
|
|||
>>> user_search.search(["toto", "foo"]) # returns a queryset of Users
|
||||
"""
|
||||
|
||||
# This says that `model` is the class corresponding to the type variable M (or a
|
||||
# subclass).
|
||||
model: Type[M]
|
||||
search_fields: Iterable[str]
|
||||
model = None
|
||||
search_fields = []
|
||||
|
||||
def get_queryset_filter(self, keywords: Iterable[str]) -> Q:
|
||||
def get_queryset_filter(self, keywords):
|
||||
filter_q = Q()
|
||||
|
||||
if not keywords:
|
||||
|
@ -47,7 +38,7 @@ class ModelSearch(Generic[M]):
|
|||
|
||||
return filter_q
|
||||
|
||||
def search(self, keywords: Iterable[str]) -> Iterable[M]:
|
||||
def search(self, keywords):
|
||||
"""Returns the queryset of model instances matching all the keywords.
|
||||
|
||||
The semantic of the search is the following: a model instance appears in the
|
||||
|
|
Loading…
Reference in a new issue