From 642643f3e30909fc2f2dd0379b19d2c250a864f3 Mon Sep 17 00:00:00 2001 From: maiconkkl Date: Thu, 6 Jun 2019 15:35:50 -0300 Subject: [PATCH] =?UTF-8?q?Remo=C3=A7=C3=A3o=20das=20fun=C3=A7=C3=B5es=20x?= =?UTF-8?q?ml=5Fdownload=5Fnfe=20e=20download=5Fnfe=20por=20serem=20iguais?= =?UTF-8?q?=20as=20xml=5Fconsulta=5Fdistribuicao=5Fnfe=20e=20consulta=5Fdi?= =?UTF-8?q?stribuicao=5Fnfe;=20Altera=C3=A7=C3=A3o=20na=20ordena=C3=A7?= =?UTF-8?q?=C3=A3o=20das=20fun=C3=A7=C3=B5es=20para=20seguir=20o=20padr?= =?UTF-8?q?=C3=A3o=20inserido=20por=20padr=C3=A3o;=20Foi=20alterado=20o=20?= =?UTF-8?q?arquivo=20de=20validar=20esquema=20para=20trabalhar=20com=20cla?= =?UTF-8?q?sses=20para=20facilitar=20a=20utiliza=C3=A7=C3=A3o=20e=20o=20re?= =?UTF-8?q?aproveitamento=20de=20codigo;=20Foi=20alterado=20a=20fun=C3=A7?= =?UTF-8?q?=C3=B5es=20de=20consulta=5Fdistribuicao=5Fnfe=20e=20autorizar?= =?UTF-8?q?=5Fnfe=20para=20validar=20o=20esquema=20do=20xml=20antes=20de?= =?UTF-8?q?=20enviar=20e=20caso=20tenha=20algum=20erro,=20interromper=20o?= =?UTF-8?q?=20envio=20para=20evitar=20bloqueio=20conforme=20Nota=20T=C3=A9?= =?UTF-8?q?cnica=202018.002.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pytrustnfe/nfe/__init__.py | 36 +++++++++++++++++++----------------- pytrustnfe/xml/validate.py | 45 ++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/pytrustnfe/nfe/__init__.py b/pytrustnfe/nfe/__init__.py index 8395f24..d1684b6 100644 --- a/pytrustnfe/nfe/__init__.py +++ b/pytrustnfe/nfe/__init__.py @@ -9,6 +9,7 @@ from lxml import etree from .patch import has_patch from .assinatura import Assinatura from pytrustnfe.xml import render_xml, sanitize_response +from pytrustnfe.xml.validate import ValidarXml from pytrustnfe.utils import gerar_chave, ChaveNFe from pytrustnfe.Servidores import localizar_url from pytrustnfe.certificado import extract_cert_and_key_from_pfx, save_cert_key @@ -20,6 +21,9 @@ from requests import Session from zeep import Client from zeep.transports import Transport +# Xml schema validation class +validar_xml = ValidarXml() + def _generate_nfe_id(**kwargs): for item in kwargs['NFes']: @@ -125,7 +129,11 @@ def xml_autorizar_nfe(certificado, **kwargs): def autorizar_nfe(certificado, **kwargs): # Assinar if "xml" not in kwargs: kwargs['xml'] = xml_autorizar_nfe(certificado, **kwargs) - return _send(certificado, 'NfeAutorizacao', **kwargs) + erros_esquemas = validar_xml.valida_nfe(kwargs['xml']) + if erros_esquemas is not False: + return erros_esquemas + else: + return _send(certificado, 'NfeAutorizacao', **kwargs) def xml_retorno_autorizar_nfe(certificado, **kwargs): @@ -214,6 +222,16 @@ def xml_consulta_distribuicao_nfe(certificado, **kwargs): # Assinar return _render(certificado, 'NFeDistribuicaoDFe', False, **kwargs) +def consulta_distribuicao_nfe(certificado, **kwargs): + if "xml" not in kwargs: + kwargs['xml'] = xml_consulta_distribuicao_nfe(certificado, **kwargs) + erros_esquemas = validar_xml.valida_distribuicao(kwargs['xml']) + if erros_esquemas is not False: + return erros_esquemas + else: + return _send_v310(certificado, **kwargs) + + def _send_v310(certificado, **kwargs): xml_send = kwargs["xml"] base_url = localizar_url( @@ -243,19 +261,3 @@ def _send_v310(certificado, **kwargs): 'received_xml': response, 'object': obj.Body.nfeDistDFeInteresseResponse.nfeDistDFeInteresseResult } - - -def consulta_distribuicao_nfe(certificado, **kwargs): - if "xml" not in kwargs: - kwargs['xml'] = xml_consulta_distribuicao_nfe(certificado, **kwargs) - return _send_v310(certificado, **kwargs) - - -def xml_download_nfe(certificado, **kwargs): # Assinar - return _render(certificado, 'NFeDistribuicaoDFe', False, **kwargs) - - -def download_nfe(certificado, **kwargs): - if "xml" not in kwargs: - kwargs['xml'] = xml_download_nfe(certificado, **kwargs) - return _send_v310(certificado, **kwargs) diff --git a/pytrustnfe/xml/validate.py b/pytrustnfe/xml/validate.py index 4564f1a..6ff45b3 100644 --- a/pytrustnfe/xml/validate.py +++ b/pytrustnfe/xml/validate.py @@ -6,23 +6,30 @@ import os from lxml import etree -PATH = os.path.dirname(os.path.abspath(__file__)) - -def valida_nfe(xml_nfe): - xsd = 'schemas/enviNFe_v4.00.xsd' - return valida_esquema(xml_nfe, xsd) - - -def valida_distribuicao(xml): - xsd = 'distDFeInt_v1.01.xsd' - return valida_esquema(xml, xsd) - - -def valida_esquema(xml, xsd_name): - xsd = os.path.join(PATH, xsd_name) - xml_etree = etree.fromstring(xml) - esquema = etree.XMLSchema(etree.parse(xsd)) - esquema.validate(xml_etree) - erros = [x.message for x in esquema.error_log] - return "\n".join(erros) +class ValidarXml: + PATH = os.path.dirname(os.path.abspath(__file__)) + + def valida_nfe(self, xml): + xsd = 'enviNFe_v4.00.xsd' + erros = self.valida_esquema(xml, xsd) + if len(erros) > 0: + return {'ErrosEsquemas': "\n".join(erros)} + else: + return False + + def valida_distribuicao(self, xml): + xsd = 'distDFeInt_v1.01.xsd' + erros = self.valida_esquema(xml, xsd) + if len(erros) >0: + return {'ErrosEsquemas': "\n".join(erros)} + else: + return False + + def valida_esquema(self, xml, xsd_name): + xsd = os.path.join(self.PATH, 'schemas/', xsd_name) + xml_etree = etree.fromstring(xml) + esquema = etree.XMLSchema(etree.parse(xsd)) + esquema.validate(xml_etree) + erros = [x.message for x in esquema.error_log] + return erros