Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent HTML templating override persistence #1943

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pygeoapi/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def get_config_(

if request.format == F_HTML:
content = render_j2_template(
admin.config, 'admin/index.html', cfg, request.locale
admin.tpl_config, admin.config['server']['templates'],
'admin/index.html', cfg, request.locale
)
else:
content = to_json(cfg, admin.pretty_print)
Expand Down Expand Up @@ -316,10 +317,8 @@ def get_resources(

if request.format == F_HTML:
content = render_j2_template(
admin.config,
'admin/index.html',
cfg['resources'],
request.locale,
admin.tpl_config, admin.config['server']['templates'],
'admin/index.html', cfg['resources'], request.locale
)
else:
content = to_json(cfg['resources'], admin.pretty_print)
Expand Down Expand Up @@ -422,7 +421,8 @@ def get_resource(

if request.format == F_HTML:
content = render_j2_template(
admin.config, 'admin/index.html', resource, request.locale
admin.tpl_config, admin.config['server']['templates'],
'admin/index.html', resource, request.locale
)
else:
content = to_json(resource, admin.pretty_print)
Expand Down
44 changes: 26 additions & 18 deletions pygeoapi/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ def get_exception(self, status, headers, format_, code,
if format_ == F_HTML:
headers['Content-Type'] = FORMAT_TYPES[F_HTML]
content = render_j2_template(
self.config, 'exception.html', exception, SYSTEM_LOCALE)
self.tpl_config, self.config['server']['templates'],
'exception.html', exception, SYSTEM_LOCALE)
else:
content = to_json(exception, self.pretty_print)

Expand All @@ -619,12 +620,13 @@ def get_format_exception(self, request) -> Tuple[dict, int, str]:
HTTPStatus.BAD_REQUEST, headers,
request.format, 'InvalidParameterValue', msg)

def get_collections_url(self):
def get_collections_url(self) -> str:
return f"{self.base_url}/collections"

def set_dataset_templates(self, dataset):
if 'templates' in self.config['resources'][dataset]:
self.tpl_config['server']['templates'] = self.config['resources'][dataset]['templates'] # noqa
def get_dataset_templates(self, dataset) -> dict:
templates = self.config['resources'][dataset].get('templates')

return templates or self.tpl_config['server']['templates']

@staticmethod
def _create_crs_transform_spec(
Expand Down Expand Up @@ -829,8 +831,10 @@ def landing_page(api: API,
'type', 'collection'):
fcm['collection'] = True

content = render_j2_template(api.tpl_config, 'landing_page.html',
fcm, request.locale)
content = render_j2_template(
api.tpl_config, api.config['server']['templates'],
'landing_page.html', fcm, request.locale)

return headers, HTTPStatus.OK, content

if request.format == F_JSONLD:
Expand Down Expand Up @@ -860,8 +864,10 @@ def openapi_(api: API, request: APIRequest) -> Tuple[dict, int, str]:
data = {
'openapi-document-path': path
}
content = render_j2_template(api.tpl_config, template, data,
request.locale)
content = render_j2_template(
api.tpl_config, api.config['server']['templates'], template, data,
request.locale)

return headers, HTTPStatus.OK, content

headers['Content-Type'] = 'application/vnd.oai.openapi+json;version=3.0' # noqa
Expand Down Expand Up @@ -908,8 +914,10 @@ def conformance(api, request: APIRequest) -> Tuple[dict, int, str]:

headers = request.get_response_headers(**api.api_headers)
if request.format == F_HTML: # render
content = render_j2_template(api.tpl_config, 'conformance.html',
conformance, request.locale)
content = render_j2_template(
api.tpl_config, api.config['server']['templates'],
'conformance.html', conformance, request.locale)

return headers, HTTPStatus.OK, content

return headers, HTTPStatus.OK, to_json(conformance, api.pretty_print)
Expand Down Expand Up @@ -1336,14 +1344,14 @@ def describe_collections(api: API, request: APIRequest,
if request.format == F_HTML: # render
fcm['collections_path'] = api.get_collections_url()
if dataset is not None:
api.set_dataset_templates(dataset)
content = render_j2_template(api.tpl_config,
tpl_config = api.get_dataset_templates(dataset)
content = render_j2_template(api.tpl_config, tpl_config,
'collections/collection.html',
fcm, request.locale)
else:
content = render_j2_template(api.tpl_config,
'collections/index.html',
fcm, request.locale)
content = render_j2_template(
api.tpl_config, api.config['server']['templates'],
'collections/index.html', fcm, request.locale)

return headers, HTTPStatus.OK, content

Expand Down Expand Up @@ -1428,14 +1436,14 @@ def get_collection_schema(api: API, request: Union[APIRequest, Any],
schema['properties'][k]['x-ogc-role'] = 'primary-instant'

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)
schema['title'] = l10n.translate(
api.config['resources'][dataset]['title'], request.locale)

schema['collections_path'] = api.get_collections_url()
schema['dataset_path'] = f'{api.get_collections_url()}/{dataset}'

content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, tpl_config,
'collections/schema.html',
schema, request.locale)

Expand Down
10 changes: 5 additions & 5 deletions pygeoapi/api/environmental_data_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def get_collection_edr_instances(api: API, request: APIRequest, dataset,
}])

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)

serialized_query_params = ''
for k, v in request.params.items():
Expand Down Expand Up @@ -224,8 +224,8 @@ def get_collection_edr_instances(api: API, request: APIRequest, dataset,
'href': f'{uri}?f={F_JSONLD}{serialized_query_params}'
}]

