Browse Source

Add barcode to comprovante de protocolo

pull/202/head
Edward Ribeiro 9 years ago
parent
commit
76614428dd
  1. 9
      protocoloadm/views.py
  2. 11
      sapl/utils.py
  3. 6
      templates/protocoloadm/comprovante.html

9
protocoloadm/views.py

@ -1,3 +1,5 @@
from sapl.utils import create_barcode_128_as_base64_png
from datetime import date, datetime from datetime import date, datetime
from re import sub from re import sub
@ -321,7 +323,12 @@ class ComprovanteProtocoloView(TemplateView):
numero = self.kwargs['pk'] numero = self.kwargs['pk']
ano = self.kwargs['ano'] ano = self.kwargs['ano']
protocolo = Protocolo.objects.get(ano=ano, numero=numero) protocolo = Protocolo.objects.get(ano=ano, numero=numero)
return self.render_to_response({"protocolo": protocolo})
# numero is string, padd with zeros left via .zfill()
base64_data = create_barcode_128_as_base64_png(numero.zfill(6))
barcode = 'data:image/png;base64,{0}'.format(base64_data)
return self.render_to_response({"protocolo": protocolo, "barcode": barcode})
class ProtocoloMateriaView(FormMixin, GenericView): class ProtocoloMateriaView(FormMixin, GenericView):

11
sapl/utils.py

@ -32,6 +32,17 @@ def xstr(s):
return '' if s is None else str(s) return '' if s is None else str(s)
def create_barcode_128_as_base64_png(value):
from base64 import b64encode
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm, inch
from reportlab.graphics.barcode import createBarcodeDrawing
barcode = createBarcodeDrawing('Code128', value = value, barWidth = 170, height=50, fontSize = 2, humanReadable = True)
data = b64encode(barcode.asString('png'))
return data.decode('utf-8')
def make_choices(*choice_pairs): def make_choices(*choice_pairs):
assert len(choice_pairs) % 2 == 0 assert len(choice_pairs) % 2 == 0
ipairs = iter(choice_pairs) ipairs = iter(choice_pairs)

6
templates/protocoloadm/comprovante.html

@ -36,7 +36,11 @@
<tr> <td colspan="2" align="center"><b>COMPROVANTE DE PROTOCOLO</b></td> </tr> <tr> <td colspan="2" align="center"><b>COMPROVANTE DE PROTOCOLO</b></td> </tr>
<tr> <tr>
<td>Código de Barra</td> <td><center>
<img src="{{barcode}}" height="50px" width="170px">
<br>
{{ protocolo.numero|stringformat:'06d' }}
</center></td>
<td align="center">Autenticação ??</td> <td align="center">Autenticação ??</td>
</tr> </tr>

Loading…
Cancel
Save