From 94ed168f697bee0f7b30c79884d035e79787303c Mon Sep 17 00:00:00 2001 From: Thierry Date: Wed, 25 Mar 2020 10:21:06 +0100 Subject: [PATCH 1/4] Ref #57 - Wrong natures for the imported accountings --- .../management/commands/import_accounting.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/budgetweb/management/commands/import_accounting.py b/budgetweb/management/commands/import_accounting.py index 43a55c5..060db90 100644 --- a/budgetweb/management/commands/import_accounting.py +++ b/budgetweb/management/commands/import_accounting.py @@ -45,9 +45,9 @@ def handle(self, *args, **options): in structure_models.Structure.active.all()} pfis = {pfi.code: pfi for pfi in structure_models.PlanFinancement.active.all()} - dan = {an.code_nature_comptable: an for an + dan = {(an.code_nature_comptable, an.is_fleche): an for an in structure_models.NatureComptableDepense.active.all()} - ran = {an.code_nature_comptable: an for an + ran = {(an.code_nature_comptable, an.is_fleche): an for an in structure_models.NatureComptableRecette.active.all()} domains = {d.code: d for d in structure_models.DomaineFonctionnel.active.all()} @@ -66,10 +66,11 @@ def handle(self, *args, **options): (year, structure, pfi, accounting_type, enveloppe, nature, domain, ae, cp, d_dc, ar, re, r_dc, commentary) = row + pfi = pfis[pfi] if accounting_type.lower().startswith('d'): # Dépense model = models.Depense - nature = self.get_object(dan, nature, 'nature') + nature = self.get_object(dan, (nature, pfi.is_fleche), 'nature') functional_domain = self.get_object( domains, domain, 'domaine fonctionnel') @@ -83,7 +84,7 @@ def handle(self, *args, **options): else: # Recette model = models.Recette - nature = self.get_object(ran, nature, 'nature') + nature = self.get_object(ran, (nature, pfi.is_fleche), 'nature') amounts = { 'montant_dc': to_decimal(r_dc), @@ -94,7 +95,7 @@ def handle(self, *args, **options): } accountings.append(model( - pfi=pfis[pfi], structure=structures[structure], + pfi=pfi, structure=structures[structure], commentaire=commentary or None, periodebudget=period, annee=year, **amounts)) @@ -114,8 +115,8 @@ def handle(self, *args, **options): else: transaction.savepoint_commit(sid) - def get_object(self, dict, key, name): + def get_object(self, dct, key, name): try: - return dict[key] + return dct[key] except KeyError: self.row_errors.append(f'{name.title()} "{key}" does not exist') From 1a2da8de144eddc6aa1b0840fdd39d4db9ddd3de Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 25 May 2020 08:39:20 +0200 Subject: [PATCH 2/4] Ref #57 - Better errors handling --- .../management/commands/import_accounting.py | 96 ++++++++++--------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/budgetweb/management/commands/import_accounting.py b/budgetweb/management/commands/import_accounting.py index 060db90..eecbcd0 100644 --- a/budgetweb/management/commands/import_accounting.py +++ b/budgetweb/management/commands/import_accounting.py @@ -12,6 +12,10 @@ def to_decimal(amount): return Decimal((amount.replace(' ', '').replace(',', '.')) or 0) +class RowError(Exception): + pass + + class Command(BaseCommand): help = 'Import the accounting' @@ -22,7 +26,7 @@ def handle(self, *args, **options): accountings = [] period = models.PeriodeBudget.active.first() - errors = [] + self.errors = [] # Structure du fichier : @@ -58,51 +62,53 @@ def handle(self, *args, **options): with open(filename, encoding='iso-8859-1') as h: reader = csv.reader(h, delimiter=';', quotechar='"') for index, row in enumerate(reader): - self.row_errors = [] if index == 0: # Ignore header continue - (year, structure, pfi, accounting_type, enveloppe, nature, - domain, ae, cp, d_dc, ar, re, r_dc, commentary) = row - - pfi = pfis[pfi] - if accounting_type.lower().startswith('d'): - # Dépense - model = models.Depense - nature = self.get_object(dan, (nature, pfi.is_fleche), 'nature') - functional_domain = self.get_object( - domains, domain, 'domaine fonctionnel') - - amounts = { - 'montant_dc': to_decimal(d_dc), - 'montant_cp': to_decimal(cp), - 'montant_ae': to_decimal(ae), - 'naturecomptabledepense': nature, - 'domainefonctionnel': functional_domain, - } - else: - # Recette - model = models.Recette - nature = self.get_object(ran, (nature, pfi.is_fleche), 'nature') - - amounts = { - 'montant_dc': to_decimal(r_dc), - 'montant_re': to_decimal(re), - 'montant_ar': to_decimal(ar), - 'naturecomptablerecette': nature, - 'domainefonctionnel': domain, - } - - accountings.append(model( - pfi=pfi, structure=structures[structure], - commentaire=commentary or None, - periodebudget=period, annee=year, **amounts)) - - errors.extend(self.row_errors) - - if errors: - print('ERRORS :\n\n{}'.format('\n'.join(errors))) + try: + (year, structure, pfi, accounting_type, enveloppe, nature, + domain, ae, cp, d_dc, ar, re, r_dc, commentary) = row + + pfi = pfis[pfi] + if accounting_type.lower().startswith('d'): + # Dépense + model = models.Depense + nature = self.get_object(dan, (nature, pfi.is_fleche), 'nature') + functional_domain = self.get_object( + domains, domain, 'domaine fonctionnel', msg=f'- nature : {nature}') + + amounts = { + 'montant_dc': to_decimal(d_dc), + 'montant_cp': to_decimal(cp), + 'montant_ae': to_decimal(ae), + 'naturecomptabledepense': nature, + 'domainefonctionnel': functional_domain, + } + else: + # Recette + model = models.Recette + nature = self.get_object(ran, (nature, pfi.is_fleche), 'nature') + + amounts = { + 'montant_dc': to_decimal(r_dc), + 'montant_re': to_decimal(re), + 'montant_ar': to_decimal(ar), + 'naturecomptablerecette': nature, + 'domainefonctionnel': domain, + } + + structure = self.get_object(structures, structure, 'structure') + accountings.append(model( + pfi=pfi, structure=structure, + commentaire=commentary or None, + periodebudget=period, annee=year, **amounts)) + + except RowError: + continue + + if self.errors: + print('ERRORS :\n\n{}'.format('\n'.join(self.errors))) else: sid = transaction.savepoint() try: @@ -115,8 +121,10 @@ def handle(self, *args, **options): else: transaction.savepoint_commit(sid) - def get_object(self, dct, key, name): + def get_object(self, dct, key, name, msg=''): try: return dct[key] except KeyError: - self.row_errors.append(f'{name.title()} "{key}" does not exist') + msg = '' or f' {msg}' + self.errors.append(f'{name.title()} "{key}" does not exist{msg}') + raise RowError From 02ae0803550207d1985d97f95396a3532b5ea12d Mon Sep 17 00:00:00 2001 From: Thierry Date: Mon, 25 May 2020 11:44:57 +0200 Subject: [PATCH 3/4] Ref #57 - Better errors handling 2 --- budgetweb/management/commands/import_accounting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/budgetweb/management/commands/import_accounting.py b/budgetweb/management/commands/import_accounting.py index eecbcd0..cfeb9f0 100644 --- a/budgetweb/management/commands/import_accounting.py +++ b/budgetweb/management/commands/import_accounting.py @@ -70,7 +70,7 @@ def handle(self, *args, **options): (year, structure, pfi, accounting_type, enveloppe, nature, domain, ae, cp, d_dc, ar, re, r_dc, commentary) = row - pfi = pfis[pfi] + pfi = self.get_object(pfis, pfi, 'PFI') if accounting_type.lower().startswith('d'): # Dépense model = models.Depense From d8513640d2ff32b8860e606654dee4c46b090930 Mon Sep 17 00:00:00 2001 From: Thierry Date: Thu, 28 May 2020 09:08:20 +0200 Subject: [PATCH 4/4] Version 1.2.8 --- budgetweb/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/budgetweb/__init__.py b/budgetweb/__init__.py index 8db4ea1..99597d1 100755 --- a/budgetweb/__init__.py +++ b/budgetweb/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 2, 7) +VERSION = (1, 2, 8) def get_version():