[REF] event_sale: move from onchange / default to stored editable computed fields

PURPOSE

Try to move from onchange / default_get to stored editable computed fields.
Behavior should be the same (computed or set by user), with support of
create / write / onchange field update without additional code.

SPECIFICATIONS: GLOBAL RULES

Update classic fields updated in some cases by onchange and/or default methods
by fields with store=True, readonly=False. It means their value comes either
from manual user input, either from trigger based computation.

Remove onchange and default_get when possible, leading to an unique computation
method and clearing fields definition.

Also clean some fields definition inconsistencies, notably required fields
that should instead be correctly computed or default that have no real meaning.

LINKS

Task ID 2089156
Community PR odoo/odoo#42911
This commit is contained in:
Thibault Delavallée
2020-03-10 11:15:09 +00:00
parent c6003af734
commit 7666f97693
3 changed files with 36 additions and 24 deletions
@@ -73,13 +73,12 @@ class EventRegistration(models.Model):
'old_ticket_name': registration.event_ticket_id.name,
'new_ticket_name': new_event_ticket.name
}
user_id = registration.event_id.user_id.id or \
registration.sale_order_id.user_id.id or \
fallback_user_id
registration.sale_order_id.activity_schedule_with_view('mail.mail_activity_data_warning',
user_id=user_id,
views_or_xmlid='event_sale.event_ticket_id_change_exception',
render_context=render_context)
user_id = registration.event_id.user_id.id or registration.sale_order_id.user_id.id or fallback_user_id
registration.sale_order_id.activity_schedule_with_view(
'mail.mail_activity_data_warning',
user_id=user_id,
views_or_xmlid='event_sale.event_ticket_id_change_exception',
render_context=render_context)
def _action_set_paid(self):
self.write({'is_paid': True})
+30 -14
View File
@@ -14,25 +14,41 @@ class EventTemplateTicket(models.Model):
def _default_product_id(self):
return self.env.ref('event_sale.product_product_event', raise_if_not_found=False)
description = fields.Text(compute='_compute_description', copy=True, readonly=False, store=True)
# product
product_id = fields.Many2one(
'product.product', string='Product', required=True, domain=[("event_ok", "=", True)],
default=_default_product_id)
price = fields.Float(string='Price', digits='Product Price')
price_reduce = fields.Float(string="Price Reduce", compute="_compute_price_reduce", digits='Product Price')
'product.product', string='Product', required=True,
domain=[("event_ok", "=", True)], default=_default_product_id)
price = fields.Float(
string='Price', compute='_compute_price',
digits='Product Price', copy=True, readonly=False, store=True)
price_reduce = fields.Float(
string="Price Reduce", compute="_compute_price_reduce",
digits='Product Price')
@api.depends('product_id')
def _compute_price_reduce(self):
for record in self:
product = record.product_id
discount = product.lst_price and (product.lst_price - product.price) / product.lst_price or 0.0
record.price_reduce = (1.0 - discount) * record.price
def _compute_price(self):
for ticket in self:
if ticket.product_id and ticket.product_id.lst_price:
ticket.price = ticket.product_id.lst_price or 0
elif not ticket.price:
ticket.price = 0
@api.onchange('product_id')
def _onchange_product_id(self):
self.price = self.product_id.list_price or 0
if not self.description and self.product_id.description_sale:
self.description = self.product_id.description_sale
@api.depends('product_id')
def _compute_description(self):
for ticket in self:
if ticket.product_id and ticket.product_id.description_sale:
ticket.description = ticket.product_id.description_sale
# initialize, i.e for embedded tree views
if not ticket.description:
ticket.description = False
@api.depends('product_id', 'price')
def _compute_price_reduce(self):
for ticket in self:
product = ticket.product_id
discount = product.lst_price and (product.lst_price - product.price) / product.lst_price or 0.0
ticket.price_reduce = (1.0 - discount) * ticket.price
def _init_column(self, column_name):
if column_name != "product_id":
@@ -38,7 +38,6 @@ class TestEventData(TestEventSaleCommon):
'seats_max': 5,
})]
})
event_type.event_type_ticket_ids._onchange_product_id()
self.assertEqual(event_type.event_type_ticket_ids.description, self.event_product.description_sale)
# synchronize event
@@ -112,9 +111,7 @@ class TestEventTicketData(TestEventSaleCommon):
],
})
first_ticket = event.event_ticket_ids.filtered(lambda t: t.name == 'First Ticket')
first_ticket._onchange_product_id()
second_ticket = event.event_ticket_ids.filtered(lambda t: t.name == 'Second Ticket')
second_ticket._onchange_product_id()
# force second ticket price, after calling the onchange
second_ticket.write({'price': 8.0})