Skip to content

Commit

Permalink
refactor(utils)!: drop filtermethods
Browse files Browse the repository at this point in the history
The `utils.filtermethods` module contained a couple of unused
filtermethods that were used in old APIS models and referenced hardcoded
field names. They were all replaced by more flexible approaches by now.
The `construct_lookup` method that was part of the module can be useful
and was therefore salvaged and moved to the `utils.helpers` module.
The `test_filtermethods` module was renamed accordingly.

Closes: #790
  • Loading branch information
b1rger committed Jul 22, 2024
1 parent 69fe3ba commit b6c393e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 114 deletions.
113 changes: 0 additions & 113 deletions apis_core/utils/filtermethods.py

This file was deleted.

36 changes: 36 additions & 0 deletions apis_core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,39 @@ def style_insert(text):
if show_a:
result += style_remove(a[a_start:a_end])
return result


def construct_lookup(value: str) -> tuple[str, str]:
"""
Helper method to parse input values and construct field lookups
(https://docs.djangoproject.com/en/4.2/ref/models/querysets/#field-lookups)
Parses user input for wildcards and returns a tuple containing the
interpreted django lookup string and the trimmed value
E.g.
- ``example`` -> ``('__icontains', 'example')``
- ``*example*`` -> ``('__icontains', 'example')``
- ``*example`` -> ``('__iendswith', 'example')``
- ``example*``-> ``('__istartswith', 'example')``
- ``"example"`` -> ``('__iexact', 'example')``
:param str value: text to be parsed for ``*``
:return: a tuple containing the lookup type and the value without modifiers
"""

if value.startswith("*") and not value.endswith("*"):
value = value[1:]
return "__iendswith", value

elif not value.startswith("*") and value.endswith("*"):
value = value[:-1]
return "__istartswith", value

elif value.startswith('"') and value.endswith('"'):
value = value[1:-1]
return "__iexact", value

else:
if value.startswith("*") and value.endswith("*"):
value = value[1:-1]
return "__icontains", value
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from django.test import TestCase

from .filtermethods import construct_lookup
from .helpers import construct_lookup

lookups = {
"*foo*": ("__icontains", "foo"),
Expand Down

0 comments on commit b6c393e

Please sign in to comment.