lib/template.py
author Tero Marttila <terom@fixme.fi>
Sun, 08 Feb 2009 02:55:53 +0200
branchsites
changeset 184 a3d9aa76790d
parent 183 8c8d082c4657
permissions -rw-r--r--
implement channel_view count, the query stuff, css, layout all need some cleanup :(
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Template handler
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
# use Mako
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
from mako import exceptions
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
from mako.template import Template
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
     8
import mako.lookup
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
# for http.ResponseError
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
import http
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
167
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    13
import helpers
f2504700e273 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents: 163
diff changeset
    14
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
# path to template files
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
TEMPLATE_DIR = "templates"
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
# path to cached templates
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
CACHE_DIR = "cache/templates"
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
# template file extension
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
TEMPLATE_EXT = "tmpl"
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
class TemplateError (http.ResponseError) :
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
        Raised by the template module functions
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    pass
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    31
class TemplateLoader (mako.lookup.TemplateLookup) :
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    """
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    33
        Our own specialization of mako's TemplateLookup
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    """
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    36
    def __init__ (self, path, fileext=TEMPLATE_EXT, **env) :
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    37
        """
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    38
            Initialize to load templates located at path, with the given file extension.
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    39
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    40
            The given **env list is supplied to every template render
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    41
        """
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    43
        # store
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    44
        self.path = path
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    45
        self.fileext = fileext
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    46
        self.env = env
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    47
            
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    48
        # build the TemplateLookup
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    49
        super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR)
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    50
    
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    51
    @staticmethod
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    52
    def _render (tpl, env, params) :
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    53
        """
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    54
            Render the given template with given env/params, returning the output as a unicode string, or raising a TemplateError
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    55
        """
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    56
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    57
        # build the context from our superglobals, env, and params
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    58
        ctx = dict(
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    59
            h       = helpers,
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    60
        )
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    61
        ctx.update(env)
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    62
        ctx.update(params)
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    63
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    64
        try :
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    65
            return tpl.render_unicode(**ctx)
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    66
        
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    67
        # a template may render other templates
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    68
        except TemplateError :
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    69
            raise
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    70
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    71
        except :
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    72
            details = exceptions.text_error_template().render()
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    73
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    74
            raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    75
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    77
    def lookup (self, name) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    78
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    79
            Looks up a template based on the bare "name", which does not include the path or file extension
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    80
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    81
        
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    82
        try :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    83
            return self.get_template("%s.%s" % (name, self.fileext))
150
817a8bb1cdc6 and it works, a lot better than before
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    85
        except :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    86
            raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    87
    
181
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    88
    def render (self, name, **params) :
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    89
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    90
            Render a template, using lookup() on the given name
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    91
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    92
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
    93
        return self._render(self.lookup(name), self.env, params)
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
    94
181
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    95
    def render_to_response (self, name, **params) :
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    96
        """
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    97
            Render a template, returning a http.Response object
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    98
        """
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
    99
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
   100
        return http.Response(self.render(name, **params))
54d0edb81cb8 the handlers work now
Tero Marttila <terom@fixme.fi>
parents: 173
diff changeset
   101
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   102
    @classmethod
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   103
    def load (cls, path) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   104
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   105
            Loads a template from a specific file
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   106
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   107
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   108
        try :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   109
            return Template(filename=path, module_directory=CACHE_DIR)
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   110
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   111
        except :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   112
            raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   113
    
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   114
    @classmethod
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   115
    def render_file (cls, path, **params) :
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   116
        """
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   117
            Render a template, using load() on the given path. No global environment vars are defined for the render.
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   118
        """
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   119
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   120
        return cls._render(cls.load(path), dict(), params)
173
f1d36d7b361e move layout.tmpl to sites/www.qmsk.net
Tero Marttila <terom@fixme.fi>
parents: 167
diff changeset
   121
183
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   122
    @classmethod
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   123
    def render_template (cls, template, **params) :
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   124
        """
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   125
            Render the given template object. No global environment vars are defined for the render.
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   126
        """
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   127
        
8c8d082c4657 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 181
diff changeset
   128
        return cls._render(template, dict(), params)