Skip to content

Commit

Permalink
Sort items on the order they're added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Phillips committed Jul 15, 2011
1 parent 5379489 commit 6554902
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django_enumfield/enumeration.py
Expand Up @@ -27,7 +27,7 @@ def __new__(mcs, name, bases, attrs):

items.append((n, item))

items.sort(key=lambda i: i[1].display)
items.sort(key=lambda i: i[1].creation_counter)
item_objects = [i[1] for i in items]

by_val = dict((i.value, i) for i in item_objects)
Expand Down
10 changes: 10 additions & 0 deletions django_enumfield/item.py
@@ -1,4 +1,11 @@
class ItemMeta(type):
def __new__(mcs, name, bases, attrs):
mcs.creation_counter = 0
return super(ItemMeta, mcs).__new__(mcs, name, bases, attrs)

class Item(object):
__metaclass__ = ItemMeta

def __init__(self, value, slug, display=None):
if not isinstance(value, int):
raise TypeError("item value should be an integer, not %r" \
Expand All @@ -22,6 +29,9 @@ def __init__(self, value, slug, display=None):
else:
self.display = display

self.creation_counter = Item.creation_counter
Item.creation_counter += 1

def __str__(self):
return self.__unicode__()

Expand Down

0 comments on commit 6554902

Please sign in to comment.