from typing import List, Dict from urllib.parse import quote_plus from flask import url_for, current_app import wrpylib.wrvalidators from wrpylib.mwmarkup import create_template from wrpylib.wrmwmarkup import create_wrmap from wrpylib.wrvalidators import LonLat, lonlat_to_str class PylonsHelper: def url(self, filename=None, controller=None, action=None, **kwargs): if filename is not None and filename.startswith('/'): filename = filename[1:] return url_for('static', filename=filename) if controller == 'rodelbahn': if action == 'index': return url_for('index') return url_for('{}_{}'.format(controller, action), **kwargs) def wiki(self, page_title=None): """Creates a link to the specified page in the www.winterrodeln.org wiki""" if page_title is None: page_title = 'Hauptseite' return current_app.config['MEDIAWIKI_WIKI'] + '/' + page_title def forum(self, forum=None): """Creates a link to the specified forum. If no id is given, a general link is created.""" if forum is None: return 'https://winterrodeln-forum.org/' return 'https://winterrodeln-forum.org/viewforum.php?f={}'.format(forum) def mediawiki_index_url(self): return f'{current_app.config["MEDIAWIKI_ROOT"]}/index.php' def sledrun_json_schema_url(self, raw: bool = True): url = f'{self.mediawiki_index_url()}?title=Winterrodeln:Datenschema/Rodelbahn/V1.json' if raw: url += '&action=raw' return url def sledrun_json_title(self, page_title: str) -> str: return f'{page_title}/Rodelbahn.json' def sledrun_json_url(self, page_title, raw: bool = True) -> str: json_page_title = self.sledrun_json_title(page_title) url = f'{self.mediawiki_index_url()}?title={quote_plus(json_page_title)}' if raw: url += '&action=raw' return url def sledrun_map_title(self, page_title: str) -> str: return f'{page_title}/Landkarte.json' def sledrun_map_url(self, page_title, raw: bool = True) -> str: map_page_title = self.sledrun_map_title(page_title) url = f'{self.mediawiki_index_url()}?title={quote_plus(map_page_title)}' if raw: url += '&action=raw' print(url) return url def create_wrmap(self, geojson: Dict) -> str: return create_wrmap(geojson) def google_maps(self, latitude, longitude): """Builds an URL like http://maps.google.at/maps?q=47.200607,11.260007""" return "https://maps.google.at/maps?q=%.6f,%.6f" % (latitude, longitude) def bool(self, value): """Takes a bool value and creates a German representation""" return wrpylib.wrvalidators.opt_bool_german_to_str(value) def tristate_tuple(self, yes, no): """Takes a German representation of a tristate value""" return {(True, True): 'Teilweise', (True, False): 'Ja', (False, True): 'Nein'}.get((yes, no), None) def tristate_float(self, value): """Takes a German representation of a tristate value""" return wrpylib.wrvalidators.opt_tristate_german_to_str(value) def public_transport(self, value): return wrpylib.wrvalidators.opt_public_transport_german_to_str(value) def json_position(self, value: dict) -> str: lon_lat = LonLat(value['longitude'], value['latitude']) return lonlat_to_str(lon_lat) def json_pos_ele_position(self, value: dict) -> str: pos = value.get('position') if pos is None: return '' return self.json_position(pos) def json_pos_ele_elevation(self, value: dict) -> str: return value.get('elevation', '') def list_template(self, name: str, value: List[str]) -> str: return str(create_template(name, value)) def json_template(self, value) -> str: args = [] kwargs = {} for p in value.get('parameter', []): v = p.get('value', '') if 'key' in p: kwargs[p['key']] = v else: args.append(v) return str(create_template(value['name'], args, kwargs))