Skip to content

Commit

Permalink
make thumbnail url a jinja filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariana Barzinpour authored and omad committed Nov 18, 2022
1 parent a637a81 commit 3ff213a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
11 changes: 11 additions & 0 deletions cubedash/_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ def _dataset_created(dataset: Dataset):
def _dataset_file_paths(dataset: Dataset):
return utils.get_dataset_file_offsets(dataset)

@bp.app_template_filter("dataset_thumbnail_url")
def _dataset_thumbnail_url(dataset: Dataset):
file_paths = _dataset_file_paths(dataset)
if "thumbnail:nbart" in file_paths:
offset = file_paths['thumbnail:nbart']
elif "thumbnail" in file_paths:
offset = file_paths['thumbnail']
else:
return ""
return _to_remote_url(offset, dataset.uris[0])


@bp.app_template_filter("resolve_remote_url")
def _to_remote_url(offset: str, base_uri: str = None):
Expand Down
15 changes: 4 additions & 11 deletions cubedash/templates/dataset.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block title %}{{ dataset | printable_dataset }}{% endblock %}

{% set extent_geojson = dataset_footprint.__geo_interface__ %}
{% set thumbnail_url = [] %}
{% set thumbnail_url = dataset | dataset_thumbnail_url %}

{% block head %}
{{ super() }}
Expand Down Expand Up @@ -144,15 +144,9 @@ <h3>
<i class="fa fa-caret-right" aria-hidden="true"></i>
</th>
<td>
{% set offset_url = offset | resolve_remote_url(uri) %}
<a href="{{ offset_url }}"
<a href="{{ offset | resolve_remote_url(uri) }}"
class="uri-path">
{{ offset }}
{% if 'thumbnail:nbart' == name or 'thumbnail' == name %}
{# workaround for jinja preventing global varaibles from being updated inside
for loops and if/else statements #}
{% set _ = thumbnail_url.append(offset_url) %}
{% endif %}
</a>
</td>
</tr>
Expand Down Expand Up @@ -229,9 +223,8 @@ <h3 class="followed">
});
L.control.zoom({position: "bottomright"}).addTo(map);
map.fitBounds(dataset_data.getBounds(), {animate: false, maxZoom: 5});
const has_thumbnail = '{{ thumbnail_url|length >= 1 }}';
if (has_thumbnail === "True") {
L.imageOverlay("{{ thumbnail_url[0]|e }}", dataset_data.getBounds()).addTo(map);
if ("{{ thumbnail_url }}") {
L.imageOverlay("{{ thumbnail_url }}", dataset_data.getBounds()).addTo(map);
}

window.MAP = map;
Expand Down

0 comments on commit 3ff213a

Please sign in to comment.