~openerp/openobject-client-web/ofc2

« back to all changes in this revision

Viewing changes to demo/demo/utils.py

  • Committer: ame (Tiny/Axelor)
  • Date: 2009-03-31 12:13:47 UTC
  • Revision ID: ame@tinyerp.com-20090331121347-1gqpx68e2972ajai
[IMP] demo, Genshi was replaced with Mako

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
import cherrypy
5
5
import simplejson
6
6
 
7
 
from genshi.template import TemplateLoader
 
7
from mako.template import Template
8
8
 
9
9
 
10
10
def find_resource(package_or_module, *names):
17
17
    return os.path.abspath(os.path.join(os.path.dirname(ref.__file__), *names))
18
18
 
19
19
 
20
 
__loader = TemplateLoader(auto_reload=True)
21
 
 
22
 
 
23
20
def load_template(template, module=None):
24
21
 
25
22
    if not template:
30
27
    else:
31
28
        template = os.path.abspath(template)
32
29
 
33
 
    return __loader.load(template)
34
 
 
35
 
 
36
 
 
37
 
def expose(format='xhtml', template=None, content_type='text/html'):
 
30
    return Template(filename=template)
 
31
 
 
32
 
 
33
def expose(format='html', template=None, content_type='text/html'):
 
34
 
 
35
    if format == 'json':
 
36
        content_type = 'text/javascript'
38
37
 
39
38
    def expose_wrapper(func):
40
39
 
43
42
            tmpl = load_template(template, func.__module__)
44
43
            res = func(*args, **kw)
45
44
 
 
45
            cherrypy.response.headers['content-type'] = content_type
 
46
 
46
47
            if tmpl:
47
 
                stream = tmpl.generate(**res)
48
 
                return stream.render(format)
 
48
                res = tmpl.render(**res)
49
49
 
50
50
            if format == 'json':
51
 
                cherrypy.response.headers['content-type'] = 'text/javascript'
52
51
                return simplejson.dumps(res)
53
 
            
 
52
 
54
53
            return str(res)
55
54
 
56
55
        func_wrapper.func_name = func.func_name