From aa682255715a2b5b522bb67c5505369dc516480c Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Wed, 10 Feb 2021 22:34:34 -0600 Subject: [PATCH] Read csv to dict --- source/app/controllers/utils.py | 19 +++++++++++++++++++ source/app/models/main.py | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/source/app/controllers/utils.py b/source/app/controllers/utils.py index 79c8870..761effc 100644 --- a/source/app/controllers/utils.py +++ b/source/app/controllers/utils.py @@ -31,6 +31,7 @@ import smtplib import sqlite3 import subprocess import threading +import unicodedata import zipfile from pathlib import Path from xml.sax.saxutils import escape @@ -760,3 +761,21 @@ def get_status_sat(xml): return node.text +def spaces(value): + return '\n'.join([' '.join(l.split()) for l in value.split('\n')]) + + +def to_slug(string): + value = (unicodedata.normalize('NFKD', string) + .encode('ascii', 'ignore') + .decode('ascii').lower()) + return value.replace(' ', '_') + + +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 b4b63e7..ec89a05 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -10523,6 +10523,26 @@ def _import_clients(rfc, path): conectar(args) log.info('Importando clientes...') + data = utils.read_csv(path) + for i, row in enumerate(data): + if i == 0: + 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 + + # ~ 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 desconectar() log.info('Proceso terminado correctamente...')