Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean-up StrategicPriority & MembershipCoordination on import #2182

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions country_plan/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import operator
import re
import typing
from functools import reduce

import pandas as pd
Expand All @@ -14,6 +15,10 @@
logger = logging.getLogger(__name__)


class RollBackException(Exception):
pass


class CountryPlanImporter:
CSV_COLUMN = [
"ISO3",
Expand Down Expand Up @@ -124,15 +129,11 @@ def _save_country_plan(cls, row):
)

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

def _process(cls, data) -> typing.List[typing.Dict]:
errors = []
for row in data.itertuples():
try:
with transaction.atomic():
cls._save_country_plan(row)
cls._save_country_plan(row)
except Exception as e:
logger.error(f"Failed to import Country-Plan: iso3:{row.ISO3}", exc_info=True)
errors.append(
Expand All @@ -143,6 +144,32 @@ def process(cls, file):
)
return errors

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

try:
with transaction.atomic():
# Clean-up existing dataset StrategicPriority,MembershipCoordination
StrategicPriority.objects.all().delete()
MembershipCoordination.objects.all().delete()

# Add new data
errors = cls._process(data)
if errors:
raise RollBackException()
except Exception as e:
# handle custom rollback error manually
if not isinstance(e, RollBackException):
logger.error("Failed to process Country-Plan", exc_info=True)
errors.append(dict(error=str(e)))
pass

# Return errors
return errors


@shared_task
def process_data_import(pk):
Expand Down