Merge branch 'develop'

Envio de correo automático
This commit is contained in:
Mauricio Baeza 2017-11-06 22:21:36 -06:00
commit 62e0320a55
2 changed files with 49 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import sqlite3
import socket
import subprocess
import tempfile
import threading
import time
import unicodedata
import uuid
@ -1163,6 +1164,29 @@ def cancel_cfdi(uuid, pk12, rfc, auth):
return data, result
#~ def run_in_thread(fn, *args, **kwargs):
#~ t = threading.Thread(target=fn, args=args, kwargs=kwargs)
#~ t.daemon = True
#~ t.start()
#~ return
def run_in_thread(fn):
def run(*k, **kw):
t = threading.Thread(target=fn, args=k, kwargs=kw)
t.start()
return t
return run
def get_bool(value):
if not value:
return False
if value == '1':
return True
return False
class ImportFacturaLibre(object):
def __init__(self, path):

View File

@ -76,6 +76,15 @@ class Configuracion(BaseModel):
@classmethod
def get_(cls, keys):
if isinstance(keys, str):
data = (Configuracion
.select(Configuracion.valor)
.where(Configuracion.clave == keys)
)
if data:
return data[0].valor
return ''
if keys['fields'] == 'correo':
fields = ('correo_servidor', 'correo_puerto', 'correo_ssl',
'correo_usuario', 'correo_contra', 'correo_copia',
@ -268,7 +277,12 @@ class Emisor(BaseModel):
def get_auth(cls):
try:
obj = Emisor.select()[0]
return {'USER': obj.correo_timbrado, 'PASS': obj.token_timbrado}
data = {
'RFC': obj.rfc,
'USER': obj.correo_timbrado,
'PASS': obj.token_timbrado,
}
return data
except:
return {}
@ -1248,6 +1262,10 @@ class Facturas(BaseModel):
return file_zip, name_zip
@util.run_in_thread
def _send(self, id, rfc):
return Facturas.send(id, rfc)
@classmethod
def send(cls, id, rfc):
values = Configuracion.get_({'fields': 'correo'})
@ -1600,9 +1618,9 @@ class Facturas(BaseModel):
obj.estatus = 'Generada'
obj.save()
enviar_correo = util.get_bool(Configuracion.get_('correo_directo'))
auth = Emisor.get_auth()
#~ error = False
msg = 'Factura timbrada correctamente'
result = util.timbra_xml(obj.xml, auth)
if result['ok']:
@ -1611,15 +1629,17 @@ class Facturas(BaseModel):
obj.fecha_timbrado = result['fecha']
obj.estatus = 'Timbrada'
obj.error = ''
#~ obj.save()
obj.save()
row = {'uuid': obj.uuid, 'estatus': 'Timbrada'}
if enviar_correo:
cls._send(cls, id, auth['RFC'])
else:
#~ error = True
msg = result['error']
obj.estatus = 'Error'
obj.error = msg
obj.save()
row = {'estatus': 'Error'}
obj.save()
return {'ok': result['ok'], 'msg': msg, 'row': row}