content = render_j2_template(api.tpl_config, template, data,
api.default_locale)
content = render_j2_template(api.tpl_config, tpl_config, template,
data, api.default_locale)
else:
content = to_json(data, api.pretty_print)

Expand Down Expand Up @@ -381,7 +381,7 @@ def get_collection_edr_query(api: API, request: APIRequest,
err.ogc_exception_code, err.message)

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)

uri = f'{api.get_collections_url()}/{dataset}/{query_type}'
serialized_query_params = ''
Expand Down Expand Up @@ -413,7 +413,7 @@ def get_collection_edr_query(api: API, request: APIRequest,
'href': f'{uri}?f={F_JSONLD}{serialized_query_params}'
}]

content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, tpl_config,
'collections/edr/query.html', data,
api.default_locale)
else:
Expand Down
12 changes: 6 additions & 6 deletions pygeoapi/api/itemtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,15 @@ def get_collection_queryables(api: API, request: Union[APIRequest, Any],
queryables['properties'][k]['x-ogc-role'] = 'primary-instant' # noqa

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)

queryables['title'] = l10n.translate(
api.config['resources'][dataset]['title'], request.locale)

queryables['collections_path'] = api.get_collections_url()
queryables['dataset_path'] = f'{api.get_collections_url()}/{dataset}'

content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, tpl_config,
'collections/queryables.html',
queryables, request.locale)

Expand Down Expand Up @@ -591,7 +591,7 @@ def get_collection_items(
l10n.set_response_language(headers, prv_locale, request.locale)

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)
# For constructing proper URIs to items

content['items_path'] = uri
Expand All @@ -608,7 +608,7 @@ def get_collection_items(
request.locale)
# If title exists, use it as id in html templates
content['id_field'] = content['title_field']
content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, tpl_config,
'collections/items/index.html',
content, request.locale)
return headers, HTTPStatus.OK, content
Expand Down Expand Up @@ -909,7 +909,7 @@ def get_collection_item(api: API, request: APIRequest,
l10n.set_response_language(headers, prv_locale, request.locale)

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)
content['title'] = l10n.translate(collections[dataset]['title'],
request.locale)
content['id_field'] = p.id_field
Expand All @@ -920,7 +920,7 @@ def get_collection_item(api: API, request: APIRequest,
request.locale)
content['collections_path'] = api.get_collections_url()

content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, tpl_config,
'collections/items/item.html',
content, request.locale)
return headers, HTTPStatus.OK, content
Expand Down
20 changes: 11 additions & 9 deletions pygeoapi/api/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ def describe_processes(api: API, request: APIRequest,

if request.format == F_HTML: # render
if process is not None:
api.set_dataset_templates(process)
response = render_j2_template(api.tpl_config,
tpl_config = api.get_dataset_templates(process)
response = render_j2_template(api.tpl_config, tpl_config,
'processes/process.html',
response, request.locale)
else:
response = render_j2_template(api.tpl_config,
'processes/index.html', response,
request.locale)
response = render_j2_template(
api.tpl_config, api.config['server']['templates'],
'processes/index.html', response, request.locale)

return headers, HTTPStatus.OK, response

Expand Down Expand Up @@ -394,8 +394,10 @@ def get_jobs(api: API, request: APIRequest,
'offset': offset,
'now': datetime.now(timezone.utc).strftime(DATETIME_FORMAT)
}
response = render_j2_template(api.tpl_config, j2_template, data,
request.locale)
response = render_j2_template(
api.tpl_config, api.config['server']['templates'], j2_template,
data, request.locale)

return headers, HTTPStatus.OK, response

return headers, HTTPStatus.OK, to_json(serialized_jobs,
Expand Down Expand Up @@ -586,8 +588,8 @@ def get_job_result(api: API, request: APIRequest,
'result': job_output
}
content = render_j2_template(
api.config, 'jobs/results/index.html',
data, request.locale)
api.config, api.config['server']['templates'],
'jobs/results/index.html', data, request.locale)

return headers, HTTPStatus.OK, content

Expand Down
26 changes: 11 additions & 15 deletions pygeoapi/api/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ def get_stac_root(api: API, request: APIRequest) -> Tuple[dict, int, str]:
})

