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

Template improvements #25

Merged
merged 14 commits into from
Jul 19, 2024
Merged
14 changes: 6 additions & 8 deletions capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import pathlib
import time
import traceback
import typing as t
import urllib.parse as urlparse
from pathlib import Path
Expand Down Expand Up @@ -148,19 +149,16 @@ def render_instance_page(self, template_text, base, object=None):
print(base)
return HTMLResponse(content=error_message)
except Exception as e:
LOGGER.exception("Error rendering template")
trace = markupsafe.escape(traceback.format_exc())
error_message = markupsafe.Markup(
'<p style="color:red">'
"Unexpected error: {etype}: {emsg}"
f"Unexpected error: {type(e).__name__}: {str(e)}"
'</p><pre style="font-size:80%;overflow:scroll">'
"object={obj}\nmodel={model}"
f"object={repr(object)}\nmodel={repr(self.model)}"
f"\n\n{trace}"
"</pre>"
).format(
etype=type(e).__name__,
emsg=str(e),
obj=repr(object),
model=repr(self.model),
)
# error = error_message
return HTMLResponse(content=error_message)

def configure_routes(self):
Expand Down
19 changes: 11 additions & 8 deletions capella_model_explorer/backend/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ def find_objects(model, obj_type=None, below=None, attr=None, filters=None):
raise ValueError("No search criteria provided")

if filters:
objects = [
obj
for obj in objects
if all(
getattr(obj, key) == value for key, value in filters.items()
)
]

filtered = []
for object in objects:
for attr_key, filter in filters.items():
attr = getattr(object, attr_key)
if filter == "not_empty":
if attr:
filtered.append(object)
else:
if attr == filter:
filtered.append(object)
objects = filtered
return objects


Expand Down
65 changes: 38 additions & 27 deletions templates/common_macros.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,31 @@
{% endif %}
{% endmacro %}

{% macro req_rels(rels) %}
{% macro req_rels(rels, scope=None) %}
{% if rels %}
<ul>
{% for rel in rels %}
<li>
<a href="{{ rel.type | make_href }}">{{rel.type.long_name }}</a>
{{ linked_name_with_icon(rel.target) | safe}}
</li>
{% if scope %}
{% if rel.target == scope or rel.source == scope %}
<li>
<a href="{{ rel.type | make_href }}">{{rel.type.long_name }}</a>
{{ linked_name_with_icon(rel.target) | safe}}
</li>
{% endif %}
{% else %}
<li>
<a href="{{ rel.type | make_href }}">{{rel.type.long_name }}</a>
{{ linked_name_with_icon(rel.target) | safe}}
</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p style="color: red;">Not linked to any model objects</p>
{% endif%}
{% endmacro %}

{% macro render_requirements_table(reqs) %}
{% macro render_requirements_table(reqs, scope=None) %}
<table>
{% for req in reqs %}
<tr>
Expand All @@ -120,7 +129,7 @@
</td>
<td>
{{ req.text if req.text else req.long_name }}
{{req_rels(req.relations) | safe}}
{{req_rels(req.relations, scope=scope) | safe}}
{{show_other_attributes(req, excluded=["related", "relations", "requirements", "text", "owner", "validate", "xtype"], hide_rules_check=True, hide_unset_attrs=True) | safe}}
</td>
</div>
Expand Down Expand Up @@ -177,7 +186,7 @@
{% endif %}
{% endmacro %}

{%- macro display_traceability(object, complain=False) -%}
{%- macro display_traceability(object, show_upper=True) -%}
{%- set realized_attrs = [] -%}
{%- set realizing_attrs = [] -%}
{%- for attr in object.__dir__() -%}
Expand All @@ -187,29 +196,31 @@
{% set _none = realizing_attrs.append(attr) %}
{%- endif-%}
{%- endfor -%}
{%- set realized_objects = object[realized_attrs | first] if realized_attrs else None -%}
{%- set realizing_objects = object[realizing_attrs | first] if realizing_attrs else None -%}
{%- set realized_objects = object[realized_attrs | first] if realized_attrs else [] -%}
{%- set realizing_objects = object[realizing_attrs | first] if realizing_attrs else [] -%}

