From c75204ec29d198c5835f4b2cea4514b8cb92a804 Mon Sep 17 00:00:00 2001 From: tdruez Date: Tue, 16 Jul 2024 11:41:57 +0400 Subject: [PATCH] Refine the rendering of license_expression #138 Signed-off-by: tdruez --- component_catalog/license_expression_dje.py | 6 +++++- component_catalog/models.py | 6 ++++++ ...unique_dependency_uid_within_product_an.py | 21 +++++++++++++++++++ product_portfolio/models.py | 15 +------------ .../tabs/tab_dependencies.html | 6 ++++++ 5 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 product_portfolio/migrations/0007_remove_productdependency_product_portfolio_productdependency_unique_dependency_uid_within_product_an.py diff --git a/component_catalog/license_expression_dje.py b/component_catalog/license_expression_dje.py index 1085dcf0..37db558a 100644 --- a/component_catalog/license_expression_dje.py +++ b/component_catalog/license_expression_dje.py @@ -14,6 +14,7 @@ from django.forms import widgets from django.urls import reverse from django.utils.html import format_html +from django.utils.safestring import mark_safe from boolean.boolean import PARSE_ERRORS from license_expression import ExpressionError @@ -432,7 +433,10 @@ def render_expression_as_html(expression, dataspace): licensing = get_dataspace_licensing(dataspace) formatted_expression = get_formatted_expression(licensing, expression, show_policy) - return format_html(formatted_expression) + return format_html( + '{}', + mark_safe(formatted_expression), # nosec + ) def get_expression_as_spdx(expression, dataspace): diff --git a/component_catalog/models.py b/component_catalog/models.py index c333c55d..01a8e89f 100644 --- a/component_catalog/models.py +++ b/component_catalog/models.py @@ -48,6 +48,7 @@ from component_catalog.license_expression_dje import get_expression_as_spdx from component_catalog.license_expression_dje import get_license_objects from component_catalog.license_expression_dje import parse_expression +from component_catalog.license_expression_dje import render_expression_as_html from dejacode_toolkit import spdx from dejacode_toolkit.download import DataCollectionException from dejacode_toolkit.download import collect_package_data @@ -228,6 +229,11 @@ def get_expression_as_spdx(self, expression): def concluded_license_expression_spdx(self): return self.get_expression_as_spdx(self.license_expression) + @property + def license_expression_html(self): + if self.license_expression: + return render_expression_as_html(self.license_expression, self.dataspace) + def save(self, *args, **kwargs): """ Call the handle_assigned_licenses method on save, except during copy. diff --git a/product_portfolio/migrations/0007_remove_productdependency_product_portfolio_productdependency_unique_dependency_uid_within_product_an.py b/product_portfolio/migrations/0007_remove_productdependency_product_portfolio_productdependency_unique_dependency_uid_within_product_an.py new file mode 100644 index 00000000..7cc6dbf5 --- /dev/null +++ b/product_portfolio/migrations/0007_remove_productdependency_product_portfolio_productdependency_unique_dependency_uid_within_product_an.py @@ -0,0 +1,21 @@ +# Generated by Django 5.0.6 on 2024-07-16 06:34 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("product_portfolio", "0006_productdependency_and_more"), + ] + + operations = [ + migrations.RemoveConstraint( + model_name="productdependency", + name="product_portfolio_productdependency_unique_dependency_uid_within_product", + ), + migrations.RemoveConstraint( + model_name="productdependency", + name="product_portfolio_productdependency_unique_uuid_within_dataspace", + ), + ] diff --git a/product_portfolio/models.py b/product_portfolio/models.py index e3a58212..f605c6d1 100644 --- a/product_portfolio/models.py +++ b/product_portfolio/models.py @@ -1381,9 +1381,7 @@ class ProductDependency(HistoryFieldsMixin, DataspacedModel): ) is_direct = models.BooleanField( default=False, - help_text=_( - "True if this is a direct, first-level dependency relationship " "for a package." - ), + help_text=_("True if this is a direct, first-level dependency relationship for a package."), ) objects = DataspacedManager.from_queryset(ProductSecuredQuerySet)() @@ -1400,17 +1398,6 @@ class Meta: models.Index(fields=["is_resolved"]), models.Index(fields=["is_direct"]), ] - constraints = [ - models.UniqueConstraint( - fields=["product", "dependency_uid"], - condition=~models.Q(dependency_uid=""), - name="%(app_label)s_%(class)s_unique_dependency_uid_within_product", - ), - models.UniqueConstraint( - fields=["dataspace", "uuid"], - name="%(app_label)s_%(class)s_unique_uuid_within_dataspace", - ), - ] def __str__(self): return self.dependency_uid diff --git a/product_portfolio/templates/product_portfolio/tabs/tab_dependencies.html b/product_portfolio/templates/product_portfolio/tabs/tab_dependencies.html index 6b2ebed7..9c34e5f6 100644 --- a/product_portfolio/templates/product_portfolio/tabs/tab_dependencies.html +++ b/product_portfolio/templates/product_portfolio/tabs/tab_dependencies.html @@ -59,6 +59,9 @@ {{ dependency.for_package }} +
+ {{ dependency.for_package.license_expression_html|default_if_none:"" }} +
{% endif %} @@ -66,6 +69,9 @@ {{ dependency.resolved_to_package }} +
+ {{ dependency.resolved_to_package.license_expression_html|default_if_none:"" }} +
{% endif %}