if request.format == F_HTML: # render
content = render_j2_template(api.tpl_config,
'stac/collection.html',
content, request.locale)
content = render_j2_template(
api.tpl_config, api.config['server']['templates'],
'stac/collection.html', content, request.locale)

return headers, HTTPStatus.OK, content

return headers, HTTPStatus.OK, to_json(content, api.pretty_print)
Expand Down Expand Up @@ -187,18 +188,13 @@ def get_stac_path(api: API, request: APIRequest,
if 'assets' in content: # item view
if content['type'] == 'Collection':
content = render_j2_template(
api.tpl_config,
api.tpl_config, api.config['server']['templates'],
'stac/collection_base.html',
content,
request.locale
)
content, request.locale)
elif content['type'] == 'Feature':
content = render_j2_template(
api.tpl_config,
'stac/item.html',
content,
request.locale
)
api.tpl_config, api.config['server']['templattes'],
'stac/item.html', content, request.locale)
else:
msg = f'Unknown STAC type {content.type}'
return api.get_exception(
Expand All @@ -208,9 +204,9 @@ def get_stac_path(api: API, request: APIRequest,
'NoApplicableCode',
msg)
else:
content = render_j2_template(api.tpl_config,
'stac/catalog.html',
content, request.locale)
content = render_j2_template(
api.tpl_config, api.config['server']['templates'],
'stac/catalog.html', content, request.locale)

return headers, HTTPStatus.OK, content

Expand Down
12 changes: 6 additions & 6 deletions pygeoapi/api/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def get_collection_tiles(api: API, request: APIRequest,
tiles['tilesets'].append(tile_matrix)

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
tpl_config = api.get_dataset_templates(dataset)
tiles['id'] = dataset
tiles['title'] = l10n.translate(
api.config['resources'][dataset]['title'], SYSTEM_LOCALE)
Expand All @@ -189,7 +189,7 @@ def get_collection_tiles(api: API, request: APIRequest,
tiles['collections_path'] = api.get_collections_url()
tiles['tile_type'] = p.tile_type

content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, tpl_config,
'collections/tiles/index.html', tiles,
request.locale)

Expand Down Expand Up @@ -322,8 +322,8 @@ def get_collection_tiles_metadata(
language=prv_locale)

if request.format == F_HTML: # render
api.set_dataset_templates(dataset)
content = render_j2_template(api.tpl_config,
tpl_config = api.get_dataset_templates(dataset)
content = render_j2_template(api.tpl_config, tpl_config,
'collections/tiles/metadata.html',
tiles_metadata, request.locale)

Expand Down Expand Up @@ -384,7 +384,7 @@ def tilematrixsets(api: API,
}]

if request.format == F_HTML: # render
content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, api.tpl_config,
'tilematrixsets/index.html',
tms, request.locale)
return headers, HTTPStatus.OK, content
Expand Down Expand Up @@ -432,7 +432,7 @@ def tilematrixset(api: API,
}

if request.format == F_HTML: # render
content = render_j2_template(api.tpl_config,
content = render_j2_template(api.tpl_config, api.tpl_config,
'tilematrixsets/tilematrixset.html',
tms, request.locale)
return headers, HTTPStatus.OK, content
Expand Down
5 changes: 4 additions & 1 deletion pygeoapi/linked_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ def geojson2jsonld(cls, data: dict, dataset: str,
else:
# Render jsonld template for single item with template configured
LOGGER.debug(f'Rendering JSON-LD template: {template}')
content = render_j2_template(cls.config, template, ldjsonData)
content = render_j2_template(
cls.config, cls.config['server']['templates'],
template, ldjsonData)

ldjsonData = json.loads(content)
return ldjsonData

Expand Down
Loading