]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/template_helper.py
b9c1dda9edaf10c0be36a833ba5554a60dc4fd45
[philipp/winterrodeln/wradmin.git] / wradmin / template_helper.py
1 from typing import List
2 from urllib.parse import quote_plus
3
4 from flask import url_for, current_app
5 import wrpylib.wrvalidators
6 from wrpylib.mwmarkup import create_template
7 from wrpylib.wrvalidators import LonLat, lonlat_to_str
8
9
10 class PylonsHelper:
11     def url(self, filename=None, controller=None, action=None, **kwargs):
12         if filename is not None and filename.startswith('/'):
13             filename = filename[1:]
14             return url_for('static', filename=filename)
15         if controller == 'rodelbahn':
16             if action == 'index':
17                 return url_for('index')
18         return url_for('{}_{}'.format(controller, action), **kwargs)
19
20     def wiki(self, page_title=None):
21         """Creates a link to the specified page in the www.winterrodeln.org wiki"""
22         if page_title is None:
23             page_title = 'Hauptseite'
24         return current_app.config['MEDIAWIKI_WIKI'] + '/' + page_title
25
26     def forum(self, forum=None):
27         """Creates a link to the specified forum. If no id is given, a general link is created."""
28         if forum is None:
29             return 'https://winterrodeln-forum.org/'
30         return 'https://winterrodeln-forum.org/viewforum.php?f={}'.format(forum)
31
32     def mediawiki_index_url(self):
33         return f'{current_app.config["MEDIAWIKI_ROOT"]}/index.php'
34
35     def sledrun_json_schema_url(self, raw: bool = True):
36         url = f'{self.mediawiki_index_url()}?title=Winterrodeln:Datenschema/Rodelbahn/V1.json'
37         if raw:
38             url += '&action=raw'
39         return url
40
41     def sledrun_json_url(self, page_title, raw: bool = True):
42         url = f'{self.mediawiki_index_url()}?title={quote_plus(page_title)}/Rodelbahn.json'
43         if raw:
44             url += '&action=raw'
45         return url
46
47     def google_maps(self, latitude, longitude):
48         """Builds an URL like http://maps.google.at/maps?q=47.200607,11.260007"""
49         return "https://maps.google.at/maps?q=%.6f,%.6f" % (latitude, longitude)
50
51     def bool(self, value):
52         """Takes a bool value and creates a German representation"""
53         return wrpylib.wrvalidators.opt_bool_german_to_str(value)
54
55     def tristate_tuple(self, yes, no):
56         """Takes a German representation of a tristate value"""
57         return {(True, True): 'Teilweise', (True, False): 'Ja', (False, True): 'Nein'}.get((yes, no), None)
58
59     def tristate_float(self, value):
60         """Takes a German representation of a tristate value"""
61         return wrpylib.wrvalidators.opt_tristate_german_to_str(value)
62
63     def public_transport(self, value):
64         return wrpylib.wrvalidators.opt_public_transport_german_to_str(value)
65
66     def json_position(self, value: dict) -> str:
67         lon_lat = LonLat(value['longitude'], value['latitude'])
68         return lonlat_to_str(lon_lat)
69
70     def json_pos_ele_position(self, value: dict) -> str:
71         pos = value.get('position')
72         if pos is None:
73             return ''
74         return self.json_position(pos)
75
76     def json_pos_ele_elevation(self, value: dict) -> str:
77         return value.get('elevation', '')
78
79     def list_template(self, name: str, value: List[str]) -> str:
80         return str(create_template(name, value))
81
82     def json_template(self, value) -> str:
83         args = []
84         kwargs = {}
85         for p in value.get('parameter', []):
86             v = p.get('value', '')
87             if 'key' in p:
88                 kwargs[p['key']] = v
89             else:
90                 args.append(v)
91         return str(create_template(value['name'], args, kwargs))