Skip to content

Commit

Permalink
feat: add is_published notion
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Hacene committed Oct 17, 2024
1 parent 8cd02fd commit 0253f45
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
43 changes: 43 additions & 0 deletions backend/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,46 @@ def handle(exc, context):
exc = DRFValidationError(detail=data)

return drf_exception_handler(exc, context)

def duplicate_related_objects(
object, duplicate_object, target_folder, field_name, model_class
):
"""
Duplicates related objects (e.g., controls, threats, assets) from a source object to a duplicate object,
ensuring that objects are not duplicated if they already exist in the/a target/sub domain (folder).
Parameters:
- object (object): The source object containing the related objects to be duplicated.
- duplicate_object (object): The duplicate object where the objects will be added.
- target_folder (object): The target folder where the duplicated objects will be stored.
- field_name (str): The field name representing the related objects to duplicate in the object.
- model_class (class): The model class of the related objects to be processed.
"""

# Get parent folders of the target folder
target_parent_folders = target_folder.get_parent_folders()

# Fetch all related objects for the given field name
related_objects = getattr(object, field_name).all()

for obj in related_objects:
# Check if an object with the same name already exists in the target folder
existing_obj = model_class.objects.filter(
name=obj.name, folder=target_folder
).first()

if existing_obj:
# If the object already exists in the targer folder, add the existing one to the duplicate object
getattr(duplicate_object, field_name).add(existing_obj)

elif obj.folder in target_parent_folders and obj.is_published:
# If the object's folder is a parent of the targert folder and is published, add the object to the duplicate object
getattr(duplicate_object, field_name).add(obj)

else:
# If the object doesn't exist, duplicate the object
duplicate_obj = obj
duplicate_obj.pk = None
duplicate_obj.folder = target_folder
duplicate_obj.save()
getattr(duplicate_object, field_name).add(duplicate_obj)
43 changes: 0 additions & 43 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,49 +598,6 @@ def treatment_plan_pdf(self, request, pk):
serializer_class=RiskAssessmentDuplicateSerializer,
)
def duplicate(self, request, pk):
def duplicate_related_objects(
scenario, duplicate_scenario, target_folder, field_name, model_class
):
"""
Duplicates related objects (e.g., controls, threats, assets) from a source scenario to a duplicate scenario,
ensuring that objects are not duplicated if they already exist in the/a target/parent domain (folder).
Parameters:
- scenario (object): The source scenario containing the related objects to be duplicated.
- duplicate_scenario (object): The duplicate scenario where the objects will be added.
- target_folder (object): The target folder where the duplicated objects will be stored.
- field_name (str): The field name representing the related objects to duplicate in the scenario.
- model_class (class): The model class of the related objects to be processed.
"""

# Get parent folders of the target folder
target_parent_folders = target_folder.get_parent_folders()

# Fetch all related objects for the given field name
related_objects = getattr(scenario, field_name).all()

for obj in related_objects:
# Check if an object with the same name already exists in the target folder
existing_obj = model_class.objects.filter(
name=obj.name, folder=target_folder
).first()

if existing_obj:
# If the object already exists in the targer folder, add the existing one to the duplicate scenario
getattr(duplicate_scenario, field_name).add(existing_obj)

elif obj.folder in target_parent_folders:
# If the object's folder is a parent of the targert folder, add the object to the duplicate scenario
getattr(duplicate_scenario, field_name).add(obj)

else:
# If the object doesn't exist, duplicate the object
duplicate_obj = obj
duplicate_obj.pk = None
duplicate_obj.folder = target_folder
duplicate_obj.save()
getattr(duplicate_scenario, field_name).add(duplicate_obj)

(object_ids_view, _, _) = RoleAssignment.get_accessible_object_ids(
Folder.get_root_folder(), request.user, RiskAssessment
)
Expand Down

0 comments on commit 0253f45

Please sign in to comment.