From cd47fd451ee66cf8a4af23865fa031daa290cb2b Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Tue, 21 Mar 2023 11:20:59 +0000 Subject: [PATCH] [CLN] website_sale_digital: split product.py (1) Part-of: odoo/odoo#116026 --- .../website_sale_digital/models/__init__.py | 6 +- .../models/{product.py => product_product.py} | 15 +++-- .../models/product_template.py | 62 +++++++++++++++++++ 3 files changed, 72 insertions(+), 11 deletions(-) rename addons/website_sale_digital/models/{product.py => product_product.py} (85%) create mode 100644 addons/website_sale_digital/models/product_template.py diff --git a/addons/website_sale_digital/models/__init__.py b/addons/website_sale_digital/models/__init__.py index dc2af98c7f4..4efa7114347 100644 --- a/addons/website_sale_digital/models/__init__.py +++ b/addons/website_sale_digital/models/__init__.py @@ -1,6 +1,6 @@ -# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from . import ir_attachment from . import account_invoice -from . import product +from . import ir_attachment +from . import product_product +from . import product_template diff --git a/addons/website_sale_digital/models/product.py b/addons/website_sale_digital/models/product_product.py similarity index 85% rename from addons/website_sale_digital/models/product.py rename to addons/website_sale_digital/models/product_product.py index ec6329b80c0..db9279a2ac3 100644 --- a/addons/website_sale_digital/models/product.py +++ b/addons/website_sale_digital/models/product_product.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import api, fields, models, _ @@ -11,7 +10,7 @@ class ProductTemplate(models.Model): def _compute_attachment_count(self): attachment_data = self.env['ir.attachment']._read_group([('res_model', '=', self._name), ('res_id', 'in', self.ids), ('product_downloadable', '=', True)], ['res_id'], ['res_id']) - mapped_data = dict([(data['res_id'], data['res_id_count']) for data in attachment_data]) + mapped_data = {data['res_id']: data['res_id_count'] for data in attachment_data} for product_template in self: product_template.attachment_count = mapped_data.get(product_template.id, 0) @@ -25,9 +24,9 @@ class ProductTemplate(models.Model): 'view_mode': 'kanban,form', 'context': "{'default_res_model': '%s','default_res_id': %d, 'default_product_downloadable': True}" % (self._name, self.id), 'help': """ -

%s

-

%s

- """ % (_("Add attachments for this digital product"), +

{}

+

{}

+ """.format(_("Add attachments for this digital product"), _("The attached files are the ones that will be purchased and sent to the customer.")), } @@ -56,8 +55,8 @@ class Product(models.Model): 'view_mode': 'kanban,form', 'context': "{'default_res_model': '%s','default_res_id': %d, 'default_product_downloadable': True}" % (self._name, self.id), 'help': """ -

%s

-

%s

- """ % (_("Add attachments for this digital product"), +

{}

+

{}

+ """.format(_("Add attachments for this digital product"), _("The attached files are the ones that will be purchased and sent to the customer.")), } diff --git a/addons/website_sale_digital/models/product_template.py b/addons/website_sale_digital/models/product_template.py new file mode 100644 index 00000000000..db9279a2ac3 --- /dev/null +++ b/addons/website_sale_digital/models/product_template.py @@ -0,0 +1,62 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, _ + + +class ProductTemplate(models.Model): + _inherit = ['product.template'] + + attachment_count = fields.Integer(compute='_compute_attachment_count', string="File") + + def _compute_attachment_count(self): + attachment_data = self.env['ir.attachment']._read_group([('res_model', '=', self._name), ('res_id', 'in', self.ids), ('product_downloadable', '=', True)], ['res_id'], ['res_id']) + mapped_data = {data['res_id']: data['res_id_count'] for data in attachment_data} + for product_template in self: + product_template.attachment_count = mapped_data.get(product_template.id, 0) + + def action_open_attachments(self): + self.ensure_one() + return { + 'name': _('Digital Attachments'), + 'domain': [('res_model', '=', self._name), ('res_id', '=', self.id), ('product_downloadable', '=', True)], + 'res_model': 'ir.attachment', + 'type': 'ir.actions.act_window', + 'view_mode': 'kanban,form', + 'context': "{'default_res_model': '%s','default_res_id': %d, 'default_product_downloadable': True}" % (self._name, self.id), + 'help': """ +

{}

+

{}

+ """.format(_("Add attachments for this digital product"), + _("The attached files are the ones that will be purchased and sent to the customer.")), + } + + +class Product(models.Model): + _inherit = 'product.product' + + attachment_count = fields.Integer(compute='_compute_attachment_count', string="File") + + def _compute_attachment_count(self): + for product in self: + product.attachment_count = self.env['ir.attachment'].search_count([ + '|', + '&', '&', ('res_model', '=', 'product.template'), ('res_id', '=', product.product_tmpl_id.id), ('product_downloadable', '=', True), + '&', '&', ('res_model', '=', 'product.product'), ('res_id', '=', product.id), ('product_downloadable', '=', True)]) + + def action_open_attachments(self): + self.ensure_one() + return { + 'name': _('Digital Attachments'), + 'domain': [('product_downloadable', '=', True), '|', + '&', ('res_model', '=', 'product.template'), ('res_id', '=', self.product_tmpl_id.id), + '&', ('res_model', '=', self._name), ('res_id', '=', self.id)], + 'res_model': 'ir.attachment', + 'type': 'ir.actions.act_window', + 'view_mode': 'kanban,form', + 'context': "{'default_res_model': '%s','default_res_id': %d, 'default_product_downloadable': True}" % (self._name, self.id), + 'help': """ +

{}

+

{}

+ """.format(_("Add attachments for this digital product"), + _("The attached files are the ones that will be purchased and sent to the customer.")), + }