Created
June 9, 2011 21:36
-
-
Save jamstooks/1017808 to your computer and use it in GitHub Desktop.
Mixin Class extending Django generic class-based views to provide a persistent custom context. Used mostly for using the request to access an object. Snippet included that uses TemplateWithContextMixin to add the requested org (from /path/to/org) to the t
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.generic.base import TemplateResponseMixin | |
from django.shortcuts import get_object_or_404 | |
from django.http import Http404 | |
from leaguejam.apps.menus.manage_org import manage_org_menu | |
from leaguejam.apps.accounts.models import Org | |
class TemplateWithContextMixin(TemplateResponseMixin): | |
""" | |
Class Based view that stores a `custom_context` property that extends | |
the context | |
`updater_list` stores a list of set_methods that are used to update context | |
in the event that the get/set methods aren't called | |
""" | |
updater_list = [] | |
def __init__(self, *args, **kwargs): | |
""" | |
Wipe the dictionary at every new request | |
""" | |
super(TemplateWithContextMixin, self).__init__(*args, **kwargs) | |
self.custom_context = {} | |
self.updater_list = [] | |
def get_custom_context(self, var): | |
if self.custom_context.has_key(var): | |
return self.custom_context[var] | |
else: | |
return None | |
def set_custom_context(self, var, value): | |
self.custom_context[var] = value | |
def get_updated_context(self, context): | |
""" | |
returns the context with any additional | |
properties in self.custom_context | |
""" | |
_context = {} | |
# run all the access methods | |
for f in self.updater_list: | |
obj = f() | |
_context.update(self.custom_context) | |
_context.update(context) | |
return _context | |
def add_updater(self, method): | |
if method not in self.updater_list: | |
self.updater_list.append(method) | |
def render_to_response(self, context, **response_kwargs): | |
_context = self.get_updated_context(context) | |
return super(TemplateWithContextMixin, self).render_to_response(_context, **response_kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrgContextMixin(TemplateWithContextMixin): | |
""" | |
Defines a get_org and set_org that can be mixed-in to any view | |
""" | |
def __init__(self, *args, **kwargs): | |
super(OrgContextMixin, self).__init__(*args, **kwargs) | |
self.add_updater(self.get_org) | |
def get_org(self): | |
org = self.get_custom_context('org') | |
if not org: | |
org = get_object_or_404(Org, acronym=self.kwargs['org_slug']) | |
self.set_org(org) | |
return org | |
def set_org(self, org): | |
self.set_custom_context('org', org) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment