Advertisement
Guest User

Untitled

a guest
Oct 19th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. # coding=utf-8
  2. import socket
  3.  
  4. from trytond.config import config as config_
  5. from trytond.pool import Pool
  6. from trytond.report import Report
  7. from trytond.report.report import TranslateFactory
  8. from trytond.transaction import Transaction
  9.  
  10. from jinja2 import Environment
  11.  
  12. # Determines the port of the Zebra Printer
  13. ZEBRA_PORT = config_.getint('zebra', 'port', default=9100)
  14.  
  15. # Determines the host_name of the Zebra Printer
  16. ZEBRA_HOST = config_.get('zebra', 'host_name', default='imp-label-1.saluc.com')
  17.  
  18.  
  19. class ZebraReport(Report):
  20.  
  21.     def print_xml(xml_document, timeout=10):
  22.         """
  23.        Send XML formatted text to a network label printer
  24.        :param xml_document: Document object, fully build for label.
  25.        :param timeout: Socket timeout for printer connection, default 10.
  26.        """
  27.  
  28.         printer_hostname = Transaction().context.get('printer_hostname')
  29.         if not printer_hostname:
  30.             printer_hostname = ZEBRA_HOST
  31.  
  32.         with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  33.             s.settimeout(timeout)
  34.             s.connect((printer_hostname, ZEBRA_PORT))
  35.             s.send(bytes(xml_document, 'utf-8'))
  36.  
  37.     @classmethod
  38.     def render(cls, report, report_context):
  39.         pool = Pool()
  40.         Translation = pool.get('ir.translation')
  41.         Company = pool.get('company.company')
  42.  
  43.         report_content = (report.report_content if report.report_content
  44.                           else False)
  45.  
  46.         if not report_content:
  47.             raise Exception('Error', 'Missing report file!')
  48.  
  49.         # Make the report itself available in the report context
  50.         report_context['report'] = report
  51.  
  52.         translate = TranslateFactory(cls.__name__, Translation)
  53.         report_context['setLang'] = lambda language: translate.set_language(
  54.             language)
  55.  
  56.         company_id = Transaction().context.get('company')
  57.         report_context['company'] = Company(company_id)
  58.  
  59.         return cls.render_template(report_content, report_context, translate)
  60.  
  61.     @classmethod
  62.     def execute(cls, ids, data):
  63.         _, content, _, _ = super().execute(ids, data)
  64.         cls.print_xml(content)
  65.         return None
  66.  
  67.     @classmethod
  68.     def get_environment(cls):
  69.         """
  70.        Create and return a jinja environment to render templates
  71.        Downstream modules can override this method to easily make changes
  72.        to environment
  73.        """
  74.         env = Environment(
  75.             autoescape=True,
  76.             trim_blocks=False,
  77.             extensions=['jinja2.ext.loopcontrols'],
  78.         )
  79.         # env.filters.update(cls.get_jinja_filters())
  80.         return env
  81.  
  82.     @classmethod
  83.     def render_template(cls, template, localcontext, translator):
  84.         """
  85.        Render the template using Jinja2
  86.        """
  87.         env = cls.get_environment()
  88.  
  89.         report_template = env.from_string(template.decode('utf-8)'))
  90.         return report_template.render(**localcontext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement