Skip to content

Commit

Permalink
Merge pull request #25 from pstch/develop
Browse files Browse the repository at this point in the history
Add get_*_url_name functionality, go 0.4.3
  • Loading branch information
Hugo Geoffroy committed Apr 23, 2014
2 parents 1322bb7 + 014e1bd commit 5eba801
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion django_crucrudile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

__title__ = 'django-crucrudile'
__version__ = '0.4.1'
__version__ = '0.4.3'
__author__ = 'Hugo Geoffroy'
__license__ = 'GNU General Public License V3.0'
__copyright__ = 'Copyright 2013-2014 Hugo Geoffroy'
22 changes: 21 additions & 1 deletion django_crucrudile/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ def _get_url(cls, *args, **kwargs):
'get_%s_url' % view_class.get_underscored_action_name(),
_get_url)

def _get_url_name(cls):
"""Private function, patched as ``get_*_url_name`` to the model mixin.
These functions only return an URL name, and you don't have to
pass an instance as argument because the instance is not
included in the return value
"""
return cls.get_url_name(view_class, prefix=True)

_get_url_name.__doc__ = "Get %s URL" % view_class.get_action_name()

# we make _get_url_name a class method only at this point to be able
# to change __doc__
_get_url_name = classmethod(_get_url_name)

setattr(ModelMixin,
'get_%s_url_name' % view_class.get_underscored_action_name(),
_get_url_name)

if extra_funcs:
for func_name, func in extra_funcs.items():
func_name = try_calling(func_name, view_class) or func_name
Expand Down Expand Up @@ -280,7 +300,7 @@ def get_url_name(cls, view, prefix=False):

namespaces_list = cls.get_url_namespaces()
if prefix and namespaces_list:
return ':'.join(cls.get_url_namespaces() + [name, ])
return ':'.join(namespaces_list + [name, ])
return name

@classmethod
Expand Down
27 changes: 24 additions & 3 deletions tests/test_model_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class MakeModelMixinTestCase(TestCase):

view_class = MakeModelMixinTestView
url_func_name = 'get_make_model_mixin_test_url'
url_name_func_name = 'get_make_model_mixin_test_url_name'


# data to test make_model_mixin (with extra_args argument) with
extra_args = {'test_key' : 'test_value',
Expand Down Expand Up @@ -194,16 +196,31 @@ def test_make_model_mixin_get_args_by_view(self):
def test_make_model_mixin_has_url_func(self):
self.assertTrue(
hasattr(self.model_class,
self.url_func_name)
self.url_name_func_name)
)
self.assertTrue(
callable(
getattr(self.model_class,
self.url_func_name,
self.url_name_func_name,
None)
)
)

def test_make_model_mixin_url_name_func(self):
self.assertTrue(
hasattr(
self.model_class,
self.url_name_func_name
)
)
self.assertEqual(
getattr(
self.model_class,
self.url_name_func_name,
lambda: None
)(),
'tests:makemodelmixintestmodel-make-model-mixin-test'
)

def test_make_model_mixin_extra_args(self):
self.assertEqual(
Expand All @@ -224,7 +241,6 @@ def test_make_model_mixin_extra_funcs(self):

class MakeModelMixinWithoutViewMixinTestCase(MakeModelMixinTestCase):
view_class = MakeModelMixinWithoutViewMixinTestView
url_func_name = 'get_make_model_mixin_without_view_mixin_test_url'

def setUp(self):
class MakeModelMixinWithoutViewMixinTestModel(
Expand Down Expand Up @@ -255,6 +271,11 @@ class ExtraFuncsMakeModelMixinWithoutViewMixinTestModel(
test_callable_value = 'model_test_callable_value'
self.extra_funcs_model_class = ExtraFuncsMakeModelMixinWithoutViewMixinTestModel

def test_make_model_mixin_url_name_func(self):
pass # not run without a view
def test_make_model_mixin_has_url_func(self):
pass # not run without a view

class MakeModelMixinsTestCase(TestCase):
views = [MakeModelMixinsFirstTestView,
MakeModelMixinsSecondTestView,
Expand Down

0 comments on commit 5eba801

Please sign in to comment.