Skip to content

admin: use BitHandler instead of bitor operator #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions bitfield/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import six

from django.db.models import F
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import FieldListFilter
from django.contrib.admin.options import IncorrectLookupParameters

from bitfield import Bit
from bitfield import Bit, BitHandler


class BitFieldListFilter(FieldListFilter):
Expand All @@ -23,9 +22,14 @@ def __init__(self, field, request, params, model, model_admin, field_path):
field, request, params, model, model_admin, field_path)

def queryset(self, request, queryset):
filter = dict((p, F(p).bitor(v)) for p, v in six.iteritems(self.used_parameters))
filter_kwargs = dict(
(p, BitHandler(v, ()))
for p, v in six.iteritems(self.used_parameters)
)
if not filter_kwargs:
return queryset
try:
return queryset.filter(**filter)
return queryset.filter(**filter_kwargs)
except ValidationError as e:
raise IncorrectLookupParameters(e)

Expand Down