[docs]defget_locale()->str|None:""" utility for getting the lang from the ``Language-Accept`` header :returns: the language key - either ``de`` or ``en`` """# the matching request.accept_languages.best_match algo is not great and# since we only have german and english avoid itifany(lang.startswith('de')forlang,_inrequest.accept_languages):return'de'else:return'en'
[docs]@isithot.route('/')defindex()->Response:"""A simple route to have nicer link to share."""returnredirect(url_for('isithot.plots',station=list(current_app.config['DATA_PROVIDERS'].keys())[0],),)
def_i18n_cache_key(**kwargs:str)->str:"""Custom function for generating a cache-key based on strings passed as keyword arguments :param kwargs: any number of keyword arguments that are strings """returnf'{"".join(kwargs.values())}-{get_locale()}'
[docs]@isithot.route('/<station>')@cache.cached(timeout=300,make_cache_key=_i18n_cache_key)defplots(station:str)->str:""" Renders the isithot page with all plots. This route is cached since compiling the data and generating the plots is quite expensive. The cache expires after 5 minutes hence it is still almost live data. :param station: The station a plot is created for. """ifstationnotincurrent_app.config['DATA_PROVIDERS'].keys():abort(404)provider:DataProvider=current_app.config['DATA_PROVIDERS'][station]data=provider.prepare_data(d=date.today())distrib_graph=provider.distrib_fig(data)hist_graph=provider.hist_fig(data)calender_graph=provider.calendar_fig(data.calendar_data)returnrender_template('index.html',distrib_graph=distrib_graph.to_json(engine='json'),hist_graph=hist_graph.to_json(engine='json'),calender_graph=calender_graph.to_json(engine='json'),station=provider,plot_data=data,data_providers=current_app.config['DATA_PROVIDERS'],)
[docs]@isithot.route('/other-years/<station>/<int:year>')# we can cache this indefinitely since it's totally static@cache.cached()deflast_years_calendar(station:str,year:int)->str:""" Returns the calendar figure data as ``json`` for the specified year. This route is cached indefinitely and does not take the locale into account, since it's only static data. :param station: The station a plot is created for. :param year: The year a plot is created for. """ifstationnotincurrent_app.config['DATA_PROVIDERS'].keys():abort(404)provider:DataProvider=current_app.config['DATA_PROVIDERS'][station]ifnot(provider.min_year<=year<=date.today().year):abort(400)_,calendar_data=provider.prepare_daily_and_calendar_data(d=date(year,1,1),)fig=provider.calendar_fig(calendar_data)returnResponse(fig.to_json(engine='json'),mimetype='application/json')