1 from typing import List
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.wrvalidators import LonLat, lonlat_to_str
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':
17 return url_for('index')
18 return url_for('{}_{}'.format(controller, action), **kwargs)
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
26 def forum(self, forum=None):
27 """Creates a link to the specified forum. If no id is given, a general link is created."""
29 return 'https://winterrodeln-forum.org/'
30 return 'https://winterrodeln-forum.org/viewforum.php?f={}'.format(forum)
32 def mediawiki_index_url(self):
33 return f'{current_app.config["MEDIAWIKI_ROOT"]}/index.php'
35 def sledrun_json_schema_url(self, raw: bool = True):
36 url = f'{self.mediawiki_index_url()}?title=Winterrodeln:Datenschema/Rodelbahn/V1.json'
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'
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)
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)
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)
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)
63 def public_transport(self, value):
64 return wrpylib.wrvalidators.opt_public_transport_german_to_str(value)
66 def json_position(self, value: dict) -> str:
67 lon_lat = LonLat(value['longitude'], value['latitude'])
68 return lonlat_to_str(lon_lat)
70 def json_pos_ele_position(self, value: dict) -> str:
71 pos = value.get('position')
74 return self.json_position(pos)
76 def json_pos_ele_elevation(self, value: dict) -> str:
77 return value.get('elevation', '')
79 def list_template(self, name: str, value: List[str]) -> str:
80 return str(create_template(name, value))
82 def json_template(self, value) -> str:
85 for p in value.get('parameter', []):
86 v = p.get('value', '')
91 return str(create_template(value['name'], args, kwargs))