From 8f184274527c73245756fce0641e9fcac3b1a68e Mon Sep 17 00:00:00 2001 From: sudan45 Date: Tue, 5 Mar 2024 11:51:29 +0545 Subject: [PATCH 1/2] Remove deleted widgets from export --- apps/analysis_framework/admin.py | 1 + apps/analysis_framework/dataloaders.py | 3 ++- apps/analysis_framework/export.py | 2 +- apps/analysis_framework/serializers.py | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/analysis_framework/admin.py b/apps/analysis_framework/admin.py index 9c50badab6..d79b7151a8 100644 --- a/apps/analysis_framework/admin.py +++ b/apps/analysis_framework/admin.py @@ -55,6 +55,7 @@ class AFRelatedAdmin(JFModelAdmin): '__str__', linkify('analysis_framework'), ) autocomplete_fields = ('analysis_framework',) + list_filter = ['analysis_framework'] def get_queryset(self, request): return super().get_queryset(request).prefetch_related('analysis_framework') diff --git a/apps/analysis_framework/dataloaders.py b/apps/analysis_framework/dataloaders.py index c4508144a7..7182525f23 100644 --- a/apps/analysis_framework/dataloaders.py +++ b/apps/analysis_framework/dataloaders.py @@ -25,7 +25,8 @@ def load_widgets(keys, parent, **filters): **{ f'{parent}__in': keys, **filters, - } + }, + is_deleted=False ).exclude(widget_id__in=Widget.DEPRECATED_TYPES)\ .annotate(conditional_parent_widget_type=models.F('conditional_parent_widget__widget_id'))\ .order_by('order', 'id') diff --git a/apps/analysis_framework/export.py b/apps/analysis_framework/export.py index 1167b198ab..980c5615b0 100644 --- a/apps/analysis_framework/export.py +++ b/apps/analysis_framework/export.py @@ -27,7 +27,7 @@ def export_af_to_csv(af, file): writer = csv.DictWriter(file, fieldnames=AF_EXPORT_COLUMNS) writer.writeheader() - for widget in af.widget_set.order_by('widget_id'): + for widget in af.widget_set.filter(is_deleted=False).order_by('widget_id'): w_type = widget.widget_id w_title = widget.title diff --git a/apps/analysis_framework/serializers.py b/apps/analysis_framework/serializers.py index a29f3545a7..976d6e3114 100644 --- a/apps/analysis_framework/serializers.py +++ b/apps/analysis_framework/serializers.py @@ -386,7 +386,7 @@ def perform_nested_delete_or_update(self, pks_to_delete, model_class, instance, section=self.instance, # NOTE: Adding this additional filter just to make sure pk__in=pks_to_delete ) - qs.delete() + qs.update(is_deleted=True) # NOTE: This is a custom function (apps/user_resource/serializers.py::UserResourceSerializer) # This makes sure only scoped (individual AF) instances (widgets) are updated. @@ -560,7 +560,7 @@ def _delete_old_secondary_taggings(self, af, secondary_tagging): analysis_framework=af, section__isnull=True, # NOTE: section are null for secondary taggings ).exclude(pk__in=current_ids) # Exclude current provided widgets - qs_to_delete.delete() + qs_to_delete.update(is_deleted=True) def _save_secondary_taggings(self, af, secondary_tagging): # Create secondary tagging widgets (Primary/Section widgets are created using WritableNestedModelSerializer) From 2ebe6d5dec1fb88966457690e31fc83a5655c2ec Mon Sep 17 00:00:00 2001 From: sudan45 Date: Thu, 16 May 2024 11:28:22 +0545 Subject: [PATCH 2/2] Remove deleted widget from entires export --- apps/export/tasks/tasks_entries.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/export/tasks/tasks_entries.py b/apps/export/tasks/tasks_entries.py index 60888c44ed..f17aa59fa0 100644 --- a/apps/export/tasks/tasks_entries.py +++ b/apps/export/tasks/tasks_entries.py @@ -4,7 +4,7 @@ from deep.permissions import ProjectPermissions as PP from deep.filter_set import get_dummy_request -from analysis_framework.models import Exportable +from analysis_framework.models import Exportable, Widget from entry.models import Entry from export.models import Export from export.entries.excel_exporter import ExcelExporter @@ -67,9 +67,14 @@ def export_entries(export): ), ), ) + widget = Widget.objects.filter( + analysis_framework__project=project, + is_deleted=False + ).values('key') exportables = Exportable.objects.filter( analysis_framework__project=project, + widget_key__in=widget ).distinct() regions = Region.objects.filter(project=project).distinct()