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 Jun 30, 2023
1 parent 33c759d commit c687b79
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 36 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
25 changes: 1 addition & 24 deletions commown_devices/tests/test_project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@


class ProjectTaskPickingTC(DeviceAsAServiceTC):
def _create_xml_id(self, record, name):
return self.env["ir.model.data"].create(
{
"module": "commown_devices",
"name": name,
"model": record._name,
"res_id": record.id,
}
)

def setUp(self):
super().setUp()

Expand Down Expand Up @@ -58,20 +48,7 @@ def setUp(self):
}
) # for checks on stage change tests

self.ongoing_stage = self.env["project.task.type"].create(
{"name": "Test Ongoing Stage"}
)
self._create_xml_id(self.ongoing_stage, "sup_picking_ongoing_stage")

# Create stage to assign xml_ids so constrains on stage_id pass
t1, t2 = self.env["project.task.type"].create(
[
{"name": "t1"},
{"name": "t2"},
]
)
self._create_xml_id(t1, "diagnostic_stage")
self._create_xml_id(t2, "resiliated_stage")
self.ongoing_stage = self.env.ref("commown_devices.sup_picking_ongoing_stage")

# Create a unused product and an unused service
self.env["product.template"].create(
Expand Down

0 comments on commit c687b79

Please sign in to comment.