Skip to content

Commit

Permalink
Merge branch 'hotfix/v1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
twidi committed May 5, 2015
2 parents d8db156 + 669e81e commit 01024db
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

Release *v1.0.4* - ``2015-05-05``
---------------------------------
* explicitly raise ``ValueError`` when using ``None`` for constant, value or display name.

Release *v1.0.3* - ``2015-05-05``
---------------------------------
* make it work again with Django ``ugettext_lazy``
Expand Down
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ These attributes are chainable (with a weird example to see chainability):
>>> entry.constant.value.value.display.constant.display
'Online'
To allow this, we had to remove support for ``None`` values. Use empty strings instead.

Note that constants can be accessed via a dict key (``STATES['ONLINE']`` for example) if
you want to fight your IDE that may warn you about undefined attributes.

Expand Down Expand Up @@ -302,6 +304,12 @@ will be removed (so not before ``1st of May, 2016``).
Note that you can use a specific version by pinning it in your requirements.
The only exception to these rules, it's the support of Django ``1.4`` that was removed in version
``1.0.3`` due to some incompatibility problems with ``ugettext_lazy``.
Also, the support of ``None`` values was removed, raising a ``ValueError`` telling the user to
instead use an empty string.
License
-------
Expand Down
2 changes: 1 addition & 1 deletion extended_choices/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
__author__ = 'Stephane "Twidi" Ange;'
__contact__ = "[email protected]"
__homepage__ = "https://pypi.python.org/pypi/django-extended-choices"
__version__ = "1.0.3"
__version__ = "1.0.4"
12 changes: 12 additions & 0 deletions extended_choices/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __new__(cls, *args, **kwargs):
All others are not needed for the other class, only for this mixin.
"""

if issubclass(cls, Promise):
# Special case to manage lazy django stuff like ugettext_lazy
return super(ChoiceAttributeMixin, cls).__new__(cls)
Expand All @@ -95,6 +96,7 @@ def __init__(self, value, choice_entry):
expect the ``choice_entry`` parameter.
"""

if isinstance(self, Promise):
# Special case to manage lazy django stuff like ugettext_lazy
super(ChoiceAttributeMixin, self).__init__(value._proxy____args, value._proxy____kw)
Expand Down Expand Up @@ -230,6 +232,16 @@ def _get_choice_attribute(self, value):
-------
An instance of a class based on ``ChoiceAttributeMixin`` for the given value.
Raises
------
ValueError
If the value is None, as we cannot really subclass NoneType.
"""

if value is None:
raise ValueError('Using `None` in a `Choices` object is not supported. You may '
'use an empty string.')

klass = self.ChoiceAttributeMixin.get_class_for_value(value)
return klass(value, self)
10 changes: 10 additions & 0 deletions extended_choices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,16 @@ def test_it_should_work_with_django_promises(self):
self.assertIsInstance(choice_entry.display, Promise)
self.assertEqual(choice_entry.display, ugettext_lazy('foo'))

self.assertEqual(choice_entry.display.constant, 'FOO')
self.assertEqual(choice_entry.display.value, 1)
self.assertEqual(choice_entry.display.display, ugettext_lazy('foo'))

def test_it_should_raise_with_none_value(self):
"""Test that it's clear that we don't support None values."""

with self.assertRaises(ValueError):
ChoiceEntry(('FOO', None, 'foo'))


class OldChoicesTestCase(BaseTestCase):
"""Test of tje ``Choices`` implementation as defined on version 0.4.1, for retro-compatibility.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def read_relative_file(filename):

setup(
name="django-extended-choices",
version="1.0.3",
version="1.0.4",
license="BSD",
description="Little helper application to improve django choices"
"(for fields)",
Expand Down

0 comments on commit 01024db

Please sign in to comment.