-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to remove subcomponent of component14 (#2372)
* Add logic to remove subcomponent of component14 * Fixed pre-commit check fails * Add command instead of migration file * Remove unused import * Update queryset * Update queryset for sub component 14 * Update query for subcomponent to parent * Changes on validated per component and add more test checks * Remove unused codes * Changes test cases for migrate component 14 --------- Co-authored-by: Sushil Tiwari <[email protected]>
- Loading branch information
1 parent
c54ef91
commit 059b5ff
Showing
4 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
per/management/commands/migrate_sub_components_to_component14.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from per.models import FormComponent, OpsLearning | ||
|
||
|
||
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).first() | ||
|
||
if not parent_component_14: | ||
self.stdout.write(self.style.ERROR("No parent component found for component 14")) | ||
return | ||
|
||
sub_components_14_ids = FormComponent.objects.filter(component_num=14, is_parent__isnull=True).values_list( | ||
"id", flat=True | ||
) | ||
|
||
if not sub_components_14_ids.exists(): | ||
self.stdout.write(self.style.ERROR("No sub components found for component 14")) | ||
return | ||
|
||
# Get OpsLearning IDs that already have parent component | ||
with_parent_component_ops_learning_qs = OpsLearning.objects.filter(per_component=parent_component_14).values_list( | ||
"id", flat=True | ||
) | ||
|
||
# For per_component | ||
# Removing if already have parent component | ||
print( | ||
OpsLearning.per_component.through.objects.filter( | ||
formcomponent_id__in=sub_components_14_ids, opslearning_id__in=with_parent_component_ops_learning_qs | ||
).delete() | ||
) | ||
|
||
# Removing all Sub-Components except one and updating to parent component | ||
OpsLearning.per_component.through.objects.filter(formcomponent_id__in=sub_components_14_ids).exclude( | ||
id__in=OpsLearning.per_component.through.objects.filter(formcomponent_id__in=sub_components_14_ids).distinct( | ||
"opslearning_id" | ||
) | ||
).delete() | ||
|
||
OpsLearning.per_component.through.objects.filter(formcomponent_id__in=sub_components_14_ids).update( | ||
formcomponent_id=parent_component_14.id | ||
) | ||
|
||
# For per_component_validated | ||
# Get OpsLearning IDs that already have parent component validated | ||
with_parent_component_validated_ops_learning_qs = OpsLearning.objects.filter( | ||
per_component_validated=parent_component_14 | ||
).values_list("id", flat=True) | ||
|
||
# Removing if already have parent component | ||
print( | ||
OpsLearning.per_component_validated.through.objects.filter( | ||
formcomponent_id__in=sub_components_14_ids, opslearning_id__in=with_parent_component_validated_ops_learning_qs | ||
).delete() | ||
) | ||
|
||
# Removing all Sub-Components except one and updating to parent component | ||
OpsLearning.per_component_validated.through.objects.filter(formcomponent_id__in=sub_components_14_ids).exclude( | ||
id__in=OpsLearning.per_component_validated.through.objects.filter( | ||
formcomponent_id__in=sub_components_14_ids | ||
).distinct("opslearning_id") | ||
).delete() | ||
|
||
OpsLearning.per_component_validated.through.objects.filter(formcomponent_id__in=sub_components_14_ids).update( | ||
formcomponent_id=parent_component_14.id | ||
) | ||
|
||
self.stdout.write(self.style.SUCCESS("Successfully migrated sub-components of component-14 to component-14")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters