Added an ExtraContextMixin #102

Closed
wants to merge 1 commit into
from

Projects

None yet

3 participants

@kitsunde
Contributor
kitsunde commented Feb 4, 2014

Hello!

This allows the user pass in a static context without needing to override get_context_data in their own views. We specifically use something like this to pass in active nav variables for menu highlighting.

Bundled with tests and doc updates. :)

@chrisjones-brack3t chrisjones-brack3t commented on the diff Feb 28, 2014
braces/views.py
+ """
+ extra_context = None
+
+ def get_context_data(self, **kwargs):
+ kwargs = super(ExtraContextMixin, self).get_context_data(**kwargs)
+ kwargs.update(self.get_extra_context())
+ return kwargs
+
+ def get_extra_context(self):
+ if self.extra_context is None:
+ raise ImproperlyConfigured(
+ "%(cls)s is missing extra_context. "
+ "Define %(cls)s.extra_context, or override "
+ "%(cls)s.get_extra_context()." % {"cls": self.__class__.__name__}
+ )
+ return self.extra_context
@chrisjones-brack3t
chrisjones-brack3t Feb 28, 2014 Member

I'd like to see some type checking before returning whatever may be set on extra_context. Should make sure the value is a dict. Example

@kennethlove
kennethlove Feb 28, 2014 Member

So long as extra_context is a dict, list/tuple of two-tuples, or some sort of object that acts like a dict, the update will work correctly. It'd be nice to not have to check for a specific type.

Maybe do the kwargs.update(self.get_extra_context()) in a try that catches TypeError?

>>> d = {}
>>> d.update([1, 2, 3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a sequence
@chrisjones-brack3t chrisjones-brack3t commented on the diff Feb 28, 2014
docs/other.rst
+.. _ExtraContextMixin:
+
+ExtraContextMixin
+-----------------
+
+The ``ExtraContextMixin`` allows us to *statically* or *programmatically* add statically to the context which is useful in cases like navigation highlighting.
+
+
+::
+
+ # views.py
+
+ from braces.views import ExtraContextMixin
+
+
+ class ContextTemplateView(SetHeadlineMixin, TemplateView):
@chrisjones-brack3t
chrisjones-brack3t Feb 28, 2014 Member

Docs are wrong. You import the mixin but never actually use it on the view.

@kennethlove kennethlove commented on the diff Mar 1, 2014
braces/views.py
@@ -401,6 +401,28 @@ def get_headline(self):
return self.headline
+class ExtraContextMixin(object):
+ """
+ Mixin allows you to set extra context through a static property on the
+ class or programmatically by overloading the get_extra_context method.
+ """
+ extra_context = None
+
+ def get_context_data(self, **kwargs):
+ kwargs = super(ExtraContextMixin, self).get_context_data(**kwargs)
+ kwargs.update(self.get_extra_context())
@kennethlove
kennethlove Mar 1, 2014 Member

I'd change this line to:

try:
    kwargs.update(self.get_extra_context())
except TypeError:
    raise TypeError('extra_context must be a dictionary or container of two-tuples')
else:
    return kwargs
@kennethlove kennethlove commented on the diff Mar 1, 2014
tests/views.py
@@ -174,6 +174,14 @@ class HeadlineView(views.SetHeadlineMixin, TemplateView):
headline = "Test headline"
+class ContextView(views.ExtraContextMixin, TemplateView):
+ """
+ View for testing ExtraContextMixin.
+ """
+ template_name = 'blank.html'
+ extra_context = {'test': True}
@kennethlove
kennethlove Mar 1, 2014 Member

Also let's add some tests for extra_context being a list of two-tuples and a list of single items (which should throw the TypeError).

@kennethlove kennethlove added this to the 1.4 milestone Mar 1, 2014
@chrisjones-brack3t chrisjones-brack3t self-assigned this Mar 1, 2014
@kitsunde
Contributor
kitsunde commented Mar 3, 2014

Thanks for the comments and the cleanup. Sorry for not getting around to fixing my own pull request. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment