From 892906c46fd93a7eb251dd154acd048b0fc9e89d Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Wed, 10 Feb 2021 22:47:00 -0600 Subject: [PATCH] Import clients --- source/app/controllers/utils.py | 1 - source/app/models/main.py | 33 ++++++++++++++++++--------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/source/app/controllers/utils.py b/source/app/controllers/utils.py index 761effc..e8f3996 100644 --- a/source/app/controllers/utils.py +++ b/source/app/controllers/utils.py @@ -775,7 +775,6 @@ def to_slug(string): def read_csv(path, args={'delimiter': '|'}): with open(path) as f: reader = csv.DictReader(f, **args) - # ~ rows = tuple(csv.reader(f, **args)) rows = [r for r in reader] return rows diff --git a/source/app/models/main.py b/source/app/models/main.py index ec89a05..ef82520 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -10524,25 +10524,28 @@ def _import_clients(rfc, path): log.info('Importando clientes...') data = utils.read_csv(path) + t = len(data) for i, row in enumerate(data): - if i == 0: + msg = f'\tImportando cliente {i+1} de {t}' + log.info(msg) + row['rfc'] = row['rfc'].upper() + row['nombre'] = utils.spaces(row['nombre']) + row['slug'] = utils.to_slug(row['nombre']) + + w = ((Socios.rfc==row['rfc']) & (Socios.slug==row['slug'])) + if Socios.select().where(w).exists(): + msg = 'Ya existe el RFC y Razón Social' + log.info(msg) continue - print(row) - # ~ w = ((Socios.rfc==fields['rfc']) & (Socios.slug==fields['slug'])) - # ~ if Socios.select().where(w).exists(): - # ~ msg = 'Ya existe el RFC y Razón Social' - # ~ data = {'ok': False, 'row': {}, 'new': True, 'msg': msg} - # ~ return data + row['es_cliente'] = True + row['forma_pago'] = SATFormaPago.get(SATFormaPago.id==row['forma_pago']) - # ~ try: - # ~ obj = Socios.create(**fields) - # ~ except IntegrityError as e: - # ~ msg = 'Ocurrio un error, al dar de alta el emisor' - # ~ data = {'ok': False, 'row': {}, 'new': True, 'msg': msg} - # ~ return data - - break + try: + obj = Socios.create(**row) + except Exception as e: + log.error(e) + break desconectar() log.info('Proceso terminado correctamente...')