Refactor filter arguments outside tile handler

This commit is contained in:
Paul Bienkowski 2022-07-28 14:11:56 +02:00
parent 201db32050
commit 1a3b971a71

View file

@ -1,6 +1,7 @@
from gzip import decompress from gzip import decompress
from sqlite3 import connect from sqlite3 import connect
from datetime import datetime, time, timedelta from datetime import datetime, time, timedelta
from typing import Optional, Tuple
import dateutil.parser import dateutil.parser
from sanic.exceptions import Forbidden, InvalidUsage from sanic.exceptions import Forbidden, InvalidUsage
@ -54,12 +55,17 @@ def round_date(date, to="weeks", up=False):
TILE_CACHE_MAX_AGE = 3600 * 24 TILE_CACHE_MAX_AGE = 3600 * 24
@app.route(r"/tiles/<zoom:int>/<x:int>/<y:(\d+)\.pbf>") def get_filter_options(
async def tiles(req, zoom: int, x: int, y: str): req,
if app.config.get("TILES_FILE"): ) -> Tuple[Optional[str], Optional[datetime], Optional[datetime]]:
tile = get_tile(req.app.config.TILES_FILE, int(zoom), int(x), int(y)) """
Returns parsed, validated and normalized options for filtering map data, a
tuple of
else: * user_id (str|None)
* start (datetime|None)
* end (datetime|None)
"""
user_id = None user_id = None
username = req.ctx.get_single_arg("user", default=None) username = req.ctx.get_single_arg("user", default=None)
if username is not None: if username is not None:
@ -79,6 +85,17 @@ async def tiles(req, zoom: int, x: int, y: str):
"end date must be later than start date (note: dates are rounded to weeks)" "end date must be later than start date (note: dates are rounded to weeks)"
) )
return user_id, start, end
@app.route(r"/tiles/<zoom:int>/<x:int>/<y:(\d+)\.pbf>")
async def tiles(req, zoom: int, x: int, y: str):
if app.config.get("TILES_FILE"):
tile = get_tile(req.app.config.TILES_FILE, int(zoom), int(x), int(y))
else:
user_id, start, end = get_filter_options(req)
tile = await req.ctx.db.scalar( tile = await req.ctx.db.scalar(
text( text(
f"select data from getmvt(:zoom, :x, :y, :user_id, :min_time, :max_time) as b(data, key);" f"select data from getmvt(:zoom, :x, :y, :user_id, :min_time, :max_time) as b(data, key);"