Skip to content

Commit

Permalink
Support django admin search for binary fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Mar 15, 2022
1 parent cf0ae43 commit f51fc5e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions gnosis/eth/django/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Tuple

from django.contrib import admin
from django.core.exceptions import ValidationError
from django.db.models import QuerySet
from django.http import HttpRequest


class BinarySearchAdmin(admin.ModelAdmin):
"""
Search inside binary fields, like EthereumAddressV2Field or Keccack256Field
"""

def get_search_results(
self, request: HttpRequest, queryset: QuerySet, search_term: str
) -> Tuple[QuerySet, bool]:
queryset, may_have_duplicates = super().get_search_results(
request, queryset, search_term
)
if search_term:
for search_field in self.get_search_fields(request):
try:
if search_field.startswith("="):
may_have_duplicates = True
queryset |= self.model.objects.filter(
**{search_field[1:]: search_term}
)
elif search_field.endswith("__icontains"):
may_have_duplicates = True
queryset |= self.model.objects.filter(
**{
search_field.replace("__icontains", "__contains"): [
search_term
]
}
)
except (ValidationError, ValueError):
pass
return queryset, may_have_duplicates
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

setup(
name="gnosis-py",
version="3.8.1",
version="3.8.2",
packages=find_packages(),
package_data={"gnosis": ["py.typed"]},
install_requires=requirements,
Expand Down

0 comments on commit f51fc5e

Please sign in to comment.