Updated model names
[bcms.git] / template.py
blobc4929b59fa431a6a5be64ea11518bc07299837d6
1 from django.http import HttpResponse
2 from django.template import RequestContext
3 from django.template.loader import render_to_string
5 # with thanks to Peter Hunt (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465427)
6 decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda func: \
7 decorator(func, *args, **kwargs)
9 # with thanks to Robert Thomson (http://www.djangosnippets.org/snippets/459/)
10 @decorator_with_args
11 def template_django(func, template=None):
12 def DJANGO_TEMPLATE(request, *args, **kwargs):
13 res = func(request, *args, **kwargs)
14 if not res:
15 res = {}
16 if type(res) == dict:
17 try:
18 return HttpResponse(render_to_string(template, res, RequestContext(request)))
19 except:
20 #if settings.DEBUG:
21 #return HttpResponse("Templating Error", status=500)
22 raise
23 else:
24 return res
25 DJANGO_TEMPLATE.__name__ = func.__name__
26 DJANGO_TEMPLATE.__doc__ = func.__doc__
27 DJANGO_TEMPLATE.__dict__ = func.__dict__
28 return DJANGO_TEMPLATE