from more.webassets import WebassetsApp
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable, Iterator
from onegov.core.request import CoreRequest
from webob.response import Response
[docs]
class MapboxApp(WebassetsApp):
""" Provides mapbox integration
:class:`onegov.core.framework.Framework` based applications.
Doesn't do much except serve the mapbox public token, so we can store it
in configuration and not with the source. Not that this token is inherently
unsafe and must be the *public* token.
Do not use private tokens!
If we wanted to avoid this we would have to use a mapbox proxy server,
which seems a bit too much. If we detect abuse of the public token we
just switch to a new one. If it must be we can even automatically rotate
the token regularly.
"""
@MapboxApp.webasset_path()
[docs]
def get_js_path() -> str:
return 'assets/js'
@MapboxApp.webasset_path()
[docs]
def get_css_path() -> str:
return 'assets/css'
@MapboxApp.webasset('leaflet', filters={'css': ['datauri', 'custom-rcssmin']})
[docs]
def get_leaflet_asset() -> 'Iterator[str]':
yield 'leaflet.css'
yield 'leaflet-easybutton.css'
yield 'leaflet-control-geocoder.css'
yield 'leaflet-control-locate.css'
yield 'leaflet-integration.css'
yield 'leaflet.js'
yield 'leaflet-sleep.js'
yield 'leaflet-easybutton.js'
yield 'leaflet-control-geocoder.js'
yield 'leaflet-control-locate.js'
yield 'leaflet-integration.js'
@MapboxApp.webasset('proj4js')
[docs]
def get_proj4js_asset() -> 'Iterator[str]':
yield 'proj4js.js'
yield 'proj4js-leaflet.js'
@MapboxApp.webasset('geo-mapbox')
[docs]
def get_geo_mapbox() -> 'Iterator[str]':
yield 'leaflet'
@MapboxApp.webasset('geo-vermessungsamt-winterthur')
[docs]
def get_geo_vermessungsamt_winterthur() -> 'Iterator[str]':
yield 'leaflet'
yield 'proj4js'
yield 'geo-vermessungsamt-winterthur.js'
@MapboxApp.webasset('geo-zugmap-basisplan')
[docs]
def get_geo_zugmap_basisplan() -> 'Iterator[str]':
yield 'leaflet'
yield 'proj4js'
yield 'geo-zugmap.js'
@MapboxApp.webasset('geo-zugmap-orthofoto')
[docs]
def get_geo_zugmap_orthofoto() -> 'Iterator[str]':
yield 'leaflet'
yield 'proj4js'
yield 'geo-zugmap-orthofoto.js'
@MapboxApp.webasset('geo-bs')
[docs]
def get_geo_bs() -> 'Iterator[str]':
yield 'leaflet'
yield 'proj4js'
yield 'geo-bs.js'
@MapboxApp.webasset('geo-admin')
[docs]
def get_geo_admin() -> 'Iterator[str]':
yield 'leaflet'
yield 'proj4js'
yield 'geo-admin.js'
@MapboxApp.webasset('geo-admin-aerial')
[docs]
def get_geo_admin_aerial() -> 'Iterator[str]':
yield 'leaflet'
yield 'proj4js'
yield 'geo-admin-aerial.js'
@MapboxApp.tween_factory()
[docs]
def inject_mapbox_api_token_tween_factory(
app: MapboxApp,
handler: 'Callable[[CoreRequest], Response]'
) -> 'Callable[[CoreRequest], Response]':
replacement = '<body data-mapbox-token="{}"'.format(app.mapbox_token)
replacement_b = replacement.encode('utf-8')
def inject_mapbox_api_token_tween(request: 'CoreRequest') -> 'Response':
response = handler(request)
if getattr(request.app, 'mapbox_token', None):
response.body = response.body.replace(
b'<body',
replacement_b,
1 # only replace the first occurrence
)
return response
return inject_mapbox_api_token_tween