Skip to content

Commit

Permalink
#1 compatible with django 1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
znotdead committed Aug 16, 2016
1 parent 1c4cc53 commit b8f1dbb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Provides a BitField like class (using a BigIntegerField) for your Django models.
Requirements
============

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

**Notes:**
Expand Down
1 change: 1 addition & 0 deletions bitfield/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from bitfield.models import Bit, BitHandler, CompositeBitField, BitField # NOQA

default_app_config = 'bitfield.apps.BitFieldConfig'

try:
VERSION = __import__('pkg_resources') \
Expand Down
6 changes: 6 additions & 0 deletions bitfield/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BitFieldAppConfig(AppConfig):
name = 'bitfield'
verbose_name = "Bit Field"
36 changes: 12 additions & 24 deletions bitfield/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

from django.db.models import signals
from django.db.models.fields import Field, BigIntegerField
from django.db.models.fields.subclassing import Creator
try:
from django.db.models.fields.subclassing import SubfieldBase
except ImportError:
# django 1.2
from django.db.models.fields.subclassing import LegacyConnection as SubfieldBase # NOQA

from bitfield.forms import BitFormField
from bitfield.query import BitQueryLookupWrapper
Expand Down Expand Up @@ -60,13 +54,20 @@ def values(self):
return list(self.itervalues())


class BitFieldCreator(Creator):
class BitFieldCreator(object):
"""
A placeholder class that provides a way to set the attribute on the model.
Descriptor for BitFields. Checks to make sure that all flags of the
instance match the class. This is to handle the case when caching
an older version of the instance and a newer version of the class is
available (usually during deploys).
"""
def __init__(self, field):
self.field = field

def __set__(self, obj, value):
obj.__dict__[self.field.name] = self.field.to_python(value)

def __get__(self, obj, type=None):
if obj is None:
return BitFieldFlags(self.field.flags)
Expand All @@ -77,26 +78,13 @@ def __get__(self, obj, type=None):
return retval


class BitFieldMeta(SubfieldBase):
"""
Modified SubFieldBase to use our contribute_to_class method (instead of
monkey-patching make_contrib). This uses our BitFieldCreator descriptor
in place of the default.
NOTE: If we find ourselves needing custom descriptors for fields, we could
make this generic.
"""
def __new__(cls, name, bases, attrs):
def contribute_to_class(self, cls, name):
BigIntegerField.contribute_to_class(self, cls, name)
setattr(cls, self.name, BitFieldCreator(self))
class BitField(BigIntegerField):

new_class = super(BitFieldMeta, cls).__new__(cls, name, bases, attrs)
new_class.contribute_to_class = contribute_to_class
return new_class

def contribute_to_class(self, cls, name, **kwargs):
super(BitField, self).contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, BitFieldCreator(self))

class BitField(six.with_metaclass(BitFieldMeta, BigIntegerField)):
def __init__(self, flags, default=None, *args, **kwargs):
if isinstance(flags, dict):
# Get only integer keys in correct range
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

setup(
name='django-bitfield',
version='1.8.0',
version='1.9.0',
author='DISQUS',
author_email='[email protected]',
url='https://github.com/disqus/django-bitfield',
description='BitField in Django',
packages=find_packages(),
zip_safe=False,
install_requires=[
'Django>=1.4',
'Django>=1.10',
'six',
],
extras_require={
Expand Down

0 comments on commit b8f1dbb

Please sign in to comment.