helpers.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 19:08:17 +0200
changeset 78 a46d2fc07951
parent 50 e4fbf480fbee
permissions -rw-r--r--
add test for tree_parse filesystem stuff
26
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Helper functions for use in templates
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
47
99c45fc13edc improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
     5
import time, cgi
26
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
46
54c5f5f340de reduce to qmsk.web lib
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     7
from qmsk.web import config
26
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
     9
class Helpers (object) :
26
9d3beac1b196 remove foo.html page, and rice up the footer a bit
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    11
        A bunch of useful helper methods for use in templates
42
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 29
diff changeset
    12
    """
5a72c00c4ae4 more fiddling around with the irclogs layout/css, add query args to URL
Tero Marttila <terom@fixme.fi>
parents: 29
diff changeset
    13
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    14
    def _bind_ctx (self, ctx) :
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    15
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    16
            Initialize with the given render context
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    17
        """
28
b68145b5ce24 breadcrumb, but it's hidden
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    18
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    19
        self.ctx = ctx
28
b68145b5ce24 breadcrumb, but it's hidden
Tero Marttila <terom@fixme.fi>
parents: 26
diff changeset
    20
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    21
    def now (self) :
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    22
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    23
            Returns the current date/time
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    24
        """
47
99c45fc13edc improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    25
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    26
        return time.strftime(config.DATETIME_FMT)
47
99c45fc13edc improved URLTree types (URLType), helpers.escape
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    27
50
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    28
    def copyright_year (self) :
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    29
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    30
            Returns the current year
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    31
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    32
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    33
        return time.strftime("%Y")
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    34
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    35
    def validation_notice (self, site_host) :
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    36
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    37
            Returns a short "Validated XHTML & CSS" link text for the given site hostname
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    38
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    39
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    40
        return 'Validated <a href="http://validator.w3.org/check?uri=http://%(host)s">XHTML 1.0 Strict</a> &amp; <a href="http://jigsaw.w3.org/css-validator/validator?uri=http://%(host)s">CSS 2.1</a>' % dict(
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    41
            host = site_host
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    42
        )
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    43
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    44
    def breadcrumb (self, trail, links=True) :
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    45
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    46
            Returns a nicely formatted breadcrumb tail, optinally with links
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    47
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    48
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    49
        return ' &raquo; '.join(
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    50
            (
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    51
                '<a href="$site_url/%s">%s</a>' % (page.url, page.title) if links else page.title
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    52
            ) for page in trail
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    53
        )
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    54
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    55
    def escape (self, data) :
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    56
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    57
            Escape data as HTML
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    58
        """
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    59
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    60
        return cgi.escape(data)
e4fbf480fbee change templates to remove all class/staticmethods, make Helpers a context-aware class, and fix defaults/URLIntegerType bugs in URLTree
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
    61