2005-09-24 05:40:04 +08:00
|
|
|
# This module collects helper functions and classes that "span" multiple levels
|
|
|
|
# of MVC. In other words, these functions/classes introduce controlled coupling
|
|
|
|
# for convenience's sake.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-12-24 12:39:59 +08:00
|
|
|
from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist
|
2005-10-15 04:10:13 +08:00
|
|
|
from django.core.template import Context, loader
|
2005-12-24 12:39:59 +08:00
|
|
|
from django.conf.settings import TEMPLATE_CONTEXT_PROCESSORS
|
2005-09-22 01:24:33 +08:00
|
|
|
from django.utils.httpwrappers import HttpResponse
|
|
|
|
|
2005-12-24 12:39:59 +08:00
|
|
|
_standard_context_processors = None
|
|
|
|
|
|
|
|
# This is a function rather than module-level procedural code because we only
|
|
|
|
# want it to execute if somebody uses DjangoContext.
|
|
|
|
def get_standard_processors():
|
|
|
|
global _standard_context_processors
|
|
|
|
if _standard_context_processors is None:
|
|
|
|
processors = []
|
|
|
|
for path in TEMPLATE_CONTEXT_PROCESSORS:
|
|
|
|
i = path.rfind('.')
|
|
|
|
module, attr = path[:i], path[i+1:]
|
|
|
|
try:
|
|
|
|
mod = __import__(module, '', '', [attr])
|
|
|
|
except ImportError, e:
|
|
|
|
raise ImproperlyConfigured, 'Error importing request processor module %s: "%s"' % (module, e)
|
|
|
|
try:
|
|
|
|
func = getattr(mod, attr)
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable request processor' % (module, attr)
|
|
|
|
processors.append(func)
|
|
|
|
_standard_context_processors = tuple(processors)
|
|
|
|
return _standard_context_processors
|
|
|
|
|
2005-09-22 22:57:05 +08:00
|
|
|
def render_to_response(*args, **kwargs):
|
2005-10-15 04:10:13 +08:00
|
|
|
return HttpResponse(loader.render_to_string(*args, **kwargs))
|
2005-09-22 22:57:05 +08:00
|
|
|
load_and_render = render_to_response # For backwards compatibility.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-09-24 05:40:04 +08:00
|
|
|
def get_object_or_404(mod, **kwargs):
|
|
|
|
try:
|
|
|
|
return mod.get_object(**kwargs)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
def get_list_or_404(mod, **kwargs):
|
|
|
|
obj_list = mod.get_list(**kwargs)
|
|
|
|
if not obj_list:
|
|
|
|
raise Http404
|
|
|
|
return obj_list
|
|
|
|
|
2005-07-16 01:09:01 +08:00
|
|
|
class DjangoContext(Context):
|
|
|
|
"""
|
2005-12-24 12:39:59 +08:00
|
|
|
This subclass of template.Context automatically populates itself using
|
|
|
|
the processors defined in TEMPLATE_CONTEXT_PROCESSORS.
|
|
|
|
Additional processors can be specified as a list of callables
|
|
|
|
using the "processors" keyword argument.
|
2005-07-16 01:09:01 +08:00
|
|
|
"""
|
2005-12-24 12:39:59 +08:00
|
|
|
def __init__(self, request, dict=None, processors=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
Context.__init__(self, dict)
|
2005-12-24 12:39:59 +08:00
|
|
|
if processors is None:
|
|
|
|
processors = ()
|
2005-11-04 12:59:46 +08:00
|
|
|
else:
|
2005-12-24 12:39:59 +08:00
|
|
|
processors = tuple(processors)
|
|
|
|
for processor in get_standard_processors() + processors:
|
|
|
|
self.update(processor(request))
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
# PermWrapper and PermLookupDict proxy the permissions system into objects that
|
|
|
|
# the template system can understand.
|
|
|
|
|
|
|
|
class PermLookupDict:
|
|
|
|
def __init__(self, user, module_name):
|
|
|
|
self.user, self.module_name = user, module_name
|
|
|
|
def __repr__(self):
|
2005-07-14 11:17:09 +08:00
|
|
|
return str(self.user.get_permission_list())
|
2005-07-13 09:25:57 +08:00
|
|
|
def __getitem__(self, perm_name):
|
|
|
|
return self.user.has_perm("%s.%s" % (self.module_name, perm_name))
|
|
|
|
def __nonzero__(self):
|
|
|
|
return self.user.has_module_perms(self.module_name)
|
|
|
|
|
|
|
|
class PermWrapper:
|
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
|
|
|
def __getitem__(self, module_name):
|
|
|
|
return PermLookupDict(self.user, module_name)
|