Commits

Andy Mikhailenko committed 99940f0

Added support for coercion of (callable) breadcrumbs (to Unicode).

Comments (0)

Files changed (2)

 # tests coverage, b) sphinx and setup.py properly import the version from a
 # single place so that I don't have to edit it always here and there.
 
-version = '0.7.1'
+version = '0.8.0'

navigation/decorators.py

 __all__ = ['breadcrumb']
 
 
-def breadcrumb(crumb):
+def breadcrumb(crumb, coerce_to=None):
     """
     Usage::
 
         def some_view(request):
             return 'Hello world!'
 
-        @breadcrumb(lambda request: 'greeting for %s' % request.user.username)
+        @breadcrumb(lambda request: u'greeting for %s' % request.user.username)
         def some_view(request):
             return 'Hello %s!' % request.user.username
 
+    .. note::
+
+        By default the value returned by a callable is required to be a
+        ``unicode`` object. If the function returns a model instance, its
+        ``__unicode__`` method is *not* called. Use ``coerce_to=unicode``.
+
+    :param crumb:
+
+        A ``unicode`` string or a callable that returns it.
+
+    :param coerce_to:
+
+        Coerces *crumb* to given type. The value can be ``unicode`` or any
+        callable that returns a ``unicode`` object.
+
     """
     def wrapper(view):
         @wraps(view)
         def inner(request, *args, **kwargs):
             return view(request, *args, **kwargs)
-        inner.breadcrumb = crumb
+
+        if coerce_to is not None:
+            inner.breadcrumb = (
+                lambda *args, **kwargs: coerce_to(crumb(*args, **kwargs)))
+        else:
+            inner.breadcrumb = crumb
         return inner
     return wrapper
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.