Skip to content

Commit

Permalink
Refine the assign_object method #12
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed May 24, 2024
1 parent a38b287 commit a9ffcde
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
4 changes: 1 addition & 3 deletions component_catalog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,7 @@ class AddToProductAdminForm(forms.Form):
)
ids = forms.CharField(widget=forms.widgets.HiddenInput)
replace_existing_version = forms.BooleanField(
required=False,
initial=False,
label="Replace existing relationships by newer version."
required=False, initial=False, label="Replace existing relationships by newer version."
)

def __init__(self, request, model, relation_model, *args, **kwargs):
Expand Down
37 changes: 23 additions & 14 deletions product_portfolio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,36 +333,45 @@ def get_cyclonedx_components(self):
]

def assign_object(self, obj, user, replace_existing_version=False):
"""
Assign a provided ``obj`` (either a ``ProductComponent`` or
``ProductPackage``) to this ``Product``.
Return The created or existing relationship object.
"""
relationship_models = {
"component": ProductComponent,
"package": ProductPackage,
}
object_model_name = obj._meta.model_name # 'component' or 'package'
object_model_name = obj._meta.model_name # "component" or "package"
object_model_class = relationship_models.get(object_model_name)
if not object_model_class:
raise ValueError
raise ValueError(f"Unsupported object model: {object_model_name}")

filters = {
"product": self,
object_model_name: obj,
"dataspace": obj.dataspace,
"defaults": {
"license_expression": obj.license_expression,
"created_by": user,
"last_modified_by": user,
},
object_model_name: obj,
}
defaults = {
"license_expression": obj.license_expression,
"created_by": user,
"last_modified_by": user,
}
relation_obj, created = object_model_class.objects.get_or_create(**filters)

relation_obj, created = object_model_class.objects.get_or_create(
defaults=defaults, **filters
)

if created:
History.log_addition(user, relation_obj)
History.log_change(user, self, f'Added {object_model_name} "{obj}"')
return relation_obj

return relation_obj if created else None

def assign_objects(self, related_objects, user, replace_existing_version=False):
"""
Assign provided `related_objects` to this `Product`.
Supported object models are `Component` and `Package`.
Return the both counts for created and unchanged objects.
Assign provided ``related_objects`` (either ``ProductComponent`` or
``ProductPackage``) to this ``Product``.
"""
created_count = 0
unchanged_count = 0
Expand All @@ -374,7 +383,7 @@ def assign_objects(self, related_objects, user, replace_existing_version=False):
else:
unchanged_count += 1

if created_count:
if created_count > 0:
self.last_modified_by = user
self.save()

Expand Down
3 changes: 2 additions & 1 deletion product_portfolio/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ def test_product_model_assign_objects(self):
)
self.assertEqual(0, self.product1.productcomponents.count())

with self.assertRaises(ValueError):
with self.assertRaises(ValueError) as cm:
self.product1.assign_objects([status1], self.super_user)
self.assertEqual("Unsupported object model: productrelationstatus", str(cm.exception))

created, unchanged = self.product1.assign_objects([], self.super_user)
self.assertEqual(0, created)
Expand Down

0 comments on commit a9ffcde

Please sign in to comment.