Skip to content

Commit

Permalink
Fixup after moving printing logic to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmiklos committed Jul 20, 2023
1 parent 4bb5e37 commit df99c31
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 30 deletions.
34 changes: 34 additions & 0 deletions InvenTree/label/migrations/0013_add_multipage_to_buildlinelabel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.2.20 on 2023-07-13 08:31

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('label', '0012_labeloutput'),
]

operations = [
migrations.AddField(
model_name='buildlinelabel',
name='multipage',
field=models.BooleanField(default=False, verbose_name='Print multiple labels to a single page'),
),
migrations.AddField(
model_name='buildlinelabel',
name='multipage_border',
field=models.CharField(blank=True, choices=[('0.5mm solid #000', 'Solid'), ('0.5mm dotted #000', 'Dotted'), ('', 'No border')], default='0.5mm solid #000', max_length=250, verbose_name='Border style'),
),
migrations.AddField(
model_name='buildlinelabel',
name='page_height',
field=models.FloatField(default=297, help_text="The number of label rows per page will be automatically calculated from the label's height", validators=[django.core.validators.MinValueValidator(2)], verbose_name='Height of one page [mm]'),
),
migrations.AddField(
model_name='buildlinelabel',
name='page_width',
field=models.FloatField(default=210, help_text="The number of label columns per page will be automatically calculated from the label's width", validators=[django.core.validators.MinValueValidator(2)], verbose_name='Width of one page [mm]'),
),
]
36 changes: 9 additions & 27 deletions InvenTree/label/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def context(self, request):
for plugin in plugins:
# Let each plugin add its own context data
plugin.add_label_context(self, self.object_to_print, request, context)

return context

def context_multipage(self, request, content):
Expand Down Expand Up @@ -290,17 +291,8 @@ def render(self, request, **kwargs):
**kwargs
)

def render_multipage_as_string(self, request, multipage_table, **kwargs):
"""Render the multipage tables to a HTML string with the multipage template.
Useful for debug mode (viewing generated code)
"""
return render_to_string(
"label/multipage_label_base.html",
self.context_multipage(request, multipage_table), request)

def render_multipage(self, request, multipage_table, **kwargs):
"""Render the multipage tables to a HTML string with the multipage template.
def render_paginated(self, request, multipage_table, **kwargs):
"""Render the multipage tables to an HTML string with the multipage template.
Uses django-weasyprint plugin to render HTML template
"""
Expand All @@ -318,24 +310,14 @@ def render_multipage(self, request, multipage_table, **kwargs):
)
return ret

def render_from_html(self, request, **kwargs):
"""Render a pre-rendered HTML data to a PDF file.
def render_paginated_to_string(self, request, multipage_table, **kwargs):
"""Render the multipage tables to a HTML string with the multipage template.
Uses django-weasyprint plugin to render HTML template
Useful for debug mode (viewing generated code)
"""
wp = WeasyprintLabelMixin(
request,
self.template_name,
base_url=request.build_absolute_uri("/"),
presentational_hints=True,
filename=self.generate_filename(request),
**kwargs
)

return wp.render_to_response(
self.context(request),
**kwargs
)
return render_to_string(
"label/multipage_label_base.html",
self.context_multipage(request, multipage_table), request)


class LabelOutput(models.Model):
Expand Down
53 changes: 50 additions & 3 deletions InvenTree/plugin/builtin/labels/inventree_label.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Default label printing plugin (supports PDF generation)"""

import math

from django.core.files.base import ContentFile
from django.http import JsonResponse
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -45,11 +47,14 @@ def print_labels(self, label: LabelTemplate, items: list, request, **kwargs):
outputs = []
output_file = None

for item in items:
if label.multipage:
outputs = self.print_paginated(label, request, items, debug)
else:
for item in items:

label.object_to_print = item
label.object_to_print = item

outputs.append(self.print_label(label, request, debug=debug, **kwargs))
outputs.append(self.print_label(label, request, debug=debug, **kwargs))

if self.get_setting('DEBUG'):
html = '\n'.join(outputs)
Expand Down Expand Up @@ -94,3 +99,45 @@ def print_label(self, label: LabelTemplate, request, **kwargs):
return self.render_to_html(label, request, **kwargs)
else:
return self.render_to_pdf(label, request, **kwargs)

def print_paginated(self, label: LabelTemplate, request, items, debug):
"""Paginate the labels to pages depending on th label/page geometry ratio"""

col_count = math.floor(label.page_width / label.width)
row_count = math.floor(label.page_height / label.height)

main_tables = '<table class="main-table"><tr class="main-table-row">'

col_counter = 0
row_counter = 0
item_counter = 0

outputs = []

for item in items:
label.object_to_print = item

main_tables += '<td class="main-table-cell">' + self.print_label(label, request, debug=True) + '</td>'

col_counter = col_counter + 1
if col_counter >= col_count:
col_counter = 0
main_tables += "</tr>"

row_counter = row_counter + 1
if row_counter >= row_count:
row_counter = 0
main_tables += "</table>"
if item_counter < len(items) - 1:
main_tables += '<table class="main-table"><tr class="main-table-row">'
else:
if item_counter < len(items) - 1:
main_tables += '<tr class="main-table-row">'
item_counter = item_counter + 1

if debug:
outputs.append(label.render_paginated_to_string(request, main_tables))
else:
outputs.append(label.render_paginated(request, main_tables))

return outputs

0 comments on commit df99c31

Please sign in to comment.