Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
807 มุมมอง
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError

class AttachmentType(models.Model):
_name = 'ir.attachment.type'
_description = 'Attachment Files Type'

name = fields.Char(required=True, string='Type', translate=True)
status = fields.Selection(string='Attachment automatically',
selection=[('yes', "Yes"),
('no', "No")],
required=True, default="yes")
attachment_ids = fields.One2many(comodel_name='ir.attachment', inverse_name='type_id', string='Attachment')

class IrAttachment(models.Model):
_inherit = 'ir.attachment'

type_id = fields.Many2one('ir.attachment.type', string='Extension')

@api.model
def create(self, vals):
if vals.get('product_tmpl_id', 0) != 0:
vals['res_id'] = vals['product_tmpl_id']
vals['res_model'] = 'product.template'

if vals.get('product_id', 0) != 0:
vals['res_id'] = vals['product_id']
vals['res_model'] = 'product.product'
return super(IrAttachment, self).create(vals)

def write(self, vals):
if vals.get('product_tmpl_id', 0) != 0:
vals['res_id'] = vals['product_tmpl_id']
vals['res_model'] = 'product.template'

if vals.get('product_id', 0) != 0:
vals['res_id'] = vals['product_id']
vals['res_model'] = 'product.product'
return super(IrAttachment, self).write(vals)

class ProductProduct(models.Model):
_inherit = 'product.product'

attachment_count = fields.Integer(compute='_attachment', string='Attachments')

def _attachment(self):
for record in self:
where = [('res_model', '=', 'product.product'), ('res_id', 'in', [record.id])]
result = self.env['ir.attachment'].search(where)
record.attachment_count = len(result)

def action_view_attachments(self):
for record in self:
return {
'type': 'ir.actions.act_window',
'name': _('Attachment'),
'res_model': 'ir.attachment',
'view_mode': 'tree,form',
'domain': [('res_model', '=', 'product.product'), ('res_id', 'in', [record.id])],
'context': {'default_res_id': record.id, 'default_res_model': 'product.product',
'default_res_name': record.name},
}


class ProductTemplate(models.Model):
_inherit = 'product.template'

attachment_count = fields.Integer(compute='_attachment', string='Attachments')

def _attachment(self):
for record in self:
where = [('res_model', '=', 'product.template'), ('res_id', 'in', [record.id])]
result = self.env['ir.attachment'].search(where)
record.attachment_count = len(result)

def action_view_attachments(self):
for record in self:
return {
'type': 'ir.actions.act_window',
'name': _('Attachment'),
'res_model': 'ir.attachment',
'view_mode': 'tree,form',
'domain': [('res_model', '=', 'product.template'), ('res_id', 'in', [record.id])],
'context': {'default_res_id': record.id, 'default_res_model': 'product.template',
'default_res_name': record.name},
}
##### Following code block not working for Odoo 17 but works for Odoo16
class Template(models.Model):
_inherit = 'mail.template'

automatically_ids = fields.Many2many('ir.attachment.type', string='Attachment automatically')

class MailCompose(models.TransientModel):
_inherit = 'mail.compose.message'

def _onchange_template_id(self, template_id, composition_mode, model, res_id):
result = super(MailCompose, self)._onchange_template_id(template_id, composition_mode, model, res_id)
value = result["value"]
if model == "sale.order":
# print("sale_order")
sale = self.env[model].browse(res_id)
template = self.env["mail.template"].browse(template_id)
for line in sale.order_line:
where = [('res_model', '=', 'product.product'), ('res_id', 'in', [line.product_id.id])]
attachments = self.env['ir.attachment'].search(where)
for attachment in attachments:
if attachment.type_id.status == "yes":
if attachment.type_id in template.automatically_ids:
if value["attachment_ids"][0][2]:
value["attachment_ids"][0][2].append(attachment.id)

where = [('res_model', '=', 'product.template'), ('res_id', 'in', [line.product_id.product_tmpl_id])]
attachments = self.env['ir.attachment'].search(where)
for attachment in attachments:
if attachment.type_id.status == "yes":
if attachment.type_id in template.automatically_ids:
if value["attachment_ids"][0][2]:
value["attachment_ids"][0][2].append(attachment.id)


if model == "account.move":
account = self.env[model].browse(res_id)
template = self.env["mail.template"].browse(template_id)
for line in account.invoice_line_ids:
where = [('res_model', '=', 'product.product'), ('res_id', 'in', [line.product_id.id])]
attachments = self.env['ir.attachment'].search(where)
for attachment in attachments:
if attachment.type_id.status == "yes":
if attachment.type_id in template.automatically_ids:
if value["attachment_ids"][0][2]:
value["attachment_ids"][0][2].append(attachment.id)

where = [('res_model', '=', 'product.template'), ('res_id', 'in', [line.product_id.product_tmpl_id])]
attachments = self.env['ir.attachment'].search(where)
for attachment in attachments:
if attachment.type_id.status == "yes":
if attachment.type_id in template.automatically_ids:
if value["attachment_ids"][0][2]:
value["attachment_ids"][0][2].append(attachment.id)

return {'value': value}

##### END above code block not working for Odoo 17 but works for Odoo16

Can someone please guide me what's the issue here with the code and why "_inherit = 'mail.compose.message'" method is working for Odoo 16 but not for Odoo 17?

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
3
ก.ค. 25
1963
1
มิ.ย. 25
2286
2
พ.ค. 25
1959
1
พ.ค. 25
1183
0
มี.ค. 25
693