diff --git a/CHANGES.md b/CHANGES.md index a731db4..41a8cdc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,10 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin - add `templated=True` in template URL links - add `(Template URL)` in template URL links title - remove *deserialization* in `tipg.factory.create_html_response` function +- add `title` option to `create_html_response` method, in order to set the web page title +- add `**kwargs` to `create_html_response` method to allow custom object to be passed to the template +- fix url/path passed to the HTML template +- fix HTML templates when passing Query Parameters ## [0.6.3] - 2024-02-02 diff --git a/tipg/factory.py b/tipg/factory.py index 147c84e..aa7be8f 100644 --- a/tipg/factory.py +++ b/tipg/factory.py @@ -116,17 +116,28 @@ def create_html_response( data: Any, templates: Jinja2Templates, template_name: str, + title: Optional[str] = None, router_prefix: Optional[str] = None, + **kwargs: Any, ) -> _TemplateResponse: """Create Template response.""" urlpath = request.url.path if root_path := request.app.root_path: urlpath = re.sub(r"^" + root_path, "", urlpath) + if router_prefix: + urlpath = re.sub(r"^" + router_prefix, "", urlpath) + crumbs = [] baseurl = str(request.base_url).rstrip("/") + if router_prefix: + baseurl += router_prefix + crumbpath = str(baseurl) + if urlpath == "/": + urlpath = "" + for crumb in urlpath.split("/"): crumbpath = crumbpath.rstrip("/") part = crumb @@ -135,9 +146,6 @@ def create_html_response( crumbpath += f"/{crumb}" crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()}) - if router_prefix: - baseurl += router_prefix - return templates.TemplateResponse( request, name=f"{template_name}.html", @@ -146,13 +154,12 @@ def create_html_response( "template": { "api_root": baseurl, "params": request.query_params, - "title": "", + "title": title or template_name, }, "crumbs": crumbs, - "url": str(request.url), - "baseurl": baseurl, - "urlpath": str(request.url.path), - "urlparams": str(request.url.query), + "url": baseurl + urlpath, + "params": str(request.url.query), + **kwargs, }, ) @@ -220,13 +227,17 @@ def _create_html_response( request: Request, data: Any, template_name: str, + title: Optional[str] = None, + **kwargs: Any, ) -> _TemplateResponse: return create_html_response( request, data, templates=self.templates, template_name=template_name, + title=title, router_prefix=self.router_prefix, + **kwargs, ) @abc.abstractmethod @@ -338,6 +349,7 @@ def landing( request, data.model_dump(exclude_none=True, mode="json"), template_name="landing", + title=self.title, ) return data @@ -532,6 +544,7 @@ def collections( request, data.model_dump(exclude_none=True, mode="json"), template_name="collections", + title="Collections list", ) return data @@ -619,6 +632,7 @@ def collection( request, data.model_dump(exclude_none=True, mode="json"), template_name="collection", + title=f"{collection.id} collection", ) return data @@ -664,6 +678,7 @@ def queryables( request, data.model_dump(exclude_none=True, mode="json"), template_name="queryables", + title=f"{collection.id} queryables", ) return data @@ -920,6 +935,7 @@ async def items( # noqa: C901 request, orjson.loads(orjsonDumps(data).decode()), template_name="items", + title=f"{collection.id} items", ) # GeoJSONSeq Response @@ -1086,6 +1102,7 @@ async def item( request, orjson.loads(orjsonDumps(data).decode()), template_name="item", + title=f"{collection.id}/{itemId} item", ) # Default to GeoJSON Response @@ -1245,6 +1262,7 @@ async def tilematrixsets( request, data.model_dump(exclude_none=True, mode="json"), template_name="tilematrixsets", + title="TileMatrixSets list", ) return data @@ -1287,6 +1305,7 @@ async def tilematrixset( "bbox": tms.bbox, }, template_name="tilematrixset", + title=f"{tileMatrixSetId} TileMatrixSet", ) return tms @@ -1379,6 +1398,7 @@ async def collection_tileset_list( request, data.model_dump(exclude_none=True, mode="json"), template_name="tilesets", + title=f"{collection.id} tilesets", ) return data @@ -1513,6 +1533,7 @@ async def collection_tileset( request, data.model_dump(exclude_none=True, mode="json"), template_name="tileset", + title=f"{collection.id} {tileMatrixSetId} tileset", ) return data diff --git a/tipg/templates/collection.html b/tipg/templates/collection.html index 5ebc174..9289d93 100644 --- a/tipg/templates/collection.html +++ b/tipg/templates/collection.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/collections.html b/tipg/templates/collections.html index 45bb4ef..e4ebf85 100644 --- a/tipg/templates/collections.html +++ b/tipg/templates/collections.html @@ -2,8 +2,8 @@ {% set show_prev_link = false %} {% set show_next_link = false %} -{% if 'items?' in url %} - {% set urlq = url + '&' %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} {% else %} {% set urlq = url + '?' %} {% endif %} @@ -17,7 +17,7 @@ {% endif %} {% endfor %} -