Skip to content

Commit

Permalink
Refine the rendering of license_expression #138
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jul 16, 2024
1 parent 17b8f6b commit c75204e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
6 changes: 5 additions & 1 deletion component_catalog/license_expression_dje.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
'<span class="license-expression">{}</span>',
mark_safe(formatted_expression), # nosec
)


def get_expression_as_spdx(expression, dataspace):
Expand Down
6 changes: 6 additions & 0 deletions component_catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
]
15 changes: 1 addition & 14 deletions product_portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@
<strong>
<a href="{{ dependency.for_package.get_absolute_url }}" target="_blank">{{ dependency.for_package }}</a>
</strong>
<div>
{{ dependency.for_package.license_expression_html|default_if_none:"" }}
</div>
{% endif %}
</td>
<td>
{% if dependency.resolved_to_package %}
<strong>
<a href="{{ dependency.resolved_to_package.get_absolute_url }}" target="_blank">{{ dependency.resolved_to_package }}</a>
</strong>
<div class="license-expression">
{{ dependency.resolved_to_package.license_expression_html|default_if_none:"" }}
</div>
{% endif %}
</td>
<td>
Expand Down

0 comments on commit c75204e

Please sign in to comment.