Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] make compatible with nfelib-xsdata with originalCase #52

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/erpbrasil/edoc/edoc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018 - TODAY Luis Felipe Mileo - KMEE INFORMATICA LTDA
# License MIT

Expand All @@ -11,6 +10,9 @@
from lxml import etree
from lxml.etree import _Element

from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

from .resposta import analisar_retorno_raw

# Fix Python 2.x.
Expand All @@ -32,6 +34,7 @@ class DocumentoEletronico(ABC):
Classe abstrata responsavel por definir os metodos e logica das classes
de transmissao com os webservices.
"""
_namespace = False
_consulta_servico_ao_enviar = False
_consulta_documento_antes_de_enviar = False

Expand Down Expand Up @@ -69,6 +72,26 @@ def _generateds_to_string_etree(self, ds, pretty_print=False):
output.close()
return contents, etree.fromstring(contents)

def render_edoc(self, edoc, pretty_print=False):
"""
Same as _generateds_to_string_etree but for xsdata bindings.
"""
if isinstance(edoc, _Element):
return etree.tostring(edoc), edoc
if isinstance(edoc, str):
return edoc, etree.fromstring(edoc)
serializer = XmlSerializer(config=SerializerConfig(pretty_print=pretty_print))

if self._namespace:
ns_map = {None: self._namespace}
else:
ns_map = None

xml_string = serializer.render(
obj=edoc, ns_map=ns_map
)
return xml_string, etree.fromstring(xml_string.encode())

def _post(self, raiz, url, operacao, classe):
xml_string, xml_etree = self._generateds_to_string_etree(raiz)
with self._transmissao.cliente(url):
Expand All @@ -79,6 +102,16 @@ def _post(self, raiz, url, operacao, classe):
operacao, raiz, xml_string, retorno, classe
)

def _post_xsdata(self, raiz, url, operacao, classe):
xml_string, xml_etree = self.render_edoc(raiz)
with self._transmissao.cliente(url):
retorno = self._transmissao.enviar(
operacao, xml_etree
)
return analisar_retorno_raw_xsdata(
operacao, raiz, xml_string, retorno, classe
)

def processar_documento(self, edoc):
""" Processar documento executa o envio do documento fiscal de forma
completa ao serviço relacionado, esta é um método padrão que
Expand Down Expand Up @@ -224,6 +257,13 @@ def assina_raiz(self, raiz, id, getchildren=False):
return xml_assinado
return xml_assinado.replace('\n', '').replace('\r', '')

def assina_raiz_xsdata(self, raiz, id, getchildren=False):
xml_etree = self.render_edoc(raiz)[1]
xml_assinado = Assinatura(self._transmissao.certificado).assina_xml2(
xml_etree, id, getchildren
)
return xml_assinado

def _verifica_servico_em_operacao(self, proc_servico):
return True

Expand Down
1 change: 0 additions & 1 deletion src/erpbrasil/edoc/nfe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding=utf-8
# Copyright (C) 2019 Luis Felipe Mileo - KMEE

from __future__ import division
Expand Down
Loading