<h2>Traceability</h2>
{% if "realization_view" in object.__dir__() %}
<p>The figure below provides an overview of objects that "{{ object.name}}" realizes but also those that realize "{{ object.name }}"</p>
{{ object.realization_view.as_svg | safe }}
{% if (realized_objects | length > 0 or realizing_objects | length > 0) and "realization_view" in object.__dir__() %}
<p>The figure below provides an overview of objects that "{{ object.name}}" realizes but also those that realize "{{ object.name }}"</p>
{{ object.realization_view.as_svg | safe }}
{% endif %}
{% if show_upper %}
{% if realized_objects %}
<p>{{ object.name }} realizes the following objects:<p>
<ul>
{%- for obj in realized_objects -%}<li style="display: block;">{{ linked_name_with_icon(obj) | safe}}</li>{%- endfor -%}
</ul>
{%- else -%}
<p style="color: red;">{{ first_upper(object.name) }} doesn't seem to realize any object</p>
{%- endif -%}
{% endif %}
{% if realized_objects %}
<p>{{ object.name }} realizes the following objects:<p>
<ul>
{%- for obj in realized_objects -%}<li style="display: block;">{{ linked_name_with_icon(obj) | safe}}</li>{%- endfor -%}
</ul>
{%- else -%}
<p style="color: red;">{{ first_upper(object.name) }} doesn't seem to realize any object</p>
{%- endif -%}
{% if realizing_objects %}
<p>{{ object.name }} is realized by the following objects:</p>
<ul>
{%- for obj in realizing_objects -%}<li style="display: block;">{{ linked_name_with_icon(obj) | safe}}</li>{%- endfor -%}
</ul>
{%- else -%}
<p style="color: red;">{{ first_upper(object.name) }} doesn't seem to be realized by any object</p>
<p>{{ object.name }} is realized by the following objects:</p>
<ul>
{%- for obj in realizing_objects -%}<li style="display: block;">{{ linked_name_with_icon(obj) | safe}}</li>{%- endfor -%}
</ul>
{%- else -%}
<p style="color: red;">{{ first_upper(object.name) }} doesn't seem to be realized by any object</p>
{% endif %}
{%- endmacro -%}

Expand Down
9 changes: 8 additions & 1 deletion templates/cross-cutting/classes.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</tbody>
</table>
{% endif %}

<p>Property {{ property.name }} has the following additional attributes:</p>
{% if prop_props %}
<table class="table">
Expand All @@ -65,7 +66,13 @@
{% for key, val in prop_props %}
<tr>
<td>{{ key }}</td>
<td>{{ val.value }} :{{ linked_name_with_icon(val.type) | safe}}</td>
<td>
{% if val.type %}
{{ val.value }} :{{ linked_name_with_icon(val.type) | safe}}
{% else %}
{{ val.value }}
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
22 changes: 14 additions & 8 deletions templates/cross-cutting/diagrams.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@
SPDX-License-Identifier: Apache-2.0
#}

{% from 'common_macros.html.j2' import show_other_attributes, description, linked_name_with_icon %}


<h1>{{ object.name }}</h1>
{{ object.as_svg | safe }}

<h2>Description</h2>
{% if object.description %}
<p>{{ object.description }}</p>
{% else %}
<p style="color:red">No description available.</p>
{% endif %}
{{ description(object) | safe }}

<h2>Referenced Objects</h2>
{% if object.nodes %}
<ul>
{% for node in object.nodes %}
{% if node.name %}
<li><a href="{{ node | make_href }}">{{ node.name }}</a></li>
{% endif %}
{% if node.__class__.__name__ == "InstanceRole" %}
<li>{{ linked_name_with_icon(node) | safe }}, an instance of {{ linked_name_with_icon(node.instance.type) | safe }}</li>
{% elif node.__class__.__name__ == "StateFragment" %}
<li>
<a href="{{ node | make_href }}">StateFragment</a>, execution of {{ linked_name_with_icon(node.function) | safe }}</li>
{% elif node.name %}
<li>{{ linked_name_with_icon(node) | safe }}</li>
{% else %}
<li><a href="{{ node | make_href }}">Unnamed {{ node.__class__.__name__}}</a></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
Expand Down
2 changes: 2 additions & 0 deletions templates/cross-cutting/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ categories:
isExperimental: true
scope:
type: Region
filters:
states: not_empty
2 changes: 2 additions & 0 deletions templates/operational-analysis/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories:
- template: operational-analysis/operational-entity.html.j2
idx: operational-entity
name: Operational Entity
isExperimental: true
description: Specifies an operational entity.
scope:
type: Entity
Expand All @@ -21,6 +22,7 @@ categories:
idx: operational-activity
name: Operational Activity
description: Specifies an operational activity.
isExperimental: true
scope:
type: OperationalActivity
below: oa
Loading
Loading