Skip to content

Commit 035722e

Browse files
committed
Update weighted_risk_score on updating the relationship
Signed-off-by: tdruez <[email protected]>
1 parent fd1b980 commit 035722e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

product_portfolio/models.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ def assign_object(self, obj, user, replace_version=False):
512512
existing_relation = other_assigned_versions[0]
513513
other_version_object = getattr(existing_relation, object_model_name)
514514
existing_relation.update(**{object_model_name: obj, "last_modified_by": user})
515+
# Update the weighted_risk_score from the new related_object
516+
existing_relation.refresh_from_db()
517+
existing_relation.update_weighted_risk_score()
515518
message = f'Updated {object_model_name} "{other_version_object}" to "{obj}"'
516519
History.log_change(user, self, message)
517520
return "updated", existing_relation
@@ -864,9 +867,9 @@ def compute_weighted_risk_score(self):
864867
weighted_risk_score = float(risk_score) * float(exposure_factor)
865868
return weighted_risk_score
866869

867-
def set_weighted_risk_score(self):
870+
def set_weighted_risk_score(self, save=False):
868871
"""
869-
Update the `weighted_risk_score` for the current instance.
872+
Set the `weighted_risk_score` for the current instance.
870873
871874
The method computes the weighted risk score using `compute_weighted_risk_score()`
872875
and assigns the computed value to the `weighted_risk_score` field if it differs
@@ -878,6 +881,13 @@ def set_weighted_risk_score(self):
878881
if weighted_risk_score != self.weighted_risk_score:
879882
self.weighted_risk_score = weighted_risk_score
880883

884+
def update_weighted_risk_score(self):
885+
"""Update the `weighted_risk_score` for the current instance."""
886+
weighted_risk_score = self.compute_weighted_risk_score()
887+
if weighted_risk_score != self.weighted_risk_score:
888+
self.weighted_risk_score = weighted_risk_score
889+
self.raw_update(weighted_risk_score=weighted_risk_score)
890+
881891
def as_spdx(self):
882892
"""
883893
Set the `license_concluded` using the license choice of the relationship,

product_portfolio/tests/test_models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,21 @@ def test_product_model_assign_object_replace_version_package(self):
410410
expected_message = 'Updated package "pkg:deb/debian/[email protected]" to "pkg:deb/debian/[email protected]"'
411411
self.assertEqual(expected_message, history_entries.latest("action_time").change_message)
412412

413+
def test_product_model_assign_object_replace_version_package_update_vulnerability_scores(self):
414+
self.assertEqual(0, self.product1.get_vulnerable_productpackages().count())
415+
package1 = make_package(self.dataspace, name="a", version="1.0", is_vulnerable=True)
416+
p1_p1 = make_product_package(self.product1, package1)
417+
p1_p1.raw_update(weighted_risk_score=5.0)
418+
self.assertTrue(self.product1.productpackages.vulnerable().exists())
419+
420+
package2 = make_package(self.dataspace, name="a", version="2.0")
421+
status, p1_p2 = self.product1.assign_object(package2, self.super_user, replace_version=True)
422+
self.assertEqual("updated", status)
423+
424+
p1_p2.refresh_from_db()
425+
self.assertIsNone(p1_p2.weighted_risk_score)
426+
self.assertFalse(self.product1.productpackages.vulnerable().exists())
427+
413428
def test_product_model_find_assigned_other_versions_component(self):
414429
component1 = Component.objects.create(name="c", version="1.0", dataspace=self.dataspace)
415430
component2 = Component.objects.create(name="c", version="2.0", dataspace=self.dataspace)

0 commit comments

Comments
 (0)