Skip to content

Commit

Permalink
Update queryset
Browse files Browse the repository at this point in the history
  • Loading branch information
sudip-khanal committed Jan 7, 2025
1 parent 83248aa commit 629fd28
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions per/management/commands/migrate_sub_components_to_component14.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ class Command(BaseCommand):
help = "Migration of sub components of component 14 to component 14"

def handle(self, *args, **kwargs):
parent_component_14 = FormComponent.objects.filter(component_num=14, is_parent=True)

parent_component_14 = FormComponent.objects.filter(component_num=14, is_parent=True).first()
sub_components_14 = FormComponent.objects.filter(component_num=14, is_parent__isnull=True)

if not sub_components_14.exists():
self.stdout.write(self.style.WARNING("No sub components found for component 14"))

return

ops_learnings = OpsLearning.objects.filter(per_component__in=sub_components_14)
sub_components_14_ids = sub_components_14.values_list("id", flat=True)

ops_learnings = OpsLearning.objects.filter(
per_component__in=sub_components_14_ids, per_component_validated__in=sub_components_14_ids
)
for ops_learning in ops_learnings:
if ops_learning.is_validated:
sub_components_in_validated = ops_learning.per_component_validated.filter(id__in=sub_components_14)
if sub_components_in_validated.exists():
ops_learning.per_component_validated.remove(*sub_components_14)
ops_learning.per_component_validated.add(parent_component_14)

sub_components_in_per_component = ops_learning.per_component.filter(id__in=sub_components_14)
if sub_components_in_per_component.exists():
ops_learning.per_component.remove(*sub_components_14)
ops_learning.per_component.add(parent_component_14)
ops_learning.per_component_validated.remove(*sub_components_14_ids)
ops_learning.per_component_validated.add(parent_component_14)
ops_learning.per_component.remove(*sub_components_14_ids)
ops_learning.per_component.add(parent_component_14)

ops_learning.save()
self.stdout.write(self.style.SUCCESS("Migration of sub components of component 14 to component 14 is done"))
self.stdout.write(self.style.SUCCESS("Migration of sub components of component 14 to component 14 is done"))

0 comments on commit 629fd28

Please sign in to comment.