Skip to content

Commit

Permalink
Fixes #57, Django 1.8 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanHayes committed Apr 2, 2015
1 parent e6062eb commit d01e82a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
17 changes: 15 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ env:
- DJANGO=1.4.12
- DJANGO=1.5.7
- DJANGO=1.6.4
- DJANGO=1.7.7
- DJANGO=1.8

matrix:
exclude:
Expand All @@ -37,13 +39,24 @@ matrix:
- python: "3.4"
env: DJANGO=1.4.12

- python: "2.6"
env: DJANGO=1.7.7
- python: "3.2"
env: DJANGO=1.7.7

- python: "2.6"
env: DJANGO=1.8
- python: "3.2"
env: DJANGO=1.8

before_script:
- psql -c 'create database bitfield_test;' -U postgres

install:
- pip install Django==$DJANGO
- "if [[ $DJANGO == '1.2.7' ]]; then pip install psycopg2==2.4.1; fi"
- "if [[ $DJANGO == '1.3.7' ]]; then pip install psycopg2==2.4.1; fi"
- "if [[ $DJANGO == '1.2.7' ]]; then pip install psycopg2==2.4.1 django-nose==0.1.3; fi"
- "if [[ $DJANGO == '1.3.7' ]]; then pip install psycopg2==2.4.1 django-nose==0.1.3; fi"
- "if [[ $DJANGO == '1.8' ]]; then pip install https://github.com/django-nose/django-nose/archive/master.zip; fi"
- pip install -e .

script: python setup.py test
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ django-bitfield
---------------

.. image:: https://api.travis-ci.org/disqus/django-bitfield.png?branch=master
:target: https://travis-ci.org/disqus/django-bitfield

Provides a BitField like class (using a BigIntegerField) for your Django models.

Expand Down
15 changes: 9 additions & 6 deletions bitfield/compat.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
__all__ = ('bitand', 'bitor')

def bitand(a, b):
return a.bitand(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):
return a & b

def bitor(a, b):
return a | b
else:
def bitand(a, b):
return a.bitand(b)

def bitor(a, b):
return a.bitor(b)
11 changes: 7 additions & 4 deletions bitfield/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db.models import signals
from django.db.models.sql.expressions import SQLEvaluator
from django.db.models.fields import Field, BigIntegerField
from django.db.models.fields.subclassing import Creator
try:
Expand Down Expand Up @@ -156,7 +155,7 @@ def get_prep_value(self, value):
# return super(BitField, self).get_db_prep_save(value, connection=connection)

def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
if isinstance(value, SQLEvaluator) and isinstance(value.expression, Bit):
if isinstance(getattr(value, 'expression', None), Bit):
value = value.expression
if isinstance(value, (BitHandler, Bit)):
if hasattr(self, 'class_lookups'):
Expand All @@ -168,7 +167,7 @@ def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
connection=connection, prepared=prepared)

def get_prep_lookup(self, lookup_type, value):
if isinstance(value, SQLEvaluator) and isinstance(value.expression, Bit):
if isinstance(getattr(value, 'expression', None), Bit):
value = value.expression
if isinstance(value, Bit):
if lookup_type in ('exact',):
Expand Down Expand Up @@ -242,13 +241,17 @@ def __setattr__(self, attr, value):


class CompositeBitField(object):
is_relation = False
many_to_many = False
concrete = False

def __init__(self, fields):
self.fields = fields

def contribute_to_class(self, cls, name):
self.name = name
self.model = cls
cls._meta.add_virtual_field(self)
cls._meta.virtual_fields.append(self)

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

Expand Down
2 changes: 2 additions & 0 deletions bitfield/tests/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django import forms
from bitfield.tests.models import BitFieldTestModel, CompositeBitFieldTestModel


class BitFieldTestModelForm(forms.ModelForm):

class Meta:
model = BitFieldTestModel
exclude = tuple()
22 changes: 22 additions & 0 deletions bitfield/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,25 @@ def get_label(self, flag):
if isinstance(flag, Bit):
flag = flag.number
return self._labels[flag]


import django

if django.VERSION[:2] >= (1, 8):
from django.core.exceptions import ImproperlyConfigured

# We need to register adapters in Django 1.8 in order to prevent
# "ProgrammingError: can't adapt type"
try:
from django.db.backends.sqlite3.base import Database
Database.register_adapter(Bit, lambda x: int(x))
Database.register_adapter(BitHandler, lambda x: int(x))
except ImproperlyConfigured:
pass

try:
from django.db.backends.postgresql_psycopg2.base import Database
Database.extensions.register_adapter(Bit, lambda x: Database.extensions.AsIs(int(x)))
Database.extensions.register_adapter(BitHandler, lambda x: Database.extensions.AsIs(int(x)))
except ImproperlyConfigured:
pass

0 comments on commit d01e82a

Please sign in to comment.