From c5f72aeb8eb8af7b97a3b3e3b0922fe9e6f82605 Mon Sep 17 00:00:00 2001 From: Junior Tada Date: Thu, 16 Jul 2015 14:23:01 -0300 Subject: [PATCH] erro de unicode no metodo .tostring() --- pynfe/processamento/assinatura.py | 3 ++- pynfe/processamento/comunicacao.py | 20 +++++++++++++------- pynfe/processamento/serializacao.py | 16 ++++++++-------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/pynfe/processamento/assinatura.py b/pynfe/processamento/assinatura.py index 0165448..d878eac 100644 --- a/pynfe/processamento/assinatura.py +++ b/pynfe/processamento/assinatura.py @@ -32,6 +32,7 @@ class AssinaturaA1(Assinatura): #root = etree.parse(xml).getroot() # caminho root = etree.fromstring(xml) # string + #root = etree.XML(xml) # string signer = signxml.xmldsig(root, digest_algorithm="sha1") signer.sign(method=signxml.methods.enveloped, key=chave, cert=cert, algorithm="rsa-sha1", c14n_algorithm='http://www.w3.org/TR/2001/REC-xml-c14n-20010315', @@ -40,5 +41,5 @@ class AssinaturaA1(Assinatura): #root = etree.SubElement(signer.data, "{http://www.w3.org/2000/09/xmldsig#}Reference", # URI='#NFe41150715389524000122651010000000271333611649') - result = etree.tostring(signer.data) + result = etree.tostring(signer.data, encoding="unicode") return result diff --git a/pynfe/processamento/comunicacao.py b/pynfe/processamento/comunicacao.py index 31e55c0..0f15ede 100644 --- a/pynfe/processamento/comunicacao.py +++ b/pynfe/processamento/comunicacao.py @@ -37,10 +37,16 @@ class ComunicacaoSefaz(Comunicacao): etree.SubElement(raiz, 'idLote').text = str(1) # numero autoincremental gerado pelo sistema etree.SubElement(raiz, 'indSinc').text = str(1) # 0 para assincrono, 1 para sincrono etree.SubElement(raiz, 'NFe').text = nota_fiscal # conjunto de nfe tramistidas (max 50) - dados = etree.tostring(raiz, encoding="UTF-8") + import ipdb + #ipdb.set_trace() + print (type(nota_fiscal)) + dados = etree.tostring(raiz, encoding="unicode") + print (dados) + print (type(dados)) + #print (dados) # Monta XML para envio da requisição xml = self._construir_xml_status_pr(cabecalho=self._cabecalho_soap(), dados=dados, url=url) - return str(xml, 'utf-8').replace('<', '<').replace('>', '>').replace('\'', '"').replace('\n', '') + return xml #return self._post(url, xml, self._post_header()) def cancelar(self, nota_fiscal): @@ -61,7 +67,7 @@ class ComunicacaoSefaz(Comunicacao): etree.SubElement(raiz, 'tpAmb').text = str(self._ambiente) etree.SubElement(raiz, 'cUF').text = CODIGOS_ESTADOS[self.uf.upper()] etree.SubElement(raiz, 'xServ').text = 'STATUS' - dados = etree.tostring(raiz, encoding="UTF-8") + dados = etree.tostring(raiz, encoding="UTF-8").decode('utf-8') # Monta XML para envio da requisição if self.uf.upper() == 'PR': xml = self._construir_xml_status_pr(cabecalho=self._cabecalho_soap(), dados=dados, url=url) @@ -152,7 +158,7 @@ class ComunicacaoSefaz(Comunicacao): etree.SubElement(raiz, 'cUF').text = str(41) etree.SubElement(raiz, 'versaoDados').text = VERSAO_PADRAO - return etree.tostring(raiz, encoding="UTF-8") + return etree.tostring(raiz, encoding="unicode") def _construir_xml_soap(self, cabecalho, metodo, tag_metodo, dados): """Mota o XML para o envio via SOAP""" @@ -167,7 +173,7 @@ class ComunicacaoSefaz(Comunicacao): etree.SubElement(met, 'nfeCabecMsg').text = cabecalho etree.SubElement(met, 'nfeDadosMsg').text = dados - return etree.tostring(raiz, encoding="UTF-8", xml_declaration=True) + return etree.tostring(raiz, encoding="unicode", xml_declaration=True) def _construir_xml_status_pr(self, cabecalho, dados, url): u"""Mota o XML para o envio via SOAP""" @@ -175,9 +181,9 @@ class ComunicacaoSefaz(Comunicacao): raiz = etree.Element('{%s}Envelope'%NAMESPACE_SOAP, nsmap={'soap': NAMESPACE_SOAP}, xmlns=url) etree.SubElement(raiz, '{%s}Header'%NAMESPACE_SOAP).text = cabecalho body = etree.SubElement(raiz, '{%s}Body'%NAMESPACE_SOAP) - etree.SubElement(body, 'nfeDadosMsg').text = str(dados) + etree.SubElement(body, 'nfeDadosMsg').text = dados - return etree.tostring(raiz, encoding="UTF-8", xml_declaration=True) + return etree.tostring(raiz, encoding="UTF-8", xml_declaration=True).decode('utf-8') def _post_header(self): u"""Retorna um dicionário com os atributos para o cabeçalho da requisição HTTP""" diff --git a/pynfe/processamento/serializacao.py b/pynfe/processamento/serializacao.py index 7228e62..7444453 100644 --- a/pynfe/processamento/serializacao.py +++ b/pynfe/processamento/serializacao.py @@ -58,7 +58,7 @@ class SerializacaoXML(Serializacao): raiz.append(self._serializar_nota_fiscal(nf, retorna_string=False)) if retorna_string: - return etree.tostring(raiz, pretty_print=True).decode('utf-8') + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz @@ -93,7 +93,7 @@ class SerializacaoXML(Serializacao): etree.SubElement(endereco, 'fone').text = emitente.endereco_telefone if retorna_string: - return etree.tostring(raiz, pretty_print=True) + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz @@ -122,7 +122,7 @@ class SerializacaoXML(Serializacao): etree.SubElement(endereco, 'fone').text = cliente.endereco_telefone if retorna_string: - return etree.tostring(raiz, pretty_print=True) + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz @@ -141,9 +141,9 @@ class SerializacaoXML(Serializacao): transportadora.endereco_municipio, transportadora.endereco_uf, ) etree.SubElement(raiz, 'UF').text = transportadora.endereco_uf - + if retorna_string: - return etree.tostring(raiz, pretty_print=True) + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz @@ -165,7 +165,7 @@ class SerializacaoXML(Serializacao): etree.SubElement(raiz, 'UF').text = entrega_retirada.endereco_uf if retorna_string: - return etree.tostring(raiz, pretty_print=True) + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz @@ -216,7 +216,7 @@ class SerializacaoXML(Serializacao): etree.SubElement(cofins_item, 'vCOFINS').text = str(produto_servico.cofins_valor) if retorna_string: - return etree.tostring(raiz, pretty_print=True) + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz @@ -368,7 +368,7 @@ class SerializacaoXML(Serializacao): etree.SubElement(info_ad, 'infCpl').text = nota_fiscal.informacoes_complementares_interesse_contribuinte if retorna_string: - return etree.tostring(raiz, pretty_print=True) + return etree.tostring(raiz, encoding="unicode", pretty_print=True) else: return raiz