@@ -1535,53 +1535,47 @@ def create(self, validated_data):
15351535 return instance
15361536
15371537 def update (self , instance , validated_data ):
1538- # Determine findings to risk accept, and findings to unaccept risk
1539- existing_findings = Finding .objects .filter (risk_acceptance = self .instance .id )
1540- new_findings_ids = [x .id for x in validated_data .get ("accepted_findings" , [])]
1541- new_findings = Finding .objects .filter (id__in = new_findings_ids )
1542- findings_to_add = set (new_findings ) - set (existing_findings )
1543- findings_to_remove = set (existing_findings ) - set (new_findings )
1544- findings_to_add = Finding .objects .filter (id__in = [x .id for x in findings_to_add ])
1545- findings_to_remove = Finding .objects .filter (id__in = [x .id for x in findings_to_remove ])
1538+ if "accepted_findings" in validated_data :
1539+ # Determine findings to risk accept, and findings to unaccept risk
1540+ existing_findings = Finding .objects .filter (risk_acceptance = self .instance .id )
1541+ new_findings_ids = [x .id for x in validated_data .get ("accepted_findings" , [])]
1542+ new_findings = Finding .objects .filter (id__in = new_findings_ids )
1543+ findings_to_add = set (new_findings ) - set (existing_findings )
1544+ findings_to_remove = set (existing_findings ) - set (new_findings )
1545+ findings_to_add = Finding .objects .filter (id__in = [x .id for x in findings_to_add ])
1546+ findings_to_remove = Finding .objects .filter (id__in = [x .id for x in findings_to_remove ])
1547+ else :
1548+ findings_to_remove = findings_to_add = []
1549+
15461550 # Make the update in the database
15471551 instance = super ().update (instance , validated_data )
1548- user = getattr (self .context .get ("request" , None ), "user" , None )
1549- # Add the new findings
1550- ra_helper .add_findings_to_risk_acceptance (user , instance , findings_to_add )
1551- # Remove the ones that were not present in the payload
1552- for finding in findings_to_remove :
1553- ra_helper .remove_finding_from_risk_acceptance (user , instance , finding )
1552+
1553+ if findings_to_add or findings_to_remove :
1554+ user = getattr (self .context .get ("request" , None ), "user" , None )
1555+ # Add the new findings
1556+ ra_helper .add_findings_to_risk_acceptance (user , instance , findings_to_add )
1557+ # Remove the ones that were not present in the payload
1558+ for finding in findings_to_remove :
1559+ ra_helper .remove_finding_from_risk_acceptance (user , instance , finding )
15541560 return instance
15551561
15561562 @extend_schema_field (serializers .CharField ())
15571563 def get_path (self , obj ):
1558- engagement = Engagement .objects .filter (
1559- risk_acceptance__id__in = [obj .id ],
1560- ).first ()
15611564 path = "No proof has been supplied"
1562- if engagement and obj .filename () is not None :
1565+ if obj .filename () is not None :
15631566 path = reverse (
1564- "download_risk_acceptance" , args = (engagement .id , obj .id ),
1567+ "download_risk_acceptance" , args = (obj . engagement .id , obj .id ),
15651568 )
15661569 request = self .context .get ("request" )
15671570 if request :
15681571 path = request .build_absolute_uri (path )
15691572 return path
15701573
1571- @extend_schema_field (serializers .IntegerField ())
1572- def get_engagement (self , obj ):
1573- engagement = Engagement .objects .filter (
1574- risk_acceptance__id__in = [obj .id ],
1575- ).first ()
1576- return EngagementSerializer (read_only = True ).to_representation (
1577- engagement ,
1578- )
1579-
15801574 def validate (self , data ):
1581- def validate_findings_have_same_engagement (finding_objects : list [Finding ]):
1575+ def validate_findings_have_same_engagement (finding_objects : list [Finding ]): # TODO: check
15821576 engagements = finding_objects .values_list ("test__engagement__id" , flat = True ).distinct ().count ()
15831577 if engagements > 1 :
1584- msg = "You are not permitted to add findings from multiple engagements"
1578+ msg = "You are not permitted to add findings from multiple engagements" # TODO: same is missing for UI
15851579 raise PermissionDenied (msg )
15861580
15871581 findings = data .get ("accepted_findings" , [])
0 commit comments