Skip to content

Commit

Permalink
Refactoring to use constrain instead of write
Browse files Browse the repository at this point in the history
  • Loading branch information
louck committed Mar 16, 2023
1 parent 71841ed commit b0b94db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
12 changes: 4 additions & 8 deletions commown_devices/models/crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ def action_generate_picking(self):
"context": {"default_lead_id": self.id},
}

@api.multi
def write(self, values):
res = super().write(values)
stage_id = values.get("stage_id")
if stage_id:
stage = self.env["crm.stage"].browse(stage_id)
if "[stock: check-has-picking]" in stage.name:
@api.constrains("stage_id")
def _check_picking_on_stage_change(self):
if self.stage_id.name:
if "[stock: check-has-picking]" in self.stage_id.name:
self.action_check_waiting_picking()
return res

def action_check_waiting_picking(self):
if self.so_line_id.product_id.product_tmpl_id.storable_product_id:
Expand Down
9 changes: 5 additions & 4 deletions commown_devices/tests/test_crm_lead.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from odoo import fields
from odoo.exceptions import UserError
from odoo.exceptions import UserError, ValidationError

from .common import DeviceAsAServiceTC

Expand All @@ -21,9 +21,10 @@ def test_picking_confirmation_on_delivery(self):

# Should not be able to put the lead to a "go" column without a picking:
stage = self.env["crm.stage"].create({"name": "GO [stock: check-has-picking]"})
with self.assertRaises(UserError) as err:
lead.stage_id = stage.id
self.assertEqual("Lead has no assigned picking.", err.exception.name)
with self.assertRaises(ValidationError):
with self.assertRaises(UserError) as err:
lead.stage_id = stage.id
self.assertEqual("Lead has no assigned picking.", err.exception.name)

picking = lead.contract_id.send_device(quant)
self.assertEqual(picking.mapped("move_lines.product_qty"), [1.0])
Expand Down

0 comments on commit b0b94db

Please sign in to comment.