Skip to content

Commit

Permalink
Merge pull request #174 from developmentseed/patch/better-handle-pref…
Browse files Browse the repository at this point in the history
…ix-and-root-path-in-template

better handle router-prefix and root-path in HTML template
  • Loading branch information
vincentsarago authored Mar 29, 2024
2 parents 1edfab5 + 64b34ec commit a6c0038
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 29 additions & 8 deletions tipg/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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,
},
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -326,6 +337,7 @@ def landing(
request,
data.model_dump(exclude_none=True, mode="json"),
template_name="landing",
title=self.title,
)

return data
Expand Down Expand Up @@ -520,6 +532,7 @@ def collections(
request,
data.model_dump(exclude_none=True, mode="json"),
template_name="collections",
title="Collections list",
)

return data
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1212,6 +1229,7 @@ async def tilematrixsets(
request,
data.model_dump(exclude_none=True, mode="json"),
template_name="tilematrixsets",
title="TileMatrixSets list",
)

return data
Expand Down Expand Up @@ -1254,6 +1272,7 @@ async def tilematrixset(
"bbox": tms.bbox,
},
template_name="tilematrixset",
title=f"{tileMatrixSetId} TileMatrixSet",
)

return tms
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/collection.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -11,7 +16,7 @@
{% endif %} {% endfor %}

<li class="ml-auto json-link">
<a target="_blank" href="{{ url }}?f=json">JSON</a>
<a target="_blank" href="{{ urlq }}f=json">JSON</a>
</li>
</ol>
</nav>
Expand Down
6 changes: 3 additions & 3 deletions tipg/templates/collections.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -17,7 +17,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/conformance.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/item.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=geojson">GeoJSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=geojson">GeoJSON</a></li>
</ol>
</nav>

Expand Down
4 changes: 2 additions & 2 deletions tipg/templates/items.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/landing.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/queryables.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=schemajson">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=schemajson">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/tilematrixset.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/tilematrixsets.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/tileset.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down
7 changes: 6 additions & 1 deletion tipg/templates/tilesets.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% include "header.html" %}
{% if params %}
{% set urlq = url + '?' + params + '&' %}
{% else %}
{% set urlq = url + '?' %}
{% endif %}

<nav aria-label="breadcrumb">
<ol class="breadcrumb bg-light">
Expand All @@ -9,7 +14,7 @@
{% endif %}
{% endfor %}

<li class="ml-auto json-link"><a target="_blank" href="{{ url }}?f=json">JSON</a></li>
<li class="ml-auto json-link"><a target="_blank" href="{{ urlq }}f=json">JSON</a></li>
</ol>
</nav>

Expand Down

0 comments on commit a6c0038

Please sign in to comment.