Commits

Anonymous committed e5134ec

Initial checkin

* Imported in ChoicesEnum helper class

  • Participants

Comments (0)

Files changed (7)

+glob:*.pyc
+glob:*~
+glob:dev.db
+glob:venv/
+Copyright (c) 2009, Tim Savage <tim.savage@poweredbypenguins.org>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright
+      notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+    * Neither the name of django-extras nor the
+      names of its contributors may be used to endorse or promote products
+      derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL TIM SAVAGE BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+=============
+Django Extras
+=============
+
+A selection of extra features for django that solve common annoyances 
+and limitations.
+
+https://bitbucket.org/timsavage/django-extras

django_extras/__init__.py

+# Copyright 2011 Tim Savage <tim.savage@poweredbypenguins.org>
+# Licensed under the terms of the BSD License (see LICENSE)
+
+VERSION = (0, 1, 0, 'alpha')
+__version__ = '.'.join(map(str, VERSION))

django_extras/db/__init__.py

Empty file added.

django_extras/db/fields/__init__.py

Empty file added.

django_extras/db/models.py

+
+
+class ChoiceEnum(object):
+    """
+    Defines a choice set for use with Django Models.
+
+    Items are defined with:
+
+        MY_CHOICES = ChoiceEnum(
+            OPTION_ONE = ('value', 'Verbose value'),
+            OPTION_TWO = ('value2', 'Verbose value 2'),
+        )
+
+    They can then be used for choices with:
+
+        MY_CHOICES.get_choices()
+
+    And values referenced using:
+
+        MY_CHOICES.OPTION_ONE
+
+    Display values can be accessed via:
+
+        MY_CHOICES.OPTION_ONE__display
+
+    """
+    def __init__(self, **entries):
+        self._value_map = dict([(key, value) for (key, value) in entries.values()])
+        self._choices = entries
+
+    def get_choices(self):
+        """
+        Return choice list for use in model definition.
+        """
+        return self._choices.values()
+
+    def resolve_display(self, value):
+        """
+        Resolve a value to it's display version.
+        """
+        return self._value_map[value]
+
+    def __contains__(self, value):
+        """
+        Check if a choice value is in this enum.
+
+        @param value Choice value.
+        """
+        return value in self._value_map.keys()
+
+    def __getattr__(self, item):
+        """
+        Get the value of an Enum.
+
+        Append __display to get display version of the item.
+        """
+        if item.endswith('__display'):
+            return self._choices[item[:-9]][1]
+        else:
+            return self._choices[item][0]