From a0c8b2e336698d0c007bc3f633afe8fe9cd0b734 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Wed, 13 Dec 2017 01:13:48 -0600 Subject: [PATCH] Copia local en carpeta compartida --- source/app/controllers/util.py | 52 ++++++++++++++++++++++++++++++++-- source/app/models/main.py | 6 ++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/source/app/controllers/util.py b/source/app/controllers/util.py index 2bb3f65..6b70c91 100644 --- a/source/app/controllers/util.py +++ b/source/app/controllers/util.py @@ -19,6 +19,7 @@ import uuid import zipfile from io import BytesIO +from pathlib import Path from smtplib import SMTPException, SMTPAuthenticationError from xml.etree import ElementTree as ET @@ -35,7 +36,7 @@ from dateutil import parser from .helper import CaseInsensitiveDict, NumLet, SendMail, TemplateInvoice, \ SeaFileAPI -from settings import DEBUG, log, template_lookup, COMPANIES, DB_SAT, \ +from settings import DEBUG, MV, log, template_lookup, COMPANIES, DB_SAT, \ PATH_XSLT, PATH_XSLTPROC, PATH_OPENSSL, PATH_TEMPLATES, PATH_MEDIA, PRE, \ PATH_XMLSEC, TEMPLATE_CANCEL, DEFAULT_SAT_PRODUCTO, DECIMALES @@ -1459,16 +1460,63 @@ def backup_dbs(): return +def _validar_directorios(path_bk, target): + path = Path(_join(path_bk, target)) + path.mkdir(parents=True, exist_ok=True) + return str(path) + + +def local_copy(files): + if not MV: + return + + path_bk = _join(str(Path.home()), 'facturas') + if not os.path.isdir(path_bk): + msg = 'No existe la carpeta: facturas' + log.error(msg) + return + + args = 'df -P ~/facturas | tail -1 | cut -d' ' -f 1' + try: + result = _call(args) + if result != 'empresalibre': + log.info(result) + msg = 'Asegurate de que exista la carpeta para sincronizar' + log.error(msg) + return + except subprocess.CalledProcessError: + msg = 'No se pudo obtener la ruta para sincronizar' + log.error(msg) + return + + try: + for obj, name, target in files: + path = _validar_directorios(path_bk, target) + path_file = _join(path, name) + m = 'wb' + if name.endswith('xml'): + m = 'w' + save_file(path_file, obj, m) + except Exception as e: + log.error(e) + + return + + def sync_cfdi(auth, files): + local_copy(files) + if DEBUG: return + if not auth['REPO']: + return + seafile = SeaFileAPI(SEAFILE_SERVER['URL'], auth['USER'], auth['PASS']) if seafile.is_connect: for f in files: seafile.update_file( f, auth['REPO'], 'Facturas/{}/'.format(f[2]), auth['PASS']) - return diff --git a/source/app/models/main.py b/source/app/models/main.py index 312a24c..9ccccf9 100644 --- a/source/app/models/main.py +++ b/source/app/models/main.py @@ -2163,17 +2163,15 @@ class Facturas(BaseModel): @classmethod def sync(cls, id, auth): - if not auth['REPO']: - return - obj = Facturas.get(Facturas.id==id) if obj.uuid is None: msg = 'La factura no esta timbrada' return + emisor = Emisor.select()[0] pdf, name_pdf = cls.get_pdf(id, auth['RFC']) name_xml = '{}{}_{}.xml'.format(obj.serie, obj.folio, obj.cliente.rfc) - target = str(obj.fecha)[:7].replace('-', '/') + target = emisor.rfc + '/' + str(obj.fecha)[:7].replace('-', '/') files = ( (obj.xml, name_xml, target), (pdf, name_pdf, target),