Skip to content

Commit

Permalink
add icon and title to barcode
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Oct 14, 2021
1 parent 0c1e32d commit 4d842fe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Lagerregal/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Django settings for Lagerregal project.
import os
from reportlab.lib.units import mm

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
Expand Down Expand Up @@ -255,6 +256,10 @@
("linux", "Linux")
]

LABEL_PAGESIZE = (83*mm, 25*mm)
LABEL_ICON = "pdf_forms/minerva.jpg"
LABEL_TITLE = "Information Services & Technology"

HANDOVER_PROTOCOL_LOCATION = "pdf_forms/Leihschein.pdf"
HANDOVER_PROTOCOL_TEXT_LOCATIONS = {
"user_name": [155, 632],
Expand Down
18 changes: 14 additions & 4 deletions devices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from reportlab.lib.units import mm
from reportlab.pdfgen import canvas
from rest_framework.renderers import JSONRenderer
from pdfrw.toreportlab import makerl

from reversion import revisions as reversion
from reversion.models import Version
Expand Down Expand Up @@ -1590,11 +1589,22 @@ def generate_label_pdf(request, pk):
# Create the PDF object, using the buffer as its "file."
p = canvas.Canvas(buffer, pagesize=size)

barcode = code128.Code128('{:06d}'.format(pk), barHeight=size[1]/2, barWidth=1)
offset = 0
if hasattr(settings, "LABEL_ICON"):
icon_size = size[1] * 0.9
offset = size[1] * 0.05
p.drawInlineImage(settings.LABEL_ICON, offset, offset, icon_size, icon_size)
size = (size[0] - icon_size, size[1])
offset += icon_size + offset

barcode = code128.Code128('{:06d}'.format(pk), barHeight=size[1]/2, barWidth=2)
barcode._calculate()
width, height = barcode._width, barcode._height
barcode.drawOn(p, (size[0] - width) / 2, (size[1] - height) / 2)
p.drawCentredString(size[0] / 2 + 1, (size[1] - height) / 2 - 10, '{:06d}'.format(pk))
barcode.drawOn(p, offset + (size[0] - width) / 2, (size[1] - height) / 2)
p.drawCentredString(offset + (size[0] / 2) + 1, (size[1] - height) / 2 - 11, '{:06d}'.format(pk))
if hasattr(settings, "LABEL_TITLE"):
p.setFontSize(9)
p.drawCentredString(offset + (size[0] / 2) + 1, (size[1] - height) / 2 + height + 6, settings.LABEL_TITLE)

# Close the PDF object cleanly, and we're done.
p.showPage()
Expand Down
30 changes: 30 additions & 0 deletions devices/views.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from devices import views
import pytest

class Test_Search_Parse_boolean:

@pytest.fixture()
def search(self):
return views.Search()


def test_parse_boolean_1(self, search):
search.parse_boolean(false)


class Test_Search_Parse_boolean:

@pytest.fixture()
def search(self):
return views.Search()


def test_parse_boolean_1(self, search):
search.parse_boolean(false)

def test_parse_boolean_2(self, search):
search.parse_boolean("no")

def test_parse_boolean_3(self, search):
search.parse_boolean("")

0 comments on commit 4d842fe

Please sign in to comment.