Browse Source

erro de unicode no metodo .tostring()

pull/1/head
Junior Tada 11 years ago
parent
commit
c5f72aeb8e
  1. 3
      pynfe/processamento/assinatura.py
  2. 20
      pynfe/processamento/comunicacao.py
  3. 14
      pynfe/processamento/serializacao.py

3
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

20
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('&lt;', '<').replace('&gt;', '>').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"""

14
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
@ -143,7 +143,7 @@ class SerializacaoXML(Serializacao):
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

Loading…
Cancel
Save