From a20d885f722d311be62307eabf3aefed48a7dda4 Mon Sep 17 00:00:00 2001 From: k9845 Date: Thu, 22 Feb 2024 10:15:05 +0545 Subject: [PATCH 1/3] Add fix for search page --- api/views.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/api/views.py b/api/views.py index fa695e652..a38e4f2fe 100644 --- a/api/views.py +++ b/api/views.py @@ -167,9 +167,10 @@ def get(self, request): SearchQuerySet() .models(Personnel) .filter( - SQ(deploying_country_name__contains=phrase) + (SQ(deploying_country_name__contains=phrase) | SQ(deployed_to_country_name__contains=phrase) - | SQ(event_name__content=phrase) + | SQ(event_name__content=phrase)) + & SQ(end_date__gt=datetime.now()) ) .order_by("-_score") ) @@ -177,7 +178,8 @@ def get(self, request): SearchQuerySet() .models(SurgeAlert) .filter( - SQ(event_name__content=phrase) | SQ(country_name__contains=phrase) | SQ(iso3__contains=phrase) + (SQ(event_name__content=phrase) | SQ(country_name__contains=phrase) | SQ(iso3__contains=phrase)) + & ~SQ(status='archived') ) .order_by("-_score") ) @@ -223,6 +225,7 @@ def get(self, request): | SQ(event_name__content=phrase) ) & ~SQ(visibility="IFRC Only") + & SQ(end_date__gt=datetime.now()) ) .order_by("-_score") ) @@ -232,6 +235,7 @@ def get(self, request): .filter( (SQ(event_name__content=phrase) | SQ(country_name__contains=phrase) | SQ(iso3__contains=phrase)) & ~SQ(visibility="IFRC Only") + & ~SQ(status='archived') ) .order_by("-_score") ) @@ -278,6 +282,7 @@ def get(self, request): | SQ(event_name__content=phrase) ) & SQ(visibility="Public") + & SQ(end_date__gt=datetime.now()) ) .order_by("-_score") ) @@ -287,6 +292,7 @@ def get(self, request): .filter( (SQ(event_name__content=phrase) | SQ(country_name__contains=phrase) | SQ(iso3__contains=phrase)) & SQ(visibility="Public") + & ~SQ(status='archived') ) .order_by("-_score") ) From b87e01e4b47f0fea6581616ee57b3e38cdc205b0 Mon Sep 17 00:00:00 2001 From: k9845 Date: Thu, 22 Feb 2024 13:59:17 +0545 Subject: [PATCH 2/3] Add migration script for per notes --- per/migrations/0099_migrate_notes.py | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 per/migrations/0099_migrate_notes.py diff --git a/per/migrations/0099_migrate_notes.py b/per/migrations/0099_migrate_notes.py new file mode 100644 index 000000000..fd2366021 --- /dev/null +++ b/per/migrations/0099_migrate_notes.py @@ -0,0 +1,43 @@ +# Generated by Django 3.2.23 on 2024-02-22 05:45 +from django.db import migrations +from django.db import models + +from api.logger import logger + + +class Migration(migrations.Migration): + def migrate_formdata_notes(apps, schema_editor): + # NOTE: No any exact match for relationships + FormComponentResponse = apps.get_model("per", "FormComponentResponse") + FormData = apps.get_model("per", "FormData") + qs = FormComponentResponse.objects.annotate( + new_notes=models.Subquery( + FormData.objects.filter( + question__component=models.OuterRef("component"), + question=74, + ) + .order_by( + "-form__area", + "-form__created_at", + "form", + "question__question_num", + ) + .filter(notes__isnull=False) + .values("notes_en")[:1], + output_field=models.CharField(), + ), + ).filter(new_notes__isnull=False) + + print( + qs.update( + notes=models.F("new_notes"), + ) + ) + + dependencies = [ + ("per", "0098_fix_reversion_data_20240208_0502"), + ] + + operations = [ + migrations.RunPython(migrate_formdata_notes, reverse_code=migrations.RunPython.noop), + ] From e64cc67833fc40aad61f941c8e5ad7707f25be1b Mon Sep 17 00:00:00 2001 From: thenav56 Date: Mon, 26 Feb 2024 18:17:55 +0545 Subject: [PATCH 3/3] Using overview to match legacy data --- per/migrations/0099_migrate_notes.py | 58 +++++++++++++++------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/per/migrations/0099_migrate_notes.py b/per/migrations/0099_migrate_notes.py index fd2366021..d1544f74e 100644 --- a/per/migrations/0099_migrate_notes.py +++ b/per/migrations/0099_migrate_notes.py @@ -2,38 +2,42 @@ from django.db import migrations from django.db import models -from api.logger import logger +def migrate_formdata_notes(apps, schema_editor): + FormComponentResponse = apps.get_model("per", "FormComponentResponse") + FormData = apps.get_model("per", "FormData") -class Migration(migrations.Migration): - def migrate_formdata_notes(apps, schema_editor): - # NOTE: No any exact match for relationships - FormComponentResponse = apps.get_model("per", "FormComponentResponse") - FormData = apps.get_model("per", "FormData") - qs = FormComponentResponse.objects.annotate( - new_notes=models.Subquery( - FormData.objects.filter( - question__component=models.OuterRef("component"), - question=74, - ) - .order_by( - "-form__area", - "-form__created_at", - "form", - "question__question_num", - ) - .filter(notes__isnull=False) - .values("notes_en")[:1], - output_field=models.CharField(), - ), - ).filter(new_notes__isnull=False) - - print( - qs.update( - notes=models.F("new_notes"), + qs = FormComponentResponse.objects.annotate( + new_notes=models.Subquery( + # Copy notes from FormData using overview + FormData.objects.filter( + form__overview=models.OuterRef('arearesponse__perassessment__overview'), + question__component=models.OuterRef('component'), + question=74, + ).values('notes_en'), + output_field=models.CharField(), + ), + ).filter(new_notes__isnull=False) + + form_component_responses = [] + for form_component_response_id, assessment_id, new_notes in qs.values_list( + 'id', + models.F('arearesponse__perassessment'), + 'new_notes', + ): + print(f'- Copying data for {assessment_id=} {form_component_response_id=}') + form_component_responses.append( + FormComponentResponse( + id=form_component_response_id, + notes=new_notes, ) ) + print(f'Total form_component_responses notes copied: {len(form_component_responses)}') + FormComponentResponse.objects.bulk_update(form_component_responses, fields=('notes',)) + + +class Migration(migrations.Migration): dependencies = [ ("per", "0098_fix_reversion_data_20240208_0502"), ]