def handle(self, *args, **options): path = options.get("csv_path") import_type = options.get("type") year = options.get("year") try: validate_year_for_archiving(year) except ArchiveYearError as ex: raise CommandError(f"Failure import {import_type}: {str(ex)}") file_name = self.path_to_upload(path, 'csv') # Windows-1252 or CP-1252, used because of a back quote csv_file = open(file_name, newline="", encoding="cp1252") try: success, msg = IMPORT_ARCHIVED_TYPE[import_type](csv_file, year) except WrongChartOFAccountCodeException as ex: csv_file.close() raise CommandError(f"Failure import {import_type}: {str(ex)}") csv_file.close() if self.upload_s3: os.remove(file_name) if success: self.stdout.write( self.style.SUCCESS( f"Successfully completed import {import_type}.")) else: raise CommandError(f"Failure import {import_type}: {msg}.")
def import_archived_analysis2(csvfile, year): msgerror = "Failure importing archived Analysis 2. Error:" try: validate_year_for_archiving(year) except ArchiveYearError as ex: raise ArchiveYearError(f"{msgerror} {str(ex)}") success, msg = import_obj(csvfile, ANALYSIS2_HISTORICAL_KEY, year=year) if not success: raise WrongChartOFAccountCodeException(f"{msgerror} {msg}") return success, msg
def import_archived_programme(csvfile, year): msgerror = "Failure importing archived Programme. Error:" try: validate_year_for_archiving(year) except ArchiveYearError as ex: raise ArchiveYearError(f"{msgerror} {str(ex)}") success, msg = import_obj(csvfile, PROGRAMME_HISTORICAL_KEY, year=year) if not success: raise WrongChartOFAccountCodeException(f"{msgerror} {msg}") return success, msg
def import_archived_cost_centre(csvfile, year): try: validate_year_for_archiving(year) except ArchiveYearError as ex: raise ArchiveYearError( f"Failure import Importing archived Cost Centre hierarchy error: {str(ex)}" ) success, msg = import_obj(csvfile, COST_CENTRE_HISTORICAL_KEY, year=year) if not success: raise WrongChartOFAccountCodeException( f"Importing archived Cost Centre hierarchy error: " f"{msg}") return success, msg
def import_archived_nac(csvfile, year): msgerror = "Failure importing archived NAC. Error:" try: validate_year_for_archiving(year) except ArchiveYearError as ex: raise ArchiveYearError(f"{msgerror} {str(ex)}") success, msg = import_obj(csvfile, NAC_HISTORICAL_KEY, year=year) if not success: raise WrongChartOFAccountCodeException(f"{msgerror} {msg}") else: # The archived NAC is not normalised, and each expenditure NAC row # contains the corresponding budget NAC. # This information is missing from the upload file, so we need to extract it # from the expenditure category: each expenditure category row is linked to # a budget NAC. # The classification of budget and expenditure NAC is internal to DIT: # budgets should be set only using budget NAC, to reduce the number of lines nac_qs = ArchivedNaturalCode.objects.filter(financial_year=year) for nac_obj in nac_qs: if nac_obj.expenditure_category: account_L6_budget_val = nac_obj.expenditure_category.linked_budget_code nac_obj.account_L6_budget = account_L6_budget_val nac_obj.save() category_qs = ArchivedExpenditureCategory.objects.filter(financial_year=year) for category_obj in category_qs: account_L6_budget_val = category_obj.linked_budget_code try: nac_obj = ArchivedNaturalCode.objects.get( financial_year=year, natural_account_code=account_L6_budget_val ) nac_obj.used_for_budget = True nac_obj.save() except ObjectDoesNotExist: msg = f"NAC {account_L6_budget_val} not defined for year {year}" raise WrongChartOFAccountCodeException(f"{msgerror} {msg}") return success, msg