Skip to content

Commit

Permalink
feat: Handle Undefined/None in make_href filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Apr 17, 2024
1 parent 881854c commit c700dca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from jinja2 import Environment, TemplateSyntaxError, FileSystemLoader
from jinja2 import (
Environment,
FileSystemLoader,
TemplateSyntaxError,
is_undefined,
)

esc = markupsafe.escape

Expand Down Expand Up @@ -65,6 +70,9 @@ def __finalize(self, markup: t.Any) -> object:
)

def __make_href_filter(self, obj: object) -> str | None:
if is_undefined(obj) or obj is None:
return "#"

if isinstance(obj, capellambse.model.ElementList):
raise TypeError("Cannot make an href to a list of elements")
if not isinstance(
Expand Down
6 changes: 3 additions & 3 deletions templates/common_macros.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<ul>
{% for rel in rels %}
<li>
<a href="{{ rel.type | make_href if rel.type else '#' }}">{{rel.type.long_name }}</a>
{{ rel.target.__class__.__name__ }}
<a href="{{ rel.target | make_href if rel.target else '#' }}">{{ rel.target.name }}</a>
<a href="{{ rel.type | make_href }}">{{rel.type.long_name }}</a>
{{ rel.target.__class__.__name__ }}
<a href="{{ rel.target | make_href }}">{{ rel.target.name }}</a>
</li>
{% endfor %}
</ul>
Expand Down

0 comments on commit c700dca

Please sign in to comment.