]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/genshi.py
6f1094c72f1d77d9b8b67f75dfc5c19801bf0e70
[philipp/winterrodeln/wradmin.git] / wradmin / genshi.py
1 import os
2 import genshi.template
3 from genshi import HTML
4 from flask import url_for
5 import wrpylib.wrvalidators
6
7
8 class PylonsHelper:
9     def url(self, filename=None, controller=None, action=None, **kwargs):
10         if filename is not None and filename.startswith('/'):
11             filename = filename[1:]
12             return url_for('static', filename=filename)
13         if controller == 'rodelbahn':
14             if action == 'index':
15                 return url_for('index')
16         return url_for('{}_{}'.format(controller, action), **kwargs)
17
18     def wiki(self, page_title=None):
19         """Creates a link to the specified page in the www.winterrodeln.org wiki"""
20         if page_title is None:
21             page_title = 'Hauptseite'
22         return 'http://www.winterrodeln.org/wiki/' + page_title
23
24     def forum(self, forum=None):
25         """Creates a link to the specified forum. If no id is given, a general link is created."""
26         if forum is None:
27             return 'http://winterrodeln-forum.org/'
28         return 'http://winterrodeln-forum.org/viewforum.php?f={}'.format(forum)
29
30     def bool(self, value):
31         """Takes a bool value and creates a German representation"""
32         return wrpylib.wrvalidators.opt_bool_german_to_str(value)
33
34     def tristate_tuple(self, yes, no):
35         """Takes a German representation of a tristate value"""
36         return {(True, True): 'Teilweise', (True, False): 'Ja', (False, True): 'Nein'}.get((yes, no), None)
37
38     def tristate_float(self, value):
39         """Takes a German representation of a tristate value"""
40         return wrpylib.wrvalidators.opt_tristate_german_to_str(value)
41
42     def public_transport(self, value):
43         return wrpylib.wrvalidators.opt_public_transport_german_to_str(value)
44
45
46 class FakeSession:
47     def has_key(self, key):
48         return False
49
50     def save(self):
51         pass
52
53
54 class TemplateContext:
55     pass
56
57
58 def render_genshi_template(template, **kwargs):
59     loader = genshi.template.TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True)
60     tmpl = loader.load(template)
61     genshi_kwargs = {'h': PylonsHelper(), 'session': FakeSession(), 'HTML': HTML}
62     genshi_kwargs.update(kwargs)
63     return tmpl.generate(**genshi_kwargs).render('html', doctype='html')