Skip to content

Commit

Permalink
Merge pull request #2170 from IFRCGo/feature/country-plan-import
Browse files Browse the repository at this point in the history
Change country_plan sector
  • Loading branch information
szabozoltan69 authored Jun 21, 2024
2 parents 6e79a94 + a18d2a7 commit 00c10fe
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 4.2.13 on 2024-06-10 08:31

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("country_plan", "0006_alter_countryplan_created_by_and_more"),
]

operations = [
migrations.AlterField(
model_name="membershipcoordination",
name="sector",
field=models.CharField(
choices=[
("climate", "Climate"),
("crisis", "Crisis"),
("health", "Health"),
("migration", "Migration"),
("inclusion", "Inclusion"),
("enabling_functions", "Enabling functions"),
],
max_length=100,
verbose_name="Sector",
),
),
migrations.AlterField(
model_name="strategicpriority",
name="type",
field=models.CharField(
choices=[
("ongoing_emergency_operations", "Ongoing emergency operations"),
("climate_and_environment", "Climate and environment"),
("disasters_and_crisis", "Disasters and crisis"),
("health_and_wellbeing", "Health and wellbeing"),
("migration_and_displacement", "Migration and displacement"),
("value_power_and_inclusion", "Values, power and inclusion"),
],
max_length=100,
verbose_name="Type",
),
),
]
14 changes: 6 additions & 8 deletions country_plan/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ def full_country_plan_mc(self):
class StrategicPriority(models.Model):
class Type(models.TextChoices):
ONGOING_EMERGENCY_OPERATIONS = "ongoing_emergency_operations", _("Ongoing emergency operations")
CLIMATE_AND_ENVIRONMENTAL_CRISIS = "climate_and_environmental_crisis", _("Climate and environmental crisis")
EVOLVING_CRISIS_AND_DISASTERS = "evolving_crisis_and_disasters", _("Evolving crisis and disasters")
GROWING_GAPS_IN_HEALTH_AND_WELLBEING = "growing_gaps_in_health_and_wellbeing", _("Growing gaps in health and wellbeing")
MIGRATION_AND_IDENTITY = "migration_and_identity", _("Migration and Identity")
VALUE_POWER_AND_INCLUSION = "value_power_and_inclusion", _("Value power and inclusion")
CLIMATE_AND_ENVIRONMENT = "climate_and_environment", _("Climate and environment")
EVOLVING_CRISIS_AND_DISASTERS = "disasters_and_crisis", _("Disasters and crisis")
HEALTH_AND_WELLBEING = "health_and_wellbeing", _("Health and wellbeing")
MIGRATION_AND_DISPLACEMENT = "migration_and_displacement", _("Migration and displacement")
VALUE_POWER_AND_INCLUSION = "value_power_and_inclusion", _("Values, power and inclusion")

country_plan = models.ForeignKey(
CountryPlan,
Expand All @@ -138,9 +138,7 @@ class Sector(models.TextChoices):
HEALTH = "health", _("Health")
MIGRATION = "migration", _("Migration")
INCLUSION = "inclusion", _("Inclusion")
ENGAGED = "engaged", _("Engaged")
ACCOUNTABLE = "accountable", _("Accountable")
TRUSTED = "trusted", _("Trusted")
ENABLING_FUNCTIONS = "enabling_functions", _("Enabling functions")

country_plan = models.ForeignKey(
CountryPlan,
Expand Down
60 changes: 31 additions & 29 deletions country_plan/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,28 @@ class CountryPlanImporter:
CSV_COLUMN = [
"ISO3",
"Country",
"Ongoing emergency operations",
"SP1 - Climate and environmental crisis",
"SP2 - Evolving crises and disasters",
"SP3 - Growing gaps in health and wellbeing",
"SP4 - Migration and identity",
"SP5 - Values, power, and inclusion",
"Max of people to be reached ",
"SP1",
"SP2",
"SP3",
"SP4",
"SP5",
"EA1",
"EA2",
"EA3",
"TOTAL\nFUNDING REQUIREMENTS",
"Reach\n2024_EO",
"Reach\n2024_SP1",
"Reach\n2024_SP2",
"Reach\n2024_SP3",
"Reach\n2024_SP4",
"Reach\n2024_SP5",
"SP1 ",
"SP2 ",
"SP3 ",
"SP4 ",
"SP5 ",
"EF",
"Total FR\n2024",
]

SP_COLUMN = [
"SP1",
"SP2",
"SP3",
"SP4",
"SP5",
"EA1",
"EA2",
"EA3",
"SP1 ",
"SP2 ",
"SP3 ",
"SP4 ",
"SP5 ",
"EF",
]

@staticmethod
Expand All @@ -56,7 +51,7 @@ def _process_number(number):
# Some specific cases using regex
value_search = re.search(r"(?P<value>(\d+(?:\.\d+)?))?\s?(?P<expression>[MKB])", number)
if value_search is None or (value_search.group("value") is None or value_search.group("expression") is None):
return pd.to_numeric(number)
return pd.to_numeric(number.replace(",", ""))
value = pd.to_numeric(value_search.group("value"))
expression = value_search.group("expression")
EXPRESSION_MULTIPLIER = {
Expand All @@ -71,8 +66,14 @@ def _save_country_plan(cls, row):
country = Country.objects.filter(record_type=CountryType.COUNTRY).get(iso3=row.ISO3)
# -- Country Plan
country_plan, _ = CountryPlan.objects.get_or_create(country=country)
country_plan.requested_amount = cls._process_number(row._18)
country_plan.people_targeted = cls._process_number(row._9)
people_targeted_list = [row._3, row._4, row._5, row._6, row._7, row._8]
people_new_list = []
for people in people_targeted_list:
people = cls._process_number(people)
people_new_list.append(people)
if len(people_new_list):
country_plan.people_targeted = max([people for people in people_new_list if people is not None], default=None)
country_plan.requested_amount = cls._process_number(row._15)
country_plan.save()

# -- StrategicPriority
Expand All @@ -88,7 +89,8 @@ def _save_country_plan(cls, row):
strategic_priority.save()

# -- MembershipCoordination
n_society_list = [row.SP1, row.SP2, row.SP3, row.SP4, row.SP5, row.EA1, row.EA2, row.EA3]
# NOTE: Replace whitespace in xlsx file
n_society_list = [row._9, row._10, row._11, row._12, row._13, row.EF]
sp_column_n_society_map = dict(zip(cls.SP_COLUMN, n_society_list))
sp_column_ns_type_map = dict(zip(cls.SP_COLUMN, MembershipCoordination.Sector))
membership_coordination_ids = []
Expand Down Expand Up @@ -123,7 +125,7 @@ def _save_country_plan(cls, row):

@classmethod
def process(cls, file):
data = pd.read_excel(file, skiprows=1, na_filter=False, usecols=cls.CSV_COLUMN)
data = pd.read_excel(file, skiprows=4, na_filter=False, usecols=cls.CSV_COLUMN)
data = data[:-1]

errors = []
Expand Down

0 comments on commit 00c10fe

Please sign in to comment.