Skip to content

Commit

Permalink
Used buildin rendering of template class
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeoLacruz committed Dec 13, 2024
1 parent fe878a6 commit 473098e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
23 changes: 7 additions & 16 deletions inventree_zebra/zebra_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
Supports direct printing of labels on label printers
"""
import os
# System stuff
import socket
from datetime import datetime

# translation
# Django stuff
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.validators import MaxValueValidator
from django.conf import settings
from django.shortcuts import render

# InvenTree plugin libs
from plugin import InvenTreePlugin
from plugin.mixins import LabelPrintingMixin, SettingsMixin

# Zebra printer support
import zpl
import socket

from inventree_zebra.version import ZEBRA_PLUGIN_VERSION
from .version import ZEBRA_PLUGIN_VERSION


class ZebraLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
Expand All @@ -30,6 +29,7 @@ class ZebraLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
VERSION = ZEBRA_PLUGIN_VERSION
NAME = "Zebra"
SLUG = "zebra"
PUBLISH_DATE = datetime.today().strftime('%Y-%m-%d')
TITLE = "Zebra Label Printer"

SETTINGS = {
Expand Down Expand Up @@ -115,16 +115,7 @@ def print_label(self, **kwargs):

# From here we need to distinguish between html templates and ZPL templates
if zpl_template:
# Find the template file and load its content
template_file = os.path.join(settings.MEDIA_ROOT, 'report/label')
template_file = os.path.join(template_file, str(kwargs['context']['template']))

# Render the template
fields = {'object': kwargs['item_instance'], 'context': kwargs['context']}
try:
raw_zpl = str(render(None, template_file, fields).content, 'utf-8').replace('\n', '')
except Exception:
raise Exception('Error rendering ZPL template')
raw_zpl = kwargs['context']['template'].render_as_string(kwargs['item_instance'], None).replace('\n', '')

# Create the zpl data
li = zpl.Label(height, width, dpmm)
Expand Down
14 changes: 7 additions & 7 deletions stockitem_50x30_zpl_001.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% autoescape off %}
^FT30,25^A0N,18,22^FDIPN^FS
^FT150,30^FB100,1,,C,,^A0N,24,32^FDACME^FS
^FT320,25^A0N,18,22^FD{{ object.pk }}^FS
^FT100,70^FB200,2,,C,,^A0N,18,22^FD{{ object.part.name }}^FS
^FT100,100^FB200,1,,C,,^A0N,18,22^FD{% for ma in object.part.manufacturer_parts.all %}{{ ma.manufacturer.name }}{% endfor %}^FS
^FT30,150^FB340,1,,C,,^A0N,30,40^FD{{ object.part.IPN }}^FS
^FT20,210^FB360,3,,L,,^A0N,18,22^FD{{ object.part.description }}^FS
^FT15,110^BQ,2,3^FDQA,{{ object.part.IPN }}^FS
^FT310,130^BQ,2,3^FDQA,{{ context.qr_data }}^FS
^FT320,25^A0N,18,22^FD{{ item.pk }}^FS
^FT100,70^FB200,2,,C,,^A0N,18,22^FD{{ part.name }}^FS
^FT100,100^FB200,1,,C,,^A0N,18,22^FD{{ part.manufacturer_parts.first.manufacturer.name }}^FS
^FT30,150^FB340,1,,C,,^A0N,30,40^FD{{ part.IPN }}^FS
^FT20,210^FB360,3,,L,,^A0N,18,22^FD{{ part.description }}^FS
^FT15,110^BQ,2,3^FDQA,{{ part.IPN }}^FS
^FT310,130^BQ,2,3^FDQA,{{ qr_data }}^FS
{% endautoescape %}

0 comments on commit 473098e

Please sign in to comment.