]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/genshi.py
2b6c5d023a19383307056b88a6264d6fedec4abd
[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, get_flashed_messages
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 google_maps(self, latitude, longitude):
31         """Builds an URL like http://maps.google.at/maps?q=47.200607,11.260007"""
32         return "http://maps.google.at/maps?q=%.6f,%.6f" % (latitude, longitude)
33
34     def bool(self, value):
35         """Takes a bool value and creates a German representation"""
36         return wrpylib.wrvalidators.opt_bool_german_to_str(value)
37
38     def tristate_tuple(self, yes, no):
39         """Takes a German representation of a tristate value"""
40         return {(True, True): 'Teilweise', (True, False): 'Ja', (False, True): 'Nein'}.get((yes, no), None)
41
42     def tristate_float(self, value):
43         """Takes a German representation of a tristate value"""
44         return wrpylib.wrvalidators.opt_tristate_german_to_str(value)
45
46     def public_transport(self, value):
47         return wrpylib.wrvalidators.opt_public_transport_german_to_str(value)
48
49
50 class TemplateContext:
51     pass
52
53
54 def render_genshi_template(template, **kwargs):
55     loader = genshi.template.TemplateLoader(os.path.join(os.path.dirname(__file__), 'templates'), auto_reload=True)
56     tmpl = loader.load(template)
57     genshi_kwargs = {'h': PylonsHelper(), 'HTML': HTML, 'get_flashed_messages': get_flashed_messages}
58     genshi_kwargs.update(kwargs)
59     return tmpl.generate(**genshi_kwargs).render('html', doctype='html')