-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to fix multi taxa groups in one survey (#3675)
- Loading branch information
1 parent
5f86293
commit fc69abb
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
scripts/management/commands/fix_multi_taxa_groups_in_one_survey.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,75 @@ | ||
# for issue https://github.com/kartoza/django-bims/issues/3491 | ||
from django.core.management import BaseCommand | ||
from django.db.models import signals, Count | ||
from bims.models import ( | ||
location_site_post_save_handler, | ||
location_context_post_save_handler, LocationContextGroup, | ||
LocationContextFilter, LocationContextFilterGroupOrder, | ||
BiologicalCollectionRecord | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
# Update location sites to use | ||
# legacy site codes if they have correct format | ||
|
||
def handle(self, *args, **options): | ||
# Query | ||
records_with_multiple_module_groups = BiologicalCollectionRecord.objects.values( | ||
'survey' | ||
).annotate( | ||
module_group_count=Count('module_group', distinct=True) | ||
).filter( | ||
module_group_count__gt=1 | ||
) | ||
|
||
signals.post_save.disconnect( | ||
location_site_post_save_handler | ||
) | ||
signals.post_save.disconnect( | ||
location_context_post_save_handler, | ||
sender=LocationContextGroup | ||
) | ||
signals.post_save.disconnect( | ||
location_context_post_save_handler, | ||
sender=LocationContextFilter | ||
) | ||
signals.post_save.disconnect( | ||
location_context_post_save_handler, | ||
sender=LocationContextFilterGroupOrder | ||
) | ||
|
||
index = 0 | ||
|
||
for record in records_with_multiple_module_groups: | ||
bio_records = BiologicalCollectionRecord.objects.filter( | ||
survey=record['survey'] | ||
) | ||
survey = bio_records.first().survey | ||
module_groups = list(bio_records.values_list( | ||
'module_group', flat=True | ||
).distinct()) | ||
print('old survey : {}'.format(survey.id)) | ||
for module_group in module_groups[1:]: | ||
survey.pk = None | ||
survey.save() | ||
bio_records.filter( | ||
module_group=module_group | ||
).update(survey=survey) | ||
print('new survey : {}'.format(survey.id)) | ||
|
||
signals.post_save.connect( | ||
location_site_post_save_handler | ||
) | ||
signals.post_save.connect( | ||
location_context_post_save_handler, | ||
sender=LocationContextGroup | ||
) | ||
signals.post_save.connect( | ||
location_context_post_save_handler, | ||
sender=LocationContextFilter | ||
) | ||
signals.post_save.connect( | ||
location_context_post_save_handler, | ||
sender=LocationContextFilterGroupOrder | ||
) |