From 5f80de76714f16394e6887dcf5dba1937cd8557d Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Fri, 29 Mar 2024 11:20:34 +0100 Subject: [PATCH 1/2] better handle router-prefix and root-path in HTML template --- tipg/factory.py | 37 +++++++++++++++++++++++------- tipg/templates/collection.html | 7 +++++- tipg/templates/collections.html | 6 ++--- tipg/templates/conformance.html | 7 +++++- tipg/templates/item.html | 7 +++++- tipg/templates/items.html | 4 ++-- tipg/templates/landing.html | 7 +++++- tipg/templates/queryables.html | 7 +++++- tipg/templates/tilematrixset.html | 7 +++++- tipg/templates/tilematrixsets.html | 7 +++++- tipg/templates/tileset.html | 7 +++++- tipg/templates/tilesets.html | 7 +++++- 12 files changed, 88 insertions(+), 22 deletions(-) diff --git a/tipg/factory.py b/tipg/factory.py index 4071d2ac..ec68139e 100644 --- a/tipg/factory.py +++ b/tipg/factory.py @@ -115,17 +115,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 @@ -134,9 +145,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", @@ -145,13 +153,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, }, ) @@ -208,13 +215,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 @@ -326,6 +337,7 @@ def landing( request, data.model_dump(exclude_none=True, mode="json"), template_name="landing", + title=self.title, ) return data @@ -520,6 +532,7 @@ def collections( request, data.model_dump(exclude_none=True, mode="json"), template_name="collections", + title="Collections list", ) return data @@ -607,6 +620,7 @@ def collection( request, data.model_dump(exclude_none=True, mode="json"), template_name="collection", + title=f"{collection.id} collection", ) return data @@ -652,6 +666,7 @@ def queryables( request, data.model_dump(exclude_none=True, mode="json"), template_name="queryables", + title=f"{collection.id} queryables", ) return data @@ -908,6 +923,7 @@ async def items( # noqa: C901 request, orjson.loads(orjsonDumps(data).decode()), template_name="items", + title=f"{collection.id} items", ) # GeoJSONSeq Response @@ -1074,6 +1090,7 @@ async def item( request, orjson.loads(orjsonDumps(data).decode()), template_name="item", + title=f"{collection.id}/{itemId} item", ) # Default to GeoJSON Response @@ -1212,6 +1229,7 @@ async def tilematrixsets( request, data.model_dump(exclude_none=True, mode="json"), template_name="tilematrixsets", + title="TileMatrixSets list", ) return data @@ -1254,6 +1272,7 @@ async def tilematrixset( "bbox": tms.bbox, }, template_name="tilematrixset", + title=f"{tileMatrixSetId} TileMatrixSet", ) return tms @@ -1346,6 +1365,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 @@ -1460,6 +1480,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 5ebc1740..9289d936 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 45bb4ef1..e4ebf85e 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 %} - + diff --git a/tipg/templates/conformance.html b/tipg/templates/conformance.html index 340be3a3..ba51cc55 100644 --- a/tipg/templates/conformance.html +++ b/tipg/templates/conformance.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/item.html b/tipg/templates/item.html index e8654220..f61b009f 100644 --- a/tipg/templates/item.html +++ b/tipg/templates/item.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/items.html b/tipg/templates/items.html index 28d7d420..ee5ba56f 100644 --- a/tipg/templates/items.html +++ b/tipg/templates/items.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 %} diff --git a/tipg/templates/landing.html b/tipg/templates/landing.html index 774023eb..9aa28860 100644 --- a/tipg/templates/landing.html +++ b/tipg/templates/landing.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/queryables.html b/tipg/templates/queryables.html index 512e9135..66b80204 100644 --- a/tipg/templates/queryables.html +++ b/tipg/templates/queryables.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/tilematrixset.html b/tipg/templates/tilematrixset.html index 7936ebfa..e711f513 100644 --- a/tipg/templates/tilematrixset.html +++ b/tipg/templates/tilematrixset.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/tilematrixsets.html b/tipg/templates/tilematrixsets.html index 364461cc..b14b0ead 100644 --- a/tipg/templates/tilematrixsets.html +++ b/tipg/templates/tilematrixsets.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/tileset.html b/tipg/templates/tileset.html index 3665fbb5..a53149cd 100644 --- a/tipg/templates/tileset.html +++ b/tipg/templates/tileset.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} diff --git a/tipg/templates/tilesets.html b/tipg/templates/tilesets.html index 8a7438f6..3cb8e153 100644 --- a/tipg/templates/tilesets.html +++ b/tipg/templates/tilesets.html @@ -1,4 +1,9 @@ {% include "header.html" %} +{% if params %} + {% set urlq = url + '?' + params + '&' %} + {% else %} + {% set urlq = url + '?' %} +{% endif %} From 64b34ec4956c0b164d925475e278d781670940ff Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Fri, 29 Mar 2024 15:24:43 +0100 Subject: [PATCH 2/2] update changelog --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a731db49..41a8cdc8 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