clean up _licenses_to_html

don't require a request object, it's not needed there
This commit is contained in:
Christoph Reiter 2025-06-29 11:04:19 +02:00
parent 279d81c584
commit 457d9817d8
2 changed files with 23 additions and 22 deletions

View File

@ -152,8 +152,7 @@ def _license_to_html(license: str) -> str:
return str(markupsafe.escape(license)) return str(markupsafe.escape(license))
@context_function("licenses_to_html") def _licenses_to_html(licenses: list[str]) -> str:
def licenses_to_html(request: Request, licenses: list[str]) -> str:
done = [] done = []
for license in licenses: for license in licenses:
needs_quote = (" " in license.strip()) and len(licenses) > 1 needs_quote = (" " in license.strip()) and len(licenses) > 1
@ -166,6 +165,11 @@ def licenses_to_html(request: Request, licenses: list[str]) -> str:
return " OR ".join(done) return " OR ".join(done)
@context_function("licenses_to_html")
def licenses_to_html(request: Request, licenses: list[str]) -> str:
return _licenses_to_html(licenses)
@template_filter("rdepends_type") @template_filter("rdepends_type")
def rdepends_type(types: set[DepType]) -> list[str]: def rdepends_type(types: set[DepType]) -> list[str]:
if list(types) == [DepType.NORMAL]: if list(types) == [DepType.NORMAL]:

View File

@ -1,40 +1,37 @@
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT
from fastapi import Request from app.web import _licenses_to_html
from app.web import licenses_to_html
def test_licenses_to_html() -> None: def test_licenses_to_html() -> None:
r = Request({"type": "http"}) assert _licenses_to_html([]) == ""
assert licenses_to_html(r, []) == "" assert _licenses_to_html(["FOO"]) == "FOO"
assert licenses_to_html(r, ["FOO"]) == "FOO" assert _licenses_to_html(["FOO", "BAR"]) == "FOO OR BAR"
assert licenses_to_html(r, ["FOO", "BAR"]) == "FOO OR BAR" assert _licenses_to_html(["FOO", "&", "<", ">"]) == \
assert licenses_to_html(r, ["FOO", "&", "<", ">"]) == \
"FOO OR &amp; OR &lt; OR &gt;" "FOO OR &amp; OR &lt; OR &gt;"
assert licenses_to_html(r, ["spdx:FOO-BAR.OK"]) == ( assert _licenses_to_html(["spdx:FOO-BAR.OK"]) == (
'<a href="https://spdx.org/licenses/FOO-BAR.OK.html">FOO-BAR.OK</a>') '<a href="https://spdx.org/licenses/FOO-BAR.OK.html">FOO-BAR.OK</a>')
assert licenses_to_html(r, ["spdx:< > &"]) == '&lt; &gt; &amp;' assert _licenses_to_html(["spdx:< > &"]) == '&lt; &gt; &amp;'
assert licenses_to_html(r, ["spdx:(FOO)"]) == \ assert _licenses_to_html(["spdx:(FOO)"]) == \
'(<a href="https://spdx.org/licenses/FOO.html">FOO</a>)' '(<a href="https://spdx.org/licenses/FOO.html">FOO</a>)'
assert licenses_to_html(r, ["spdx:FOO", "spdx:BAR"]) == ( assert _licenses_to_html(["spdx:FOO", "spdx:BAR"]) == (
'<a href="https://spdx.org/licenses/FOO.html">FOO</a> OR ' '<a href="https://spdx.org/licenses/FOO.html">FOO</a> OR '
'<a href="https://spdx.org/licenses/BAR.html">BAR</a>') '<a href="https://spdx.org/licenses/BAR.html">BAR</a>')
assert licenses_to_html(r, ["custom:BLA", "GPL"]) == "custom:BLA OR GPL" assert _licenses_to_html(["custom:BLA", "GPL"]) == "custom:BLA OR GPL"
assert licenses_to_html(r, ["spdx:BLA", "GPL"]) == \ assert _licenses_to_html(["spdx:BLA", "GPL"]) == \
'<a href="https://spdx.org/licenses/BLA.html">BLA</a> OR GPL' '<a href="https://spdx.org/licenses/BLA.html">BLA</a> OR GPL'
assert licenses_to_html(r, ["spdx:MIT OR BSD-3-Clause", "GPL"]) == ( assert _licenses_to_html(["spdx:MIT OR BSD-3-Clause", "GPL"]) == (
'(<a href="https://spdx.org/licenses/MIT.html">MIT</a> OR ' '(<a href="https://spdx.org/licenses/MIT.html">MIT</a> OR '
'<a href="https://spdx.org/licenses/BSD-3-Clause.html">BSD-3-Clause</a>) OR GPL') '<a href="https://spdx.org/licenses/BSD-3-Clause.html">BSD-3-Clause</a>) OR GPL')
assert licenses_to_html(r, ["&<>"]) == "&amp;&lt;&gt;" assert _licenses_to_html(["&<>"]) == "&amp;&lt;&gt;"
assert licenses_to_html(r, ["spdx:GPL-2.0-or-later WITH Autoconf-exception-2.0"]) == ( assert _licenses_to_html(["spdx:GPL-2.0-or-later WITH Autoconf-exception-2.0"]) == (
'<a href="https://spdx.org/licenses/GPL-2.0-or-later.html">GPL-2.0-or-later</a> WITH ' '<a href="https://spdx.org/licenses/GPL-2.0-or-later.html">GPL-2.0-or-later</a> WITH '
'<a href="https://spdx.org/licenses/Autoconf-exception-2.0.html">Autoconf-exception-2.0</a>' '<a href="https://spdx.org/licenses/Autoconf-exception-2.0.html">Autoconf-exception-2.0</a>'
) )
assert licenses_to_html(r, ["spdx:GPL-2.0+"]) == ( assert _licenses_to_html(["spdx:GPL-2.0+"]) == (
'<a href="https://spdx.org/licenses/GPL-2.0%2B.html">GPL-2.0+</a>' '<a href="https://spdx.org/licenses/GPL-2.0%2B.html">GPL-2.0+</a>'
) )
assert licenses_to_html(r, ["spdx:StandardML-NJ"]) == ( assert _licenses_to_html(["spdx:StandardML-NJ"]) == (
'<a href="https://spdx.org/licenses/StandardML-NJ.html">StandardML-NJ</a>' '<a href="https://spdx.org/licenses/StandardML-NJ.html">StandardML-NJ</a>'
) )
assert licenses_to_html(r, ["spdx:LicenseRef-foobar"]) == 'foobar' assert _licenses_to_html(["spdx:LicenseRef-foobar"]) == 'foobar'