Skip to content

Commit a6c0038

Browse files
Merge pull request #174 from developmentseed/patch/better-handle-prefix-and-root-path-in-template
better handle router-prefix and root-path in HTML template
2 parents 1edfab5 + 64b34ec commit a6c0038

13 files changed

+92
-22
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin
1212
- add `templated=True` in template URL links
1313
- add `(Template URL)` in template URL links title
1414
- remove *deserialization* in `tipg.factory.create_html_response` function
15+
- add `title` option to `create_html_response` method, in order to set the web page title
16+
- add `**kwargs` to `create_html_response` method to allow custom object to be passed to the template
17+
- fix url/path passed to the HTML template
18+
- fix HTML templates when passing Query Parameters
1519

1620
## [0.6.3] - 2024-02-02
1721

tipg/factory.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,28 @@ def create_html_response(
115115
data: Any,
116116
templates: Jinja2Templates,
117117
template_name: str,
118+
title: Optional[str] = None,
118119
router_prefix: Optional[str] = None,
120+
**kwargs: Any,
119121
) -> _TemplateResponse:
120122
"""Create Template response."""
121123
urlpath = request.url.path
122124
if root_path := request.app.root_path:
123125
urlpath = re.sub(r"^" + root_path, "", urlpath)
124126

127+
if router_prefix:
128+
urlpath = re.sub(r"^" + router_prefix, "", urlpath)
129+
125130
crumbs = []
126131
baseurl = str(request.base_url).rstrip("/")
127132

133+
if router_prefix:
134+
baseurl += router_prefix
135+
128136
crumbpath = str(baseurl)
137+
if urlpath == "/":
138+
urlpath = ""
139+
129140
for crumb in urlpath.split("/"):
130141
crumbpath = crumbpath.rstrip("/")
131142
part = crumb
@@ -134,9 +145,6 @@ def create_html_response(
134145
crumbpath += f"/{crumb}"
135146
crumbs.append({"url": crumbpath.rstrip("/"), "part": part.capitalize()})
136147

137-
if router_prefix:
138-
baseurl += router_prefix
139-
140148
return templates.TemplateResponse(
141149
request,
142150
name=f"{template_name}.html",
@@ -145,13 +153,12 @@ def create_html_response(
145153
"template": {
146154
"api_root": baseurl,
147155
"params": request.query_params,
148-
"title": "",
156+
"title": title or template_name,
149157
},
150158
"crumbs": crumbs,
151-
"url": str(request.url),
152-
"baseurl": baseurl,
153-
"urlpath": str(request.url.path),
154-
"urlparams": str(request.url.query),
159+
"url": baseurl + urlpath,
160+
"params": str(request.url.query),
161+
**kwargs,
155162
},
156163
)
157164

@@ -208,13 +215,17 @@ def _create_html_response(
208215
request: Request,
209216
data: Any,
210217
template_name: str,
218+
title: Optional[str] = None,
219+
**kwargs: Any,
211220
) -> _TemplateResponse:
212221
return create_html_response(
213222
request,
214223
data,
215224
templates=self.templates,
216225
template_name=template_name,
226+
title=title,
217227
router_prefix=self.router_prefix,
228+
**kwargs,
218229
)
219230

220231
@abc.abstractmethod
@@ -326,6 +337,7 @@ def landing(
326337
request,
327338
data.model_dump(exclude_none=True, mode="json"),
328339
template_name="landing",
340+
title=self.title,
329341
)
330342

331343
return data
@@ -520,6 +532,7 @@ def collections(
520532
request,
521533
data.model_dump(exclude_none=True, mode="json"),
522534
template_name="collections",
535+
title="Collections list",
523536
)
524537

525538
return data
@@ -607,6 +620,7 @@ def collection(
607620
request,
608621
data.model_dump(exclude_none=True, mode="json"),
609622
template_name="collection",
623+
title=f"{collection.id} collection",
610624
)
611625

612626
return data
@@ -652,6 +666,7 @@ def queryables(
652666
request,
653667
data.model_dump(exclude_none=True, mode="json"),
654668
template_name="queryables",
669+
title=f"{collection.id} queryables",
655670
)
656671

657672
return data
@@ -908,6 +923,7 @@ async def items( # noqa: C901
908923
request,
909924
orjson.loads(orjsonDumps(data).decode()),
910925
template_name="items",
926+
title=f"{collection.id} items",
911927
)
912928

913929
# GeoJSONSeq Response
@@ -1074,6 +1090,7 @@ async def item(
10741090
request,
10751091
orjson.loads(orjsonDumps(data).decode()),
10761092
template_name="item",
1093+
title=f"{collection.id}/{itemId} item",
10771094
)
10781095

10791096
# Default to GeoJSON Response
@@ -1212,6 +1229,7 @@ async def tilematrixsets(
12121229
request,
12131230
data.model_dump(exclude_none=True, mode="json"),
12141231
template_name="tilematrixsets",
1232+
title="TileMatrixSets list",
12151233
)
12161234

12171235
return data
@@ -1254,6 +1272,7 @@ async def tilematrixset(
12541272
"bbox": tms.bbox,
12551273
},
12561274
template_name="tilematrixset",
1275+
title=f"{tileMatrixSetId} TileMatrixSet",
12571276
)
12581277

12591278
return tms
@@ -1346,6 +1365,7 @@ async def collection_tileset_list(
13461365
request,
13471366
data.model_dump(exclude_none=True, mode="json"),
13481367
template_name="tilesets",
1368+
title=f"{collection.id} tilesets",
13491369
)
13501370

13511371
return data
@@ -1460,6 +1480,7 @@ async def collection_tileset(
14601480
request,
14611481
data.model_dump(exclude_none=True, mode="json"),
14621482
template_name="tileset",
1483+
title=f"{collection.id} {tileMatrixSetId} tileset",
14631484
)
14641485

14651486
return data

tipg/templates/collection.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

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

1318
<li class="ml-auto json-link">
14-
<a target="_blank" href="{{ url }}?f=json">JSON</a>
19+
<a target="_blank" href="{{ urlq }}f=json">JSON</a>
1520
</li>
1621
</ol>
1722
</nav>

tipg/templates/collections.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
{% set show_prev_link = false %}
44
{% set show_next_link = false %}
5-
{% if 'items?' in url %}
6-
{% set urlq = url + '&' %}
5+
{% if params %}
6+
{% set urlq = url + '?' + params + '&' %}
77
{% else %}
88
{% set urlq = url + '?' %}
99
{% endif %}
@@ -17,7 +17,7 @@
1717
{% endif %}
1818
{% endfor %}
1919

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

tipg/templates/conformance.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/item.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/items.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
{% set show_prev_link = false %}
44
{% set show_next_link = false %}
5-
{% if 'items?' in url %}
6-
{% set urlq = url + '&' %}
5+
{% if params %}
6+
{% set urlq = url + '?' + params + '&' %}
77
{% else %}
88
{% set urlq = url + '?' %}
99
{% endif %}

tipg/templates/landing.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/queryables.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/tilematrixset.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/tilematrixsets.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/tileset.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

tipg/templates/tilesets.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% include "header.html" %}
2+
{% if params %}
3+
{% set urlq = url + '?' + params + '&' %}
4+
{% else %}
5+
{% set urlq = url + '?' %}
6+
{% endif %}
27

38
<nav aria-label="breadcrumb">
49
<ol class="breadcrumb bg-light">
@@ -9,7 +14,7 @@
914
{% endif %}
1015
{% endfor %}
1116

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

0 commit comments

Comments
 (0)