Fixed template loading paths to don't depend on APP_ROOT
[bcms.git] / template.py
blob5aff07cebb56769dc89e70d27b7b04f64ce3367e
1 # BC CMS - Content Management System
2 # Copyright (c) 2008, Carlos Daniel Ruvalcaba Valenzuela
3 # All rights reserved.
5 # Redistribution and use in source and binary forms, with or without modification,
6 # are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright notice, this
9 # list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright notice, this
11 # list of conditions and the following disclaimer in the documentation and/or other
12 # materials provided with the distribution.
13 # * Neither the name of the BlackChair Software nor the names of its contributors may be
14 # used to endorse or promote products derived from this software without specific prior
15 # written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 # FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 from django.http import HttpResponse
27 from django.template import RequestContext
28 from django.template.loader import render_to_string
30 # with thanks to Peter Hunt (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465427)
31 decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda func: \
32 decorator(func, *args, **kwargs)
34 # with thanks to Robert Thomson (http://www.djangosnippets.org/snippets/459/)
35 @decorator_with_args
36 def template_django(func, template=None):
37 def DJANGO_TEMPLATE(request, *args, **kwargs):
38 res = func(request, *args, **kwargs)
39 if not res:
40 res = {}
41 if type(res) == dict:
42 try:
43 return HttpResponse(render_to_string(template, res, RequestContext(request)))
44 except:
45 #if settings.DEBUG:
46 #return HttpResponse("Templating Error", status=500)
47 raise
48 else:
49 return res
50 DJANGO_TEMPLATE.__name__ = func.__name__
51 DJANGO_TEMPLATE.__doc__ = func.__doc__
52 DJANGO_TEMPLATE.__dict__ = func.__dict__
53 return DJANGO_TEMPLATE