Skip to content

Commit

Permalink
[IMP] base_report_to_printer : add output_tray
Browse files Browse the repository at this point in the history
- Enables selection of printer output tray
  • Loading branch information
ecino committed Sep 17, 2024
1 parent ab0e7d3 commit 7c00129
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 156 deletions.
2 changes: 1 addition & 1 deletion base_report_to_printer/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{
"name": "Report to printer",
"version": "17.0.1.0.1",
"version": "17.0.1.0.2",
"category": "Generic Modules/Base",
"author": "Agile Business Group & Domsense, Pegueroles SCP, NaN,"
" LasLabs, Camptocamp, Odoo Community Association (OCA),"
Expand Down
2 changes: 1 addition & 1 deletion base_report_to_printer/i18n/sv.po
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ msgstr "Guide"
#: code:addons/base_report_to_printer/wizards/print_attachment_report.py:0
#, python-format
msgid "{name} ({copies} copies)"
msgstr "{namn} ({kopior} kopior)"
msgstr "{name} ({copies} kopior)"

#~ msgid "Job"
#~ msgstr "Jobb"
Expand Down
25 changes: 18 additions & 7 deletions base_report_to_printer/models/ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ class IrActionsReport(models.Model):
printing_printer_id = fields.Many2one(
comodel_name="printing.printer", string="Default Printer"
)
printer_tray_id = fields.Many2one(
comodel_name="printing.tray",
printer_input_tray_id = fields.Many2one(
comodel_name="printing.tray.input",
string="Paper Source",
domain="[('printer_id', '=', printing_printer_id)]",
)
printer_output_tray_id = fields.Many2one(
comodel_name="printing.tray.output",
string="Output Bin",
domain="[('printer_id', '=', printing_printer_id)]",
)
printing_action_ids = fields.One2many(
comodel_name="printing.report.xml.action",
inverse_name="report_id",
Expand All @@ -37,7 +42,8 @@ class IrActionsReport(models.Model):
@api.onchange("printing_printer_id")
def onchange_printing_printer_id(self):
"""Reset the tray when the printer is changed"""
self.printer_tray_id = False
self.printer_input_tray_id = False
self.printer_output_tray_id = False

@api.model
def print_action_for_report_name(self, report_name):
Expand All @@ -61,8 +67,11 @@ def _get_user_default_print_behaviour(self):
return dict(
action=user.printing_action or "client",
printer=user.printing_printer_id or printer_obj.get_default(),
tray=str(user.printer_tray_id.system_name)
if user.printer_tray_id
input_tray=str(user.printer_input_tray_id.system_name)
if user.printer_input_tray_id
else False,
output_tray=str(user.printer_output_tray_id.system_name)
if user.printer_output_tray_id
else False,
)

Expand All @@ -73,8 +82,10 @@ def _get_report_default_print_behaviour(self):
result["action"] = report_action.action_type
if self.printing_printer_id:
result["printer"] = self.printing_printer_id
if self.printer_tray_id:
result["tray"] = self.printer_tray_id.system_name
if self.printer_input_tray_id:
result["input_tray"] = self.printer_input_tray_id.system_name
if self.printer_output_tray_id:
result["output_tray"] = self.printer_output_tray_id.system_name
return result

def behaviour(self):
Expand Down
72 changes: 42 additions & 30 deletions base_report_to_printer/models/printing_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ class PrintingPrinter(models.Model):
model = fields.Char(readonly=True)
location = fields.Char(readonly=True)
uri = fields.Char(string="URI", readonly=True)
tray_ids = fields.One2many(
comodel_name="printing.tray", inverse_name="printer_id", string="Paper Sources"
input_tray_ids = fields.One2many(
comodel_name="printing.tray.input",
inverse_name="printer_id",
string="Paper Sources",
)
output_tray_ids = fields.One2many(
comodel_name="printing.tray.output",
inverse_name="printer_id",
string="Output trays",
)

def _prepare_update_from_cups(self, cups_connection, cups_printer):
Expand Down Expand Up @@ -94,42 +101,44 @@ def _prepare_update_from_cups(self, cups_connection, cups_printer):
return vals

ppd = cups.PPD(ppd_path)
option = ppd.findOption("InputSlot")
input_options = ppd.findOption("InputSlot")
output_options = ppd.findOption("OutputBin")
try:
os.unlink(ppd_path)
except OSError as err:
# ENOENT means No such file or directory
# The file has already been deleted, we can continue the update
if err.errno != errno.ENOENT:
raise
if not option:
return vals

tray_commands = []
cups_trays = {
tray_option["choice"]: tray_option["text"] for tray_option in option.choices
}

# Add new trays
tray_commands.extend(
[
(0, 0, {"name": text, "system_name": choice})
for choice, text in cups_trays.items()
if choice not in self.tray_ids.mapped("system_name")
if input_options:
vals["input_tray_ids"] = [
(0, 0, {"name": opt["text"], "system_name": opt["choice"]})
for opt in input_options.choices
if opt["choice"] not in self.input_tray_ids.mapped("system_name")
]
)

# Remove deleted trays
tray_commands.extend(
[
(2, tray.id)
for tray in self.tray_ids.filtered(
lambda record: record.system_name not in cups_trays.keys()
)
trays = [opt["choice"] for opt in input_options.choices]
vals["input_tray_ids"].extend(
[
(2, tray.id)
for tray in self.input_tray_ids
if tray.system_name not in trays
]
)

if output_options:
vals["output_tray_ids"] = [
(0, 0, {"name": opt["text"], "system_name": opt["choice"]})
for opt in output_options.choices
if opt["choice"] not in self.output_tray_ids.mapped("system_name")
]
)
if tray_commands:
vals["tray_ids"] = tray_commands
trays = [opt["choice"] for opt in output_options.choices]
vals["output_tray_ids"].extend(
[
(2, tray.id)
for tray in self.output_tray_ids
if tray.system_name not in trays
]
)
return vals

def print_document(self, report, content, **print_opts):
Expand All @@ -154,11 +163,14 @@ def _set_option_doc_format(report, value):
# Backwards compatibility of builtin used as kwarg
_set_option_format = _set_option_doc_format

def _set_option_tray(self, report, value):
def _set_option_input_tray(self, report, value):
"""Note we use self here as some older PPD use tray
rather than InputSlot so we may need to query printer in override"""
return {"InputSlot": str(value)} if value else {}

def _set_option_output_tray(self, report, value):
return {"OutputBin": str(value)} if value else {}

@staticmethod
def _set_option_noop(report, value):
return {}
Expand Down
15 changes: 11 additions & 4 deletions base_report_to_printer/models/printing_report_xml_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ class PrintingReportXmlAction(models.Model):
)
printer_id = fields.Many2one(comodel_name="printing.printer", string="Printer")

printer_tray_id = fields.Many2one(
comodel_name="printing.tray",
printer_input_tray_id = fields.Many2one(
comodel_name="printing.tray.input",
string="Paper Source",
domain="[('printer_id', '=', printer_id)]",
)
printer_output_tray_id = fields.Many2one(
comodel_name="printing.tray.output",
string="Output Bin",
domain="[('printer_id', '=', printer_id)]",
)

@api.onchange("printer_id")
def onchange_printer_id(self):
"""Reset the tray when the printer is changed"""
self.printer_tray_id = False
self.printer_input_tray_id = False
self.printer_output_tray_id = False

def behaviour(self):
if not self:
return {}
return {
"action": self.action,
"printer": self.printer_id,
"tray": self.printer_tray_id.system_name,
"input_tray": self.printer_input_tray_id.system_name,
"output_tray": self.printer_output_tray_id.system_name,
}
14 changes: 13 additions & 1 deletion base_report_to_printer/models/printing_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from odoo import fields, models


class PrinterTray(models.Model):
class PrinterTray(models.AbstractModel):
_name = "printing.tray"
_description = "Printer Tray"

Expand All @@ -19,3 +19,15 @@ class PrinterTray(models.Model):
readonly=True,
ondelete="cascade",
)


class PrinterInputTray(models.Model):
_name = "printing.tray.input"
_description = "Printer Tray Input"
_inherit = "printing.tray"


class PrinterOutputTray(models.Model):
_name = "printing.tray.output"
_description = "Printer Tray Output"
_inherit = "printing.tray"
12 changes: 9 additions & 3 deletions base_report_to_printer/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ def SELF_WRITEABLE_FIELDS(self):
"printing_printer_id",
]

printer_tray_id = fields.Many2one(
comodel_name="printing.tray",
printer_input_tray_id = fields.Many2one(
comodel_name="printing.tray.input",
string="Default Printer Paper Source",
domain="[('printer_id', '=', printing_printer_id)]",
)
printer_output_tray_id = fields.Many2one(
comodel_name="printing.tray.output",
string="Default Printer Output Bin",
domain="[('printer_id', '=', printing_printer_id)]",
)

@api.onchange("printing_printer_id")
def onchange_printing_printer_id(self):
"""Reset the tray when the printer is changed"""
self.printer_tray_id = False
self.printer_input_tray_id = False
self.printer_output_tray_id = False
4 changes: 4 additions & 0 deletions base_report_to_printer/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_update_cups,update printer from cups manager,base_report_to_printer.model_printing_printer_update_wizard,base_report_to_printer.printing_group_manager,1,1,1,1
access_print_attachment,print attachment cups manager,base_report_to_printer.model_wizard_print_attachment,base_report_to_printer.printing_group_manager,1,1,1,1
access_print_attachment_line,print attachment line cups manager,base_report_to_printer.model_wizard_print_attachment_line,base_report_to_printer.printing_group_manager,1,1,1,1
26 changes: 22 additions & 4 deletions base_report_to_printer/security/security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,35 @@
<field eval="0" name="perm_create" />
</record>
<record id="access_printing_tray_all" model="ir.model.access">
<field name="name">Printing Tray User</field>
<field name="model_id" ref="model_printing_tray" />
<field name="name">Printing Input Tray User</field>
<field name="model_id" ref="model_printing_tray_input" />
<field name="group_id" ref="printing_group_user" />
<field eval="1" name="perm_read" />
<field eval="0" name="perm_unlink" />
<field eval="0" name="perm_write" />
<field eval="0" name="perm_create" />
</record>
<record id="access_printing_tray_operator" model="ir.model.access">
<field name="name">Printing Tray User</field>
<field name="model_id" ref="model_printing_tray" />
<field name="name">Printing Input Tray Manager</field>
<field name="model_id" ref="model_printing_tray_input" />
<field name="group_id" ref="printing_group_manager" />
<field eval="1" name="perm_read" />
<field eval="1" name="perm_unlink" />
<field eval="1" name="perm_write" />
<field eval="1" name="perm_create" />
</record>
<record id="access_printing_tray_output__all" model="ir.model.access">
<field name="name">Printing Output Tray User</field>
<field name="model_id" ref="model_printing_tray_output" />
<field name="group_id" ref="printing_group_user" />
<field eval="1" name="perm_read" />
<field eval="0" name="perm_unlink" />
<field eval="0" name="perm_write" />
<field eval="0" name="perm_create" />
</record>
<record id="access_printing_tray_output_operator" model="ir.model.access">
<field name="name">Printing Output Tray Manager</field>
<field name="model_id" ref="model_printing_tray_output" />
<field name="group_id" ref="printing_group_manager" />
<field eval="1" name="perm_read" />
<field eval="1" name="perm_unlink" />
Expand Down
Loading

0 comments on commit 7c00129

Please sign in to comment.