Skip to content

Commit

Permalink
added handling household flexfields and int field handling fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
johniak committed Feb 27, 2025
1 parent 6eaebc6 commit 9aac9e0
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 deletions.
10 changes: 10 additions & 0 deletions src/hct_mis_api/apps/universal_update_script/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class UniversalUpdateAdminForm(forms.ModelForm):
widget=ArrayFieldFilteredSelectMultiple("Individual Flex Fields", is_stacked=False),
required=False,
)

household_flex_fields_fields = SimpleArrayField(
base_field=forms.CharField(max_length=255), # type: ignore
widget=ArrayFieldFilteredSelectMultiple("Household Flex Fields", is_stacked=False),
required=False,
)
household_fields = SimpleArrayField(
base_field=forms.CharField(max_length=255), # type: ignore
widget=ArrayFieldFilteredSelectMultiple("Household Fields", is_stacked=False),
Expand All @@ -73,6 +79,9 @@ def __init__(self, *args: list[Any], **kwargs: dict[Any, Any]) -> None:
self.fields["individual_flex_fields_fields"].widget.choices = list(
self.get_dynamic_individual_flex_fields_choices()
)
self.fields["household_flex_fields_fields"].widget.choices = list(
self.get_dynamic_individual_flex_fields_choices()
)
self.fields["household_fields"].widget.choices = list(self.get_dynamic_household_fields_choices())
self.fields["document_types"].queryset = self.get_dynamic_document_types_queryset()
self.fields["delivery_mechanisms"].queryset = self.get_dynamic_delivery_mechanisms_queryset()
Expand Down Expand Up @@ -133,6 +142,7 @@ class UniversalUpdateAdmin(HOPEModelAdminBase):
"fields": (
"individual_fields",
"individual_flex_fields_fields",
"household_flex_fields_fields",
"household_fields",
"document_types",
"delivery_mechanisms",
Expand Down
1 change: 1 addition & 0 deletions src/hct_mis_api/apps/universal_update_script/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class UniversalUpdate(
individual_fields = ArrayField(base_field=models.CharField(max_length=255), default=list)
individual_flex_fields_fields = ArrayField(base_field=models.CharField(max_length=255), default=list)
household_fields = ArrayField(base_field=models.CharField(max_length=255), default=list)
household_flex_fields_fields = ArrayField(base_field=models.CharField(max_length=255), default=list)
document_types = models.ManyToManyField(DocumentType, blank=True)
delivery_mechanisms = models.ManyToManyField(DeliveryMechanism, blank=True)
template_file = models.FileField(blank=True, null=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from hct_mis_api.apps.universal_update_script.universal_individual_update_service.validator_and_handlers import (
handle_admin_field,
handle_boolean_field,
handle_integer_field,
handle_simple_field,
validate_admin,
validate_boolean,
Expand Down Expand Up @@ -72,58 +73,66 @@
"admin2": ("admin2", validate_string, handle_admin_field),
"admin3": ("admin3", validate_string, handle_admin_field),
"admin4": ("admin4", validate_string, handle_admin_field),
"size": ("size", validate_integer, handle_simple_field),
"female_age_group_0_5_count": ("female_age_group_0_5_count", validate_integer, handle_simple_field),
"female_age_group_6_11_count": ("female_age_group_6_11_count", validate_integer, handle_simple_field),
"female_age_group_12_17_count": ("female_age_group_12_17_count", validate_integer, handle_simple_field),
"female_age_group_18_59_count": ("female_age_group_18_59_count", validate_integer, handle_simple_field),
"female_age_group_60_count": ("female_age_group_60_count", validate_integer, handle_simple_field),
"pregnant_count": ("pregnant_count", validate_integer, handle_simple_field),
"male_age_group_0_5_count": ("male_age_group_0_5_count", validate_integer, handle_simple_field),
"male_age_group_6_11_count": ("male_age_group_6_11_count", validate_integer, handle_simple_field),
"male_age_group_12_17_count": ("male_age_group_12_17_count", validate_integer, handle_simple_field),
"male_age_group_18_59_count": ("male_age_group_18_59_count", validate_integer, handle_simple_field),
"male_age_group_60_count": ("male_age_group_60_count", validate_integer, handle_simple_field),
"size": ("size", validate_integer, handle_integer_field),
"female_age_group_0_5_count": ("female_age_group_0_5_count", validate_integer, handle_integer_field),
"female_age_group_6_11_count": ("female_age_group_6_11_count", validate_integer, handle_integer_field),
"female_age_group_12_17_count": ("female_age_group_12_17_count", validate_integer, handle_integer_field),
"female_age_group_18_59_count": ("female_age_group_18_59_count", validate_integer, handle_integer_field),
"female_age_group_60_count": ("female_age_group_60_count", validate_integer, handle_integer_field),
"pregnant_count": ("pregnant_count", validate_integer, handle_integer_field),
"male_age_group_0_5_count": ("male_age_group_0_5_count", validate_integer, handle_integer_field),
"male_age_group_6_11_count": ("male_age_group_6_11_count", validate_integer, handle_integer_field),
"male_age_group_12_17_count": ("male_age_group_12_17_count", validate_integer, handle_integer_field),
"male_age_group_18_59_count": ("male_age_group_18_59_count", validate_integer, handle_integer_field),
"male_age_group_60_count": ("male_age_group_60_count", validate_integer, handle_integer_field),
"female_age_group_0_5_disabled_count": (
"female_age_group_0_5_disabled_count",
validate_integer,
handle_simple_field,
handle_integer_field,
),
"female_age_group_6_11_disabled_count": (
"female_age_group_6_11_disabled_count",
validate_integer,
handle_simple_field,
handle_integer_field,
),
"female_age_group_12_17_disabled_count": (
"female_age_group_12_17_disabled_count",
validate_integer,
handle_simple_field,
handle_integer_field,
),
"female_age_group_18_59_disabled_count": (
"female_age_group_18_59_disabled_count",
validate_integer,
handle_simple_field,
handle_integer_field,
),
"female_age_group_60_disabled_count": (
"female_age_group_60_disabled_count",
validate_integer,
handle_integer_field,
),
"male_age_group_0_5_disabled_count": ("male_age_group_0_5_disabled_count", validate_integer, handle_integer_field),
"male_age_group_6_11_disabled_count": (
"male_age_group_6_11_disabled_count",
validate_integer,
handle_integer_field,
),
"female_age_group_60_disabled_count": ("female_age_group_60_disabled_count", validate_integer, handle_simple_field),
"male_age_group_0_5_disabled_count": ("male_age_group_0_5_disabled_count", validate_integer, handle_simple_field),
"male_age_group_6_11_disabled_count": ("male_age_group_6_11_disabled_count", validate_integer, handle_simple_field),
"male_age_group_12_17_disabled_count": (
"male_age_group_12_17_disabled_count",
validate_integer,
handle_simple_field,
handle_integer_field,
),
"male_age_group_18_59_disabled_count": (
"male_age_group_18_59_disabled_count",
validate_integer,
handle_simple_field,
handle_integer_field,
),
"male_age_group_60_disabled_count": ("male_age_group_60_disabled_count", validate_integer, handle_simple_field),
"children_count": ("children_count", validate_integer, handle_simple_field),
"male_children_count": ("male_children_count", validate_integer, handle_simple_field),
"female_children_count": ("female_children_count", validate_integer, handle_simple_field),
"children_disabled_count": ("children_disabled_count", validate_integer, handle_simple_field),
"male_children_disabled_count": ("male_children_disabled_count", validate_integer, handle_simple_field),
"female_children_disabled_count": ("female_children_disabled_count", validate_integer, handle_simple_field),
"male_age_group_60_disabled_count": ("male_age_group_60_disabled_count", validate_integer, handle_integer_field),
"children_count": ("children_count", validate_integer, handle_integer_field),
"male_children_count": ("male_children_count", validate_integer, handle_integer_field),
"female_children_count": ("female_children_count", validate_integer, handle_integer_field),
"children_disabled_count": ("children_disabled_count", validate_integer, handle_integer_field),
"male_children_disabled_count": ("male_children_disabled_count", validate_integer, handle_integer_field),
"female_children_disabled_count": ("female_children_disabled_count", validate_integer, handle_integer_field),
"returnee": ("returnee", validate_boolean, handle_boolean_field),
"first_registration_date": ("first_registration_date", validate_date, handle_simple_field),
"last_registration_date": ("last_registration_date", validate_date, handle_simple_field),
Expand All @@ -149,6 +158,19 @@ def get_individual_flex_fields() -> dict[Any, Any]:
return flex_fields_dict


def get_household_flex_fields() -> dict[Any, Any]:
flex_fields_dict = dict()
for flexible_attribute in FlexibleAttribute.objects.filter(
associated_with=FlexibleAttribute.ASSOCIATED_WITH_HOUSEHOLD
):
flex_fields_dict[f"flex_ind__{flexible_attribute.name}"] = (
flexible_attribute.name,
validate_flex_field_string,
handle_simple_field,
)
return flex_fields_dict


def get_document_fields() -> list[Any]:
return [(f"{x}_no_i_c", f"{x}_country_i_c") for x in DocumentType.objects.values_list("key", flat=True)]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from hct_mis_api.apps.universal_update_script.models import UniversalUpdate
from hct_mis_api.apps.universal_update_script.universal_individual_update_service.all_updatable_fields import (
get_document_fields,
get_household_flex_fields,
get_individual_flex_fields,
get_wallet_fields,
household_fields,
Expand Down Expand Up @@ -65,6 +66,11 @@ def __init__(
for column_name, data in get_individual_flex_fields().items()
if data[0] in universal_update.individual_flex_fields_fields
}
self.household_flex_fields = {
column_name: data
for column_name, data in get_household_flex_fields().items()
if data[0] in universal_update.household_flex_fields_fields
}
self.document_fields = []
for document_no_column_name, _ in get_document_fields():
key = self.document_types[document_no_column_name].key
Expand Down Expand Up @@ -130,6 +136,19 @@ def validate_individual_flex_fields(
errors.append(f"Row: {row_index} - {error}")
return errors

def validate_household_flex_fields(
self, row: Tuple[Any, ...], headers: List[str], individual: Individual, row_index: int
) -> List[str]:
errors = []
if self.household_flex_fields is None:
return []
for field, (name, validator, _handler) in self.household_flex_fields.items():
value = row[headers.index(field)]
error = validator(value, name, row, self.business_area, self.program)
if error: # pragma: no cover
errors.append(f"Row: {row_index} - {error}")
return errors

def validate_documents(
self, row: Tuple[Any, ...], headers: List[str], individual: Individual, row_index: int
) -> List[str]:
Expand Down Expand Up @@ -180,6 +199,7 @@ def validate(self, sheet: Worksheet, headers: List[str]) -> List[str]:
errors.extend(self.validate_household_fields(row, headers, household, row_index))
errors.extend(self.validate_individual_fields(row, headers, individual, row_index))
errors.extend(self.validate_individual_flex_fields(row, headers, individual, row_index))
errors.extend(self.validate_household_flex_fields(row, headers, individual, row_index))
errors.extend(self.validate_documents(row, headers, individual, row_index))
return errors

Expand Down Expand Up @@ -213,6 +233,16 @@ def handle_individual_flex_update(self, row: Tuple[Any, ...], headers: List[str]
continue
individual.flex_fields[name] = handled_value

def handle_household_flex_update(self, row: Tuple[Any, ...], headers: List[str], household: Household) -> None:
if self.household_flex_fields is None:
return
for field, (name, _validator, handler) in self.household_flex_fields.items():
value = row[headers.index(field)]
handled_value = handler(value, field, household, self.business_area, self.program)
if self.ignore_empty_values and (handled_value is None or handled_value == ""): # pragma: no cover
continue
household.flex_fields[name] = handled_value

def handle_documents_update(
self, row: Tuple[Any, ...], headers: List[str], individual: Individual
) -> Tuple[list, list]:
Expand Down Expand Up @@ -317,6 +347,7 @@ def handle_update(self, sheet: Worksheet, headers: List[str]) -> List[str]:
self.handle_household_update(row, headers, household)
self.handle_individual_update(row, headers, individual)
self.handle_individual_flex_update(row, headers, individual)
self.handle_household_flex_update(row, headers, household)
documents_to_update_part, documents_to_create_part = self.handle_documents_update(row, headers, individual)
documents_to_update.extend(documents_to_update_part)
documents_to_create.extend(documents_to_create_part)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def handle_boolean_field(value: Any, name: str, household: Any, business_area: B
return value == "TRUE"


def handle_integer_field(
value: Any, name: str, household: Any, business_area: BusinessArea, program: Program
) -> Optional[int]:
if value is None or value == "":
return None
return int(value)


def handle_admin_field(
value: Any, name: str, household: Any, business_area: BusinessArea, program: Program
) -> Optional[Area]:
Expand Down Expand Up @@ -66,7 +74,7 @@ def validate_date(


def validate_integer(
value: Any, name: str, model_class: Any, business_area: BusinessArea, program: Program
value: Any, name: str, modified_object: Any, business_area: BusinessArea, program: Program
) -> Optional[str]:
if value is None or value == "":
return None
Expand Down

0 comments on commit 9aac9e0

Please sign in to comment.