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

Feature to add title (BODCWEBAPP-2818) #50

Merged
merged 8 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 32 additions & 4 deletions nvsvocprez/routes/collection_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
get_collection_query,
get_alt_profile_objects,
get_ontologies,
get_external_mappings,
extract_external_mapping_url,
get_user_status,
sparql_construct,
sparql_query,
Expand Down Expand Up @@ -273,6 +275,7 @@ class CollectionRenderer(Renderer):
def __init__(self):
self.alt_profiles = get_alt_profiles()
self.ontologies = get_ontologies()

self.instance_uri = f"{DATA_URI}/collection/{collection_id}/current/"
profiles = {"nvs": nvs, "skos": skos, "vocpub": vocpub, "dd": dd}
for collection in cache_return(collections_or_conceptschemes="collections"):
Expand Down Expand Up @@ -508,6 +511,9 @@ def __init__(self, request):

self.alt_profiles = get_alt_profiles()
self.ontologies = get_ontologies()
collection_id = self.instance_uri.split("/collection/")[1].split("/")[0]
self.external_mappings = get_external_mappings(collection_id)

collection_uri = self.instance_uri.split("/current/")[0] + "/current/"
for collection in cache_return(collections_or_conceptschemes="collections"):
if collection["uri"]["value"] == collection_uri:
Expand Down Expand Up @@ -682,6 +688,7 @@ def _render_nvs_or_profile_html(self):
"profile_token": self.profile,
"alt_profiles": self.alt_profiles,
"profile_properties_for_button": [],
"external_mappings": self.external_mappings,
}

def make_predicate_label_from_uri(uri):
Expand Down Expand Up @@ -890,11 +897,32 @@ def return_alt_label(collection: str) -> str:
for entry in alt_labels_json["results"]["bindings"]:
if collection in entry["x"]["value"]:
return entry["label"]["value"]
return ""
else:
# If it is in external mappings
url = extract_external_mapping_url(collection)
if url:
for ext_mapping in context["external_mappings"].keys():
if ext_mapping in url:
return context["external_mappings"][ext_mapping]["title"]
else:
return ""

# populate the alternate labels
alt_labels = {}
for sub_dict in context["related"].values():
for k in sub_dict.copy().keys():
if "<td" in k:
alt_labels[return_alt_label(k)] = k
# Need to swap the title and the link for ext mappings
(list(context["related"].values())[0])[return_alt_label(k)] = k
del list(context["related"].values())[0][k]
else:
alt_labels[k] = return_alt_label(k)

context["alt_labels"] = {
k: return_alt_label(k) for sub_dict in context["related"].values() for k in sub_dict.keys()
}
context["alt_labels"] = alt_labels
# context["alt_labels"] = {
matcor-bodc marked this conversation as resolved.
Show resolved Hide resolved
# k: return_alt_label(k) for sub_dict in context["related"].values() for k in sub_dict.keys()
# }
return templates.TemplateResponse("concept.html", context=context)

def _render_nvs_rdf(self):
Expand Down
31 changes: 31 additions & 0 deletions nvsvocprez/routes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,37 @@ def get_collection_query(profile: Profile, instance_uri: str, ontologies: Dict):
return query


def get_external_mappings(collection_id: str) -> Dict:
"""Get external mappings title from livbodcsos ords endpoint.

Returns (Dict): Dict of parsed external mappings data.
"""
if page_configs.ORDS_ENDPOINT_URL is None:
logging.error("Environment variable ORDS_ENDPOINT_URL is not set.")
return {}
try:
url = f"{page_configs.ORDS_ENDPOINT_URL}/collection-external-mappings/{collection_id}"
resp_json = requests.get(url).json()
external_mapping_data = {mapping["url"]: mapping for mapping in resp_json["items"]}
return external_mapping_data
except requests.RequestException as exc:
logging.error("Failed to retrieve external mappings information from %s.\n%s", url, exc)
return {} # Return blank dict to avoid internal server error.


def extract_external_mapping_url(tag: str) -> str:
"""Returns a external mappings from html tag.

Returns :The related external mappings data.
"""
soup = BeautifulSoup(tag, features="html.parser")
aTags = soup.find("a")
if aTags != None:
return soup.find("a").getText()
else:
return ""


class RelatedItem:
"""Hold related items and provide functionality for sorting and grouping."""

Expand Down
73 changes: 51 additions & 22 deletions nvsvocprez/view/templates/concept.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,61 @@ <h2>{{ prefLabel }}</h2>
{% endif %}
{% if related|length > 0 %}
{% for key, groups in related.items() %}
<tr>
<tr>
<th>
{{ key|safe }}
</th>
</tr>
{% for k, vi in groups.items() %}
{% if vi|length == 1 %}
<tr>
<td>{{ vi[0].object_html|safe }}</td>
</tr>
{% else %}
<tr class="group-header header">
<th></th>
<th colspan="3" style="color:#007dbb;font-size:12px;cursor: pointer">
{{ "{} {} - ({})".format(k, alt_labels[k], vi|length) }}
<span class="toggle-icon">[+]</span>
</th>
</tr>
{% for v in vi %}
<tr class="group-row hidden-row">
<td>{{ v.object_html|safe }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% for k, vi in groups.items() %}

{% if vi|length == 1 %}
{% if loop.index > 1 %}
<tr>
<td>
{% endif %}
{{ vi[0].object_html|safe }}
</td>
<td>
{{ "{}".format(alt_labels[k]) }}
</td>
{% elif vi is string %}
{% if loop.index > 1 %}
<tr>
<td colspan="1">
{% endif %}
<td colspan="1" style="color:black;font-size:12px;">
{{ "{}".format(k)}}
</td>
{{alt_labels[k]|safe}}
{% if loop.last and loop.length == 1 %}
</td>
{% else %}
</td>
</tr>
{% endif %}
{% else %}
<tr class="group-header header">
<th></th>
<th colspan="3" style="color:#007dbb;font-size:12px;cursor: pointer">
{{ "{} {} - ({})".format(k, alt_labels[k], vi|length) }}
<span class="toggle-icon">[+]</span>
</th>
</tr>
{% for v in vi %}
<tr class="group-row hidden-row">
<td>{{ v.object_html|safe }}</td>
</tr>
{% endfor %}
{% endif %}

{% if loop.last and loop.length == 1 %}
</td>
{% else %}
</td>
</tr>
{% endif %}
{% endfor %}
{% endfor %}
</tr>
{% endif %}
</table>
</div>
Expand Down