Skip to content

Commit

Permalink
Add IntEnum (like the standard library's IntEnum)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Jun 16, 2016
1 parent 1e112ab commit 6e0344a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion enumfields/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .enums import Enum
from .enums import Enum, IntEnum
from .fields import EnumField, EnumIntegerField
6 changes: 6 additions & 0 deletions enumfields/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ def __str__(self):
Show our label when Django uses the Enum for displaying in a view
"""
return force_text(self.label)


@python_2_unicode_compatible
class IntEnum(int, Enum):
def __str__(self): # See Enum.__str__
return force_text(self.label)
5 changes: 3 additions & 2 deletions tests/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.db import models
from enum import Enum, IntEnum

from enumfields import EnumField, EnumIntegerField
from enumfields import Enum, IntEnum, EnumField, EnumIntegerField


class MyModel(models.Model):
Expand All @@ -26,6 +25,8 @@ class ZeroEnum(Enum):
class IntegerEnum(IntEnum):
A = 0
B = 1
class Labels:
A = 'foo'

taste = EnumField(Taste, default=Taste.SWEET)
taste_null_default = EnumField(Taste, null=True, blank=True, default=None)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import six
from six import u

from tests.models import MyModel


class Color(Enum):
__order__ = 'RED GREEN BLUE'
Expand Down Expand Up @@ -35,11 +37,13 @@ def test_custom_labels():
# Custom label
assert Color.RED.label == 'Reddish'
assert six.text_type(Color.RED) == 'Reddish'
assert six.text_type(MyModel.IntegerEnum.A) == 'foo'

def test_automatic_labels():
# Automatic label
assert Color.GREEN.label == 'Green'
assert six.text_type(Color.GREEN) == 'Green'
assert six.text_type(MyModel.IntegerEnum.B) == 'B'

def test_lazy_labels():
# Lazy label
Expand Down
7 changes: 4 additions & 3 deletions tests/test_form_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -- encoding: UTF-8 --

from django.db.models import BLANK_CHOICE_DASH
from django.forms.models import modelform_factory
import pytest
from .models import MyModel
Expand All @@ -8,7 +8,7 @@

def get_form(**kwargs):
instance = MyModel(color=MyModel.Color.RED)
FormClass = modelform_factory(MyModel, fields=("color", "zero2"))
FormClass = modelform_factory(MyModel, fields=("color", "zero2", "int_enum"))
return FormClass(instance=instance, **kwargs)


Expand All @@ -26,4 +26,5 @@ def test_bound_form_with_instance():

def test_choices():
form = get_form()
assert form.base_fields["zero2"].choices == [(0, 'ZERO'), (1, 'ONE')]
assert form.base_fields["zero2"].choices == [(0, 'Zero'), (1, 'One')]
assert form.base_fields["int_enum"].choices == BLANK_CHOICE_DASH + [(0, 'foo'), (1, 'B')]

0 comments on commit 6e0344a

Please sign in to comment.