Skip to content

Commit

Permalink
Add common label style field for multiple labels per page
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmiklos committed Jul 20, 2023
1 parent df99c31 commit 4b3fc59
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion InvenTree/InvenTree/static/script/inventree/label_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function updateDropDownItemsVisibility()
{
var multipageWidgetNames = ['page_width', 'page_height',
'pagesize_preset', 'page_orientation',
'multipage_border'];
'multipage_border', 'multipage_common_style'];
multipageWidgetNames.forEach(function(widget) {
if (django.jQuery('#id_multipage').prop("checked"))
django.jQuery('.field-' + widget).show();
Expand Down
17 changes: 11 additions & 6 deletions InvenTree/label/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from django.utils.translation import gettext_lazy as _

import label.models
from .models import (LabelTemplate, PartLabel, StockItemLabel,
StockLocationLabel, BuildLineLabel)

from .models import (BuildLineLabel, LabelTemplate, PartLabel, StockItemLabel,
StockLocationLabel)


class LabelAdminForm(forms.ModelForm):
"""Custom form for the label's admin form to inject non-model fields"""
Expand All @@ -15,7 +17,7 @@ class Meta:
model = LabelTemplate
fields = ['metadata', 'name', 'description', 'label', 'enabled', 'width', 'height',
'filename_pattern', 'multipage', 'pagesize_preset', 'page_orientation',
'page_width', 'page_height', 'multipage_border']
'page_width', 'page_height', 'multipage_border', 'multipage_common_style']

pagesize_preset = forms.ChoiceField(
choices=[
Expand Down Expand Up @@ -57,11 +59,12 @@ class Meta:
model = PartLabel
fields = LabelAdminForm.Meta.fields + ["filters"]


class BuildLineLabelAdminForm(LabelAdminForm):
"""Custom form for the Build line label's admin form"""
class Meta:
"""Meta is just for adding the fields field to the right position (last field)"""
model = PartLabel
model = BuildLineLabel
fields = LabelAdminForm.Meta.fields + ["filters"]


Expand All @@ -75,6 +78,7 @@ class Media:
'script/inventree/label_admin.js',
)


class StockLabelAdmin(LabelAdmin):
"""Admin class for the Stock label models"""
form = StockItemLabelAdminForm
Expand All @@ -89,12 +93,13 @@ class PartLabelAdmin(LabelAdmin):
"""Admin class for the Stock location label models"""
form = PartLabelAdminForm

class BuildLineLabel(LabelAdmin):

class BuildLineLabelAdmin(LabelAdmin):
"""Admin class for the Build line label models"""
form = BuildLineLabelAdminForm


admin.site.register(label.models.StockItemLabel, LabelAdmin)
admin.site.register(label.models.StockLocationLabel, LabelAdmin)
admin.site.register(label.models.PartLabel, LabelAdmin)
admin.site.register(label.models.BuildLineLabel, LabelAdmin)

11 changes: 10 additions & 1 deletion InvenTree/label/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ def __str__(self):
blank=True,
)

multipage_common_style = models.TextField(
verbose_name=_('Common style for multiple labels per page'),
help_text=_("This CSS snippet will be generated once into the generated label once"
"into the head section of the main (builtin) template which providing the grid table for the individual labels."),
blank=True,
)

@property
def template_name(self):
"""Returns the file system path to the template file.
Expand Down Expand Up @@ -255,7 +262,9 @@ def context_multipage(self, request, content):
'page_height': self.page_height,
'label_width': self.width,
'label_height': self.height,
'content': content}
'multipage_common_style': self.multipage_common_style,
'content': content,
}
# Pass the context through to any registered plugins
plugins = registry.with_mixin('report')

Expand Down
2 changes: 2 additions & 0 deletions InvenTree/label/templates/label/multipage_label_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
body {
margin: 0mm !important;
}

{{ multipage_common_style }}
{% endlocalize %}
</style>
</head>
Expand Down
12 changes: 12 additions & 0 deletions docs/docs/report/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ As an example, consider a label template for a StockItem. A user may wish to def

To restrict the label accordingly, we could set the *filters* value to `part__IPN=IPN123`.

### Printing multiple pages per page

If the 'Print multiple labels to a single page' option is enabled for a specific label then the label rendering is going to be performed in a different way:
- A builtin template (InvenTree/label/templates/label/multipage_label_base.html) will provide an HTML table which serves as a grid
- The labels' contents will be rendered to the cells of this table through the uploaded template

This implies the following:
- The uploaded template file should not include the head/body sections
- The CSS included in the label's uploaded template will be generated to each cell.
This should not cause rendering issues, however it is advisable to move the common styles into the 'Common style for multiple labels per page' field.
The contents of this field will be rendered into the style tag in the head section of the grid template, so this was redundant CSS could be eliminated from the output.

## Built-In Templates

The InvenTree installation provides a number of simple *default* templates which can be used as a starting point for creating custom labels. These built-in templates can be disabled if they are not required.
Expand Down

0 comments on commit 4b3fc59

Please sign in to comment.