Skip to content

Commit

Permalink
Add preview output using labalary API
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeoLacruz committed Dec 14, 2024
1 parent d789d10 commit 14e9922
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
44 changes: 44 additions & 0 deletions inventree_zebra/request_wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import requests
import os


# ----------------------------------------------------------------------------
# Wrappers around the requests for better error handling
class Wrappers():

def post_request(self, post_data, path, headers):
proxy_con = os.getenv('PROXY_CON')
proxy_url = os.getenv('PROXY_URL')
if proxy_con and proxy_url:
proxies = {proxy_con: proxy_url}
elif self.get_setting('PROXY_CON') != '' and self.get_setting('PROXY_URL') != '':
proxies = {self.get_setting('PROXY_CON'): self.get_setting('PROXY_URL')}
else:
proxies = {}
try:
response = requests.post(path,
proxies=proxies,
data=post_data,
timeout=5,
headers=headers)
except Exception as e:
return (e.args)
return (response)

def get_request(self, path, headers):
proxy_con = os.getenv('PROXY_CON')
proxy_url = os.getenv('PROXY_URL')
if proxy_con and proxy_url:
proxies = {proxy_con: proxy_url}
elif self.get_setting('PROXY_CON') != '' and self.get_setting('PROXY_URL') != '':
proxies = {self.get_setting('PROXY_CON'): self.get_setting('PROXY_URL')}
else:
proxies = {}
try:
response = requests.get(path,
proxies=proxies,
timeout=5,
headers=headers)
except Exception as e:
return (e.args)
return (response)
20 changes: 20 additions & 0 deletions inventree_zebra/zebra_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.validators import MaxValueValidator
from django.core.files.base import ContentFile

# InvenTree plugin libs
from plugin import InvenTreePlugin
Expand All @@ -20,6 +21,7 @@
import zpl

from .version import ZEBRA_PLUGIN_VERSION
from .request_wrappers import Wrappers


class ZebraLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
Expand All @@ -31,12 +33,15 @@ class ZebraLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
SLUG = "zebra"
PUBLISH_DATE = datetime.today().strftime('%Y-%m-%d')
TITLE = "Zebra Label Printer"
preview_result = ''

# BLOCKING_PRINT = True
SETTINGS = {
'CONNECTION': {
'name': _('Printer Interface'),
'description': _('Select local or network printer'),
'choices': [('local', 'Local printer e.g. USB'),
('preview', 'ZPL preview using labelary.com API'),
('network', 'Network printer with IP address')],
'default': 'local',
},
Expand Down Expand Up @@ -154,6 +159,7 @@ def print_label(self, **kwargs):
printer = open(interface, 'w')
printer.write(li.dumpZPL())
printer.close()
self.preview_result = None
except Exception as error:
raise ConnectionError('Error connecting to local printer: ' + str(error))
elif (connection == 'network'):
Expand All @@ -164,7 +170,21 @@ def print_label(self, **kwargs):
data = li.dumpZPL()
mysocket.send(data.encode())
mysocket.close()
self.preview_result = None
except Exception as error:
raise ConnectionError('Error connecting to network printer: ' + str(error))
elif (connection == 'preview'):
width_inch = round(width / 25.4, 2)
height_inch = round(height / 25.4, 2)
url = f'http://api.labelary.com/v1/printers/{dpmm}dpmm/labels/{width_inch}x{height_inch}/0'
header = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'application/pdf'}
response = Wrappers.post_request(self, li.dumpZPL(), url, header)
if response.status_code == 200:
self.preview_result = ContentFile(response.content, 'label.pdf')
else:
self.preview_result = ContentFile(f'Labalary API Error: {response.content}', 'label.html')
else:
print('Unknown Interface')

def get_generated_file(self, **kwargs):
return self.preview_result

0 comments on commit 14e9922

Please sign in to comment.