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