Skip to content

Commit

Permalink
chore: run format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Hacene committed Oct 17, 2024
1 parent 1e88dbf commit 5fbb9f7
Showing 1 changed file with 47 additions and 46 deletions.
93 changes: 47 additions & 46 deletions backend/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,50 +1176,51 @@ def handle(exc, context):

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()
sub_folders = target_folder.sub_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)
elif obj.folder in sub_folders:
# If the object's folder is a subfolder of the target folder, 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)
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()
sub_folders = target_folder.sub_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)

elif obj.folder in sub_folders:
# If the object's folder is a subfolder of the target folder, 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)

0 comments on commit 5fbb9f7

Please sign in to comment.