Skip to content

Commit

Permalink
merge from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
dss09 committed Sep 16, 2019
2 parents ae52c35 + 6fe7d1f commit daceab6
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 196 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- '3.4'
- '3.5'
- '3.6'
- '3.7'
install: pip install flake8 tox-travis

services:
Expand Down
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Provides a BitField like class (using a BigIntegerField) for your Django models.
Requirements
============

* Django >= 1.4
* Django >= 1.10.8
* PostgreSQL (see notes)

**Notes:**
Expand Down Expand Up @@ -109,3 +109,12 @@ To use it set ``list_filter`` ModelAdmin option::
BitFieldListFilter is in ``bitfield.admin`` module::

from bitfield.admin import BitFieldListFilter

Changelog
=========

Scheduled for 2.0 release:

- Drop support for Django versions below 1.10.
- Use _meta.private_fields instead of deprecated _meta.virtual_fields in CompositeBitField.
- Add testing with python 3.6, 3.7 and Django 2.x to travis configuration.
6 changes: 1 addition & 5 deletions bitfield/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@

default_app_config = 'bitfield.apps.BitFieldAppConfig'

try:
VERSION = __import__('pkg_resources') \
.get_distribution('bitfield').version
except Exception:
VERSION = 'unknown'
VERSION = '1.9.6+whyfly.1'
16 changes: 0 additions & 16 deletions bitfield/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,3 @@ def bitand(a, b):

def bitor(a, b):
return a.bitor(b)


try:
from django.db.models.expressions import ExpressionNode
ExpressionNode.BITAND # noqa
del ExpressionNode
except ImportError:
# Django >= 1.8
pass
except AttributeError:
# Django < 1.5
def bitand(a, b): # NOQA
return a & b

def bitor(a, b): # NOQA
return a | b
13 changes: 5 additions & 8 deletions bitfield/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

from django.forms import CheckboxSelectMultiple, IntegerField, ValidationError

try:
from django.utils.encoding import force_text
except ImportError:
from django.utils.encoding import force_unicode as force_text
from django.utils.encoding import force_text

from bitfield.types import BitHandler


class BitFieldCheckboxSelectMultiple(CheckboxSelectMultiple):
def render(self, name, value, attrs=None, choices=()):
def render(self, name, value, attrs=None, choices=(), renderer=None):
if isinstance(value, BitHandler):
value = [k for k, v in value if v]
elif isinstance(value, int):
Expand Down Expand Up @@ -43,11 +40,11 @@ def __init__(self, choices=(), widget=BitFieldCheckboxSelectMultiple, *args, **k

if isinstance(kwargs['initial'], int):
iv = kwargs['initial']
li = []
iv_list = []
for i in range(0, min(len(choices), 63)):
if (1 << i) & iv > 0:
li += [choices[i][0]]
kwargs['initial'] = li
iv_list += [choices[i][0]]
kwargs['initial'] = iv_list
self.widget = widget
super(BitFormField, self).__init__(widget=widget, *args, **kwargs)
self.choices = self.widget.choices = choices
Expand Down
11 changes: 2 additions & 9 deletions bitfield/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ def deconstruct(self):
return name, path, args, kwargs


try:
BitField.register_lookup(BitQueryLookupWrapper)
except AttributeError:
pass
BitField.register_lookup(BitQueryLookupWrapper)


class CompositeBitFieldWrapper(object):
Expand Down Expand Up @@ -249,11 +246,7 @@ def __init__(self, fields):
def contribute_to_class(self, cls, name):
self.name = name
self.model = cls
# virtual_fields was deprecated in Django 1.10 and removed in 2.0
private_fields = getattr(cls._meta, "private_fields", None)
if private_fields is None:
private_fields = cls._meta.virtual_fields
private_fields.append(self)
cls._meta.private_fields.append(self)

signals.class_prepared.connect(self.validate_fields, sender=cls)

Expand Down
58 changes: 16 additions & 42 deletions bitfield/query.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,26 @@
from __future__ import absolute_import

from bitfield.types import Bit, BitHandler
from django.db.models.lookups import Exact


class BitQueryLookupWrapper(object):
def __init__(self, alias, column, bit):
self.table_alias = alias
self.column = column
self.bit = bit

def as_sql(self, qn, connection=None):
"""
Create the proper SQL fragment. This inserts something like
"(T0.flags & value) != 0".
This will be called by Where.as_sql()
"""
if self.bit:
return ("(%s.%s | %d)" % (qn(self.table_alias), qn(self.column), self.bit.mask),
[])
return ("(%s.%s & %d)" % (qn(self.table_alias), qn(self.column), self.bit.mask),
[])


try:
# Django 1.7+
from django.db.models.lookups import Exact

class BitQueryLookupWrapper(Exact): # NOQA
def process_lhs(self, qn, connection, lhs=None):
lhs_sql, params = super(BitQueryLookupWrapper, self).process_lhs(
qn, connection, lhs)
if self.rhs:
lhs_sql = lhs_sql + ' & %s'
else:
lhs_sql = lhs_sql + ' | %s'
params.extend(self.get_db_prep_lookup(self.rhs, connection)[1])
return lhs_sql, params

def get_db_prep_lookup(self, value, connection, prepared=False):
v = value.mask if isinstance(value, (BitHandler, Bit)) else value
return super(BitQueryLookupWrapper, self).get_db_prep_lookup(v, connection)
class BitQueryLookupWrapper(Exact): # NOQA
def process_lhs(self, qn, connection, lhs=None):
lhs_sql, params = super(BitQueryLookupWrapper, self).process_lhs(
qn, connection, lhs)
if self.rhs:
lhs_sql = lhs_sql + ' & %s'
else:
lhs_sql = lhs_sql + ' | %s'
params.extend(self.get_db_prep_lookup(self.rhs, connection)[1])
return lhs_sql, params

def get_prep_lookup(self):
return self.rhs
def get_db_prep_lookup(self, value, connection, prepared=False):
v = value.mask if isinstance(value, (BitHandler, Bit)) else value
return super(BitQueryLookupWrapper, self).get_db_prep_lookup(v, connection)

except ImportError:
pass
def get_prep_lookup(self):
return self.rhs


class BitQuerySaveWrapper(BitQueryLookupWrapper):
Expand Down
Loading

0 comments on commit daceab6

Please sign in to comment.