1 from typing import List, Dict
2 from urllib.parse import quote_plus
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
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':
18 return url_for('index')
19 return url_for('{}_{}'.format(controller, action), **kwargs)
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
27 def forum(self, forum=None):
28 """Creates a link to the specified forum. If no id is given, a general link is created."""
30 return 'https://winterrodeln-forum.org/'
31 return 'https://winterrodeln-forum.org/viewforum.php?f={}'.format(forum)
33 def mediawiki_index_url(self):
34 return f'{current_app.config["MEDIAWIKI_ROOT"]}/index.php'
36 def sledrun_json_schema_url(self, raw: bool = True):
37 url = f'{self.mediawiki_index_url()}?title=Winterrodeln:Datenschema/Rodelbahn/V1.json'
42 def sledrun_json_title(self, page_title: str) -> str:
43 return f'{page_title}/Rodelbahn.json'
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)}'
52 def sledrun_map_title(self, page_title: str) -> str:
53 return f'{page_title}/Landkarte.json'
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)}'
63 def create_wrmap(self, geojson: Dict) -> str:
64 return create_wrmap(geojson)
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)
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)
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)
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)
82 def public_transport(self, value):
83 return wrpylib.wrvalidators.opt_public_transport_german_to_str(value)
85 def json_position(self, value: dict) -> str:
86 lon_lat = LonLat(value['longitude'], value['latitude'])
87 return lonlat_to_str(lon_lat)
89 def json_pos_ele_position(self, value: dict) -> str:
90 pos = value.get('position')
93 return self.json_position(pos)
95 def json_pos_ele_elevation(self, value: dict) -> str:
96 return value.get('elevation', '')
98 def list_template(self, name: str, value: List[str]) -> str:
99 return str(create_template(name, value))
101 def json_template(self, value) -> str:
104 for p in value.get('parameter', []):
105 v = p.get('value', '')
110 return str(create_template(value['name'], args, kwargs))