Skip to content

Commit

Permalink
Add an improve_packages_from_purldb method and view #45
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed May 28, 2024
1 parent e9490d2 commit b0a0178
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions product_portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ def get_import_manifests_url(self):
def get_pull_project_data_url(self):
return self.get_url("pull_project_data")

def get_improve_packages_from_purldb_url(self):
return self.get_url("improve_packages_from_purldb")

def can_be_changed_by(self, user):
perms = guardian.shortcuts.get_perms(user, self)
has_change_permission_on_product = "change_product" in perms
Expand Down Expand Up @@ -454,6 +457,15 @@ def scan_all_packages_task(self, user):
dataspace_uuid=user.dataspace.uuid,
)

def improve_packages_from_purldb(self, user):
"""Update all Packages assigned to the Product using PurlDB data."""
updated_packages = []
for package in self.packages.all():
updated_fields = package.update_from_purldb(user)
if updated_fields:
updated_packages.append(package)
return updated_packages


class ProductRelationStatus(BaseStatusMixin, DataspacedModel):
class Meta(BaseStatusMixin.Meta):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

{% if has_scan_all_packages or has_change_permission %}
<div class="dropdown btn-group">
<a class="btn btn-outline-dark dropdown-toggle" data-bs-toggle="dropdown" role="button" href="#"><i class="fas fa-barcode"></i>&nbsp;Scan</a>
<a class="btn btn-outline-dark dropdown-toggle" data-bs-toggle="dropdown" role="button" href="#"><i class="fas fa-toolbox"></i>&nbsp;Actions</a>
<div class="dropdown-menu dropdown-menu-end">
<div class="dropdown-header">Import</div>
{% if has_change_permission %}
<a class="dropdown-item" href="{{ object.get_import_from_scan_url }}"><i class="fas fa-file-upload"></i> {% trans 'Import data from Scan' %}</a>
{% if request.user.dataspace.enable_package_scanning %}
Expand All @@ -46,8 +47,13 @@
{% endif %}
{% endif %}
{% if has_scan_all_packages %}
<div class="dropdown-header">ScanCode.io</div>
<a class="dropdown-item{% if not tabsets.Inventory %} disabled{% endif %}" href="#" data-bs-toggle="modal" data-bs-target="#scan-all-packages-modal"><i class="fas fa-barcode"></i> {% trans 'Scan all Packages' %}</a>
{% endif %}
{% if has_change_permission %}{# TODO: Only if PurlDB is configured! #}
<div class="dropdown-header">PurlDB</div>
<a class="dropdown-item" href="{{ object.get_improve_packages_from_purldb_url }}"><i class="fas fa-database"></i> {% trans 'Improve Packages from PurlDB' %}</a>
{% endif %}
</div>
</div>
{% endif %}
Expand Down
2 changes: 2 additions & 0 deletions product_portfolio/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from product_portfolio.views import edit_productrelation_ajax_view
from product_portfolio.views import import_from_scan_view
from product_portfolio.views import import_packages_from_scancodeio_view
from product_portfolio.views import improve_packages_from_purldb_view
from product_portfolio.views import license_summary_view
from product_portfolio.views import scan_all_packages_view
from product_portfolio.views import scancodeio_project_status_view
Expand Down Expand Up @@ -77,6 +78,7 @@ def product_path(path_segment, view):
),
*product_path("add_customcomponent_ajax", add_customcomponent_ajax_view),
*product_path("scan_all_packages", scan_all_packages_view),
*product_path("improve_packages_from_purldb", improve_packages_from_purldb_view),
*product_path("about_files", ProductSendAboutFilesView.as_view()),
*product_path("export_spdx", ProductExportSPDXDocumentView.as_view()),
*product_path("export_cyclonedx", ProductExportCycloneDXBOMView.as_view()),
Expand Down
27 changes: 27 additions & 0 deletions product_portfolio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,3 +1997,30 @@ def scancodeio_project_status_view(request, scancodeproject_uuid):
}

return TemplateResponse(request, template, context)


@login_required
def improve_packages_from_purldb_view(request, dataspace, name, version=""):
user = request.user

purldb = PurlDB(user)
conditions = [
purldb.is_configured(),
# user.is_superuser,
user.dataspace.enable_purldb_access,
user.dataspace.name == dataspace,
]

if not all(conditions):
raise Http404

guarded_qs = Product.objects.get_queryset(user)
product = get_object_or_404(guarded_qs, name=unquote_plus(name), version=unquote_plus(version))

if not product.packages.count():
raise Http404("No packages available for this product.")

transaction.on_commit(lambda: product.improve_packages_from_purldb(user))
messages.success(request, "Improve Packages from PurlDB in progress...")

return redirect(product)

0 comments on commit b0a0178

Please sign in to comment.