Skip to content

Commit

Permalink
Add the extra_data field on the DiscoveredPackage model #191
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Jul 1, 2021
1 parent b18319e commit b24b279
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

### unreleased

- Add the extra_data field on the DiscoveredPackage model.
https://github.com/nexB/scancode.io/issues/191

- Improve XLSX creation. We now check that the content is correctly added before
calling XlsxWriter and report and error if the truncated can be truncated.
https://github.com/nexB/scancode.io/issues/206
Expand Down
23 changes: 23 additions & 0 deletions scanpipe/migrations/0008_package_extra_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.4 on 2021-07-01 15:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('scanpipe', '0007_resource_is_binary_is_text_is_archive'),
]

operations = [
migrations.AddField(
model_name='discoveredpackage',
name='extra_data',
field=models.JSONField(blank=True, default=dict, help_text='Optional mapping of extra data key/values.'),
),
migrations.AlterField(
model_name='project',
name='extra_data',
field=models.JSONField(blank=True, default=dict, help_text='Optional mapping of extra data key/values.'),
),
]
56 changes: 37 additions & 19 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ def set_task_ended(self, exitcode, output, refresh_first=True):
self.save()


class ExtraDataFieldMixin(models.Model):
"""
Adds the `extra_data` field and helper methods.
"""

extra_data = models.JSONField(
default=dict,
blank=True,
help_text=_("Optional mapping of extra data key/values."),
)

def update_extra_data(self, data):
"""
Update the `extra_data` field with the provide `data` dict.
"""
if type(data) != dict:
raise ValueError("Argument `data` value must be a dict()")

self.extra_data.update(data)
self.save()

class Meta:
abstract = True


def get_project_work_directory(project):
"""
Return the work directory location for the provided `project`.
Expand All @@ -194,7 +219,7 @@ def get_project_work_directory(project):
return f"{WORKSPACE_LOCATION}/projects/{slugify(project.name)}-{project.short_uuid}"


class Project(UUIDPKModel, models.Model):
class Project(UUIDPKModel, ExtraDataFieldMixin, models.Model):
"""
The Project encapsulate all analysis processing.
Multiple analysis pipelines can be run on the project.
Expand All @@ -218,7 +243,6 @@ class Project(UUIDPKModel, models.Model):
help_text=_("Project work directory location."),
)
input_sources = models.JSONField(default=dict, blank=True, editable=False)
extra_data = models.JSONField(default=dict, editable=False)

class Meta:
ordering = ["-created_date"]
Expand Down Expand Up @@ -275,16 +299,6 @@ def reset(self, keep_input=True):

self.setup_work_directory()

def update_extra_data(self, data):
"""
Update the project `extra_data` field with the provide `data` dict.
"""
if type(data) != dict:
raise ValueError("Argument `data` value must be a dict()")

self.extra_data.update(data)
self.save()

def setup_work_directory(self):
"""
Create all the work_directory structure, skip existing.
Expand Down Expand Up @@ -918,7 +932,11 @@ def copy_scan_results(self, from_instance, save=False):


class CodebaseResource(
ProjectRelatedModel, ScanFieldsModelMixin, SaveProjectErrorMixin, AbstractResource
ProjectRelatedModel,
ScanFieldsModelMixin,
ExtraDataFieldMixin,
SaveProjectErrorMixin,
AbstractResource,
):
rootfs_path = models.CharField(
max_length=2000,
Expand Down Expand Up @@ -947,11 +965,6 @@ class Type(models.TextChoices):
"Type of this resource as one of: {}".format(", ".join(Type.values))
),
)
extra_data = models.JSONField(
default=dict,
blank=True,
help_text=_("Optional mapping of extra data key/values."),
)
name = models.CharField(
max_length=255,
blank=True,
Expand Down Expand Up @@ -1165,7 +1178,12 @@ class DiscoveredPackageQuerySet(PackageURLQuerySetMixin, ProjectRelatedQuerySet)
pass


class DiscoveredPackage(ProjectRelatedModel, SaveProjectErrorMixin, AbstractPackage):
class DiscoveredPackage(
ProjectRelatedModel,
ExtraDataFieldMixin,
SaveProjectErrorMixin,
AbstractPackage,
):
codebase_resources = models.ManyToManyField(
"CodebaseResource", related_name="discovered_packages"
)
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def test_scanpipe_api_serializer_get_model_serializer(self):
get_model_serializer(None)

def test_scanpipe_api_serializer_get_serializer_fields(self):
self.assertEqual(28, len(get_serializer_fields(DiscoveredPackage)))
self.assertEqual(29, len(get_serializer_fields(DiscoveredPackage)))
self.assertEqual(24, len(get_serializer_fields(CodebaseResource)))
with self.assertRaises(LookupError):
get_serializer_fields(None)

0 comments on commit b24b279

Please sign in to comment.