Skip to content

Commit

Permalink
Merge pull request #47 from adepue/master
Browse files Browse the repository at this point in the history
Support django 1.10
  • Loading branch information
Adam DePue authored Aug 3, 2016
2 parents 8d760ab + 5bf630b commit f99c6c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ env:
- DJANGO_VERSION_CEILING=1.10 DJANGO_DB_ENGINE=sqlite
- DJANGO_VERSION_CEILING=1.10 DJANGO_DB_ENGINE=postgres DJANGO_DB_NAME=testdb DJANGO_DB_USER=postgres
- DJANGO_VERSION_CEILING=1.10 DJANGO_DB_ENGINE=mysql DJANGO_DB_NAME=testdb DJANGO_DB_USER=travis
- DJANGO_VERSION_CEILING=1.11 DJANGO_DB_ENGINE=sqlite
- DJANGO_VERSION_CEILING=1.11 DJANGO_DB_ENGINE=postgres DJANGO_DB_NAME=testdb DJANGO_DB_USER=postgres
- DJANGO_VERSION_CEILING=1.11 DJANGO_DB_ENGINE=mysql DJANGO_DB_NAME=testdb DJANGO_DB_USER=travis

install:
- pip install "Django<$DJANGO_VERSION_CEILING" mysqlclient psycopg2
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


install_requires = (
'Django>=1.8,<1.10',
'Django>=1.8,<1.11',
'richenum',
'six',
)
Expand Down Expand Up @@ -78,7 +78,7 @@ def run_tests(self):

setup(
name='django-richenum',
version='3.0.1',
version='3.1.0',
description='Django Enum library for python.',
long_description=(
open('README.rst').read() + '\n\n' +
Expand Down
31 changes: 29 additions & 2 deletions src/django_richenum/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,34 @@
from richenum import RichEnumValue, OrderedRichEnumValue


# https://github.com/django/django/blob/64200c14e0072ba0ffef86da46b2ea82fd1e019a/django/db/models/fields/subclassing.py#L31-L44
class Creator(object):
"""
A placeholder class that provides a way to set the attribute on the model.
"""
def __init__(self, field):
self.field = field

def __get__(self, obj, type=None):
if obj is None:
return self
return obj.__dict__[self.field.name]

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


class IndexEnumField(models.IntegerField):
'''Store ints in DB, but expose OrderedRichEnumValues in Python.
'''
description = 'Efficient storage for OrderedRichEnums'
__metaclass__ = models.SubfieldBase

def contribute_to_class(self, cls, name, **kwargs):
super(IndexEnumField, self).contribute_to_class(cls, name, **kwargs)

# Add Creator descriptor to allow the field to be set directly
setattr(cls, self.name, Creator(self))

def __init__(self, enum, *args, **kwargs):
if not hasattr(enum, 'from_index'):
Expand Down Expand Up @@ -110,7 +132,12 @@ class CanonicalNameEnumField(models.CharField):
'''
description = 'Storage for RichEnums'
__metaclass__ = models.SubfieldBase

def contribute_to_class(self, cls, name, **kwargs):
super(CanonicalNameEnumField, self).contribute_to_class(cls, name, **kwargs)

# Add Creator descriptor to allow the field to be set directly
setattr(cls, self.name, Creator(self))

def __init__(self, enum, *args, **kwargs):
if not hasattr(enum, 'from_canonical'):
Expand Down

0 comments on commit f99c6c8

Please sign in to comment.