Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/developmentseed/tipg into f…
Browse files Browse the repository at this point in the history
…eature/new-multi-tms-viewer
  • Loading branch information
vincentsarago committed Mar 29, 2024
2 parents e40d1f1 + a6c0038 commit 73ad49f
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 @@ -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
Expand All @@ -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",
Expand All @@ -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,
},
)

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

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

return data
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1245,6 +1262,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 @@ -1287,6 +1305,7 @@ async def tilematrixset(
"bbox": tms.bbox,
},
template_name="tilematrixset",
title=f"{tileMatrixSetId} TileMatrixSet",
)

return tms
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
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 73ad49f

Please sign in to comment.