From 47c83911a288621072dd6d9b3e25444d9a3ccc0e Mon Sep 17 00:00:00 2001 From: rup-narayan-rajbanshi Date: Mon, 3 Jun 2024 17:47:59 +0545 Subject: [PATCH] Add management command to patch ahhs data. --- .../management/commands/fix_ahhs_data.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 apps/entry/management/commands/fix_ahhs_data.py diff --git a/apps/entry/management/commands/fix_ahhs_data.py b/apps/entry/management/commands/fix_ahhs_data.py new file mode 100644 index 000000000..f2000629b --- /dev/null +++ b/apps/entry/management/commands/fix_ahhs_data.py @@ -0,0 +1,73 @@ +import csv +import os +import logging +from decimal import Decimal + +from django.core.management.base import BaseCommand +from django.db import transaction + +from apps.entry.models import Figure +from utils.common import round_half_up + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + + help = "Fix AHHS household size data." + + def add_arguments(self, parser): + parser.add_argument('csv_file_path', type=str, help="Path to the CSV file containing the data.") + + # FIXME Use Bulk Manager + def patch_figure_household_from_csv(self, file_path): + change_in_repoted_value_figures = [] + change_in_unit_figures = [] + figure_id_not_found = [] + + with open(file_path, 'r') as file: + reader = csv.DictReader(file) + for row in reader: + fig_id = row.get['figure_id'] + figure = Figure.objects.get(id=fig_id) + if not figure: + figure_id_not_found.append(fig_id) + print("No figure found for ids:", fig_id) + continue + + if figure.unit != Figure.UNIT.HOUSEHOLD: + change_in_unit_figures.append(figure.id) + print("Figure unit is not Household") + continue + + if figure.reported != row.get['figure_reported_value']: + change_in_repoted_value_figures.append(figure.id) + print("Change in reported value") + continue + + if figure.household_size == row.get['figure_household_size']: + change_in_repoted_value_figures.append(figure.id) + print("No change in household size") + continue + + figure.household_size = row.get('figure_household_size') + figure.total_figures = round_half_up(row.get['reported'] * Decimal(str(row.get('figure_household_size')))) + figure.save() + + print("Figure ids not found:", figure_id_not_found) + print("Figure unit not household:", change_in_unit_figures) + print("Change in reported value figure ids:", change_in_repoted_value_figures) + + @transaction.atomic() + def handle(self, *args, **kwargs): + csv_file_path = kwargs['csv_file_path'] + if not os.path.exists(csv_file_path): + logger.error(f"CSV file path does not exist: {csv_file_path}") + return + try: + self.patch_figure_household_from_csv(csv_file_path) + logger.info("2024 AHHS data fixed successfully.") + except FileNotFoundError: + logger.error(f"CSV file at {csv_file_path} not found.") + except Exception: + logger.error("Failed to fix 2024 AHHS data:", exc_info=True)