-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add management command to patch ahhs data.
- Loading branch information
1 parent
a54e564
commit 47c8391
Showing
1 changed file
with
73 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |