Debian Codegraph @

addons/point_of_sale/models/pos_printer.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from base64 import b32encode
4 from hashlib import sha256
5 from odoo import api, fields, models, _
6 from odoo.exceptions import ValidationError
7
8
9 def format_epson_certified_domain(serial_number):
10 """Epson printers can be configured to use a wildcard certificate,
11 for a domain name derived from the printer serial number.
12
13 :param serial_number: The printer serial number or an IP address.
14 :return: The corresponding domain name, or the original IP address.
15 """
16 if "." in serial_number:
17 # If the field is provided an epson serial number, convert it to a domain name
18 # Note: serial numbers should not contain dots, as IPs or URLs would.
19 return serial_number
20
21 epson_domain = "omnilinkcert.epson.biz"
22
23 sha256_hash = sha256(serial_number.encode()).digest()
24 base32_text = b32encode(sha256_hash).decode().rstrip("=")
25 return f"{base32_text.lower()}.{epson_domain}"
26
27
28 class PosPrinter(models.Model):
29 _name = 'pos.printer'
30
31 _description = 'Point of Sale Printer'
32 _inherit = ['pos.load.mixin']
33
34 name = fields.Char('Printer Name', required=True, default='Printer', help='An internal identification of the printer')
35 printer_type = fields.Selection(
36 string='Printer Type',
37 default='iot',
38 selection=[
39 ('iot', 'Use a printer connected to the IoT Box'),
40 ('epson_epos', 'Use an Epson printer'),
41 ]
42 )
43 proxy_ip = fields.Char('Proxy IP Address', help="The IP Address or hostname of the Printer's hardware proxy")
44 product_categories_ids = fields.Many2many('pos.category', 'printer_category_rel', 'printer_id', 'category_id', string='Printed Product Categories')
45 company_id = fields.Many2one('res.company', string='Company', required=True, default=lambda self: self.env.company)
46 pos_config_ids = fields.Many2many('pos.config', 'pos_config_printer_rel', 'printer_id', 'config_id')
47 epson_printer_ip = fields.Char(
48 string='Epson Printer IP Address',
49 help=(
50 "Local IP address of an Epson receipt printer, or its serial number if the "
51 "'Automatic Certificate Update' option is enabled in the printer settings."
52 ),
53 default="0.0.0.0"
54 )
55
56 @api.model
57 def _load_pos_data_domain(self, data, config):
58 return [('id', 'in', config.printer_ids.ids)]
59
60 @api.model
61 def _load_pos_data_fields(self, config):
62 return ['id', 'name', 'proxy_ip', 'product_categories_ids', 'printer_type', 'epson_printer_ip']
63
64 @api.constrains('epson_printer_ip')
65 def _constrains_epson_printer_ip(self):
66 for record in self:
67 if record.printer_type == 'epson_epos' and not record.epson_printer_ip:
68 raise ValidationError(_("Epson Printer IP Address cannot be empty."))
69
70 @api.onchange("epson_printer_ip")
71 def _onchange_epson_printer_ip(self):
72 for rec in self:
73 if rec.epson_printer_ip:
74 rec.epson_printer_ip = format_epson_certified_domain(rec.epson_printer_ip)