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 d6923e8 commit 953a9a6
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions backend/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,17 +1190,23 @@ def duplicate_related_objects(
- field_name (str): The field name representing the related objects in the source object.
- model_class (class): The model class of the related objects.
"""

def process_related_object(
obj, duplicate_object, target_folder, target_parent_folders, sub_folders, field_name, model_class
obj,
duplicate_object,
target_folder,
target_parent_folders,
sub_folders,
field_name,
model_class,
):
"""
Process a single related object: add, link, or duplicate it based on folder and existence checks.
"""

# Check if the object already exists in the target folder
existing_obj = get_existing_object(obj, target_folder, model_class)

if existing_obj:
# If the object exists in the target folder, link it to the duplicate object
link_existing_object(duplicate_object, existing_obj, field_name)
Expand All @@ -1217,29 +1223,26 @@ def process_related_object(
# Otherwise, duplicate the object and link it
duplicate_and_link_object(obj, duplicate_object, target_folder, field_name)


def get_existing_object(obj, target_folder, model_class):
"""
Check if an object with the same name already exists in the target folder.
"""
return model_class.objects.filter(name=obj.name, folder=target_folder).first()


def link_existing_object(duplicate_object, existing_obj, field_name):
"""
Link an existing object to the duplicate object by adding it to the related field.
"""
getattr(duplicate_object, field_name).add(existing_obj)


def duplicate_and_link_object(obj, duplicate_object, target_folder, field_name):
"""
Duplicate an object and link it to the duplicate object.
"""
duplicate_obj = obj
duplicate_obj.pk = None # Reset primary key to create a new object
duplicate_obj.pk = None
duplicate_obj.folder = target_folder
duplicate_obj.save() # Save the new duplicated object
duplicate_obj.save()
getattr(duplicate_object, field_name).add(duplicate_obj)

# Get parent and sub-folders of the target folder
Expand All @@ -1251,5 +1254,12 @@ def duplicate_and_link_object(obj, duplicate_object, target_folder, field_name):

# Process each related object
for obj in related_objects:
# Determine if the object should be added, linked, or duplicated
process_related_object(obj, duplicate_object, target_folder, target_parent_folders, sub_folders, field_name, model_class)
process_related_object(
obj,
duplicate_object,
target_folder,
target_parent_folders,
sub_folders,
field_name,
model_class,
)

0 comments on commit 953a9a6

Please sign in to comment.