Skip to content

Commit

Permalink
[FIX] fieldservice_activity: deprecated field "states"
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantodorovich committed Nov 21, 2024
1 parent 694d8b9 commit 25f6f86
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
45 changes: 32 additions & 13 deletions fieldservice_activity/models/fsm_activity.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
# Copyright (C) 2019 Open Source Integrators
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo import _, fields, models
from odoo.exceptions import ValidationError


class FSMActivity(models.Model):
_name = "fsm.activity"
_description = "Field Service Activity"

name = fields.Char(
required=True, readonly=True, states={"todo": [("readonly", False)]}
)
required = fields.Boolean(
default=False,
readonly=True,
states={"todo": [("readonly", False)]},
)
name = fields.Char(required=True)
required = fields.Boolean()
sequence = fields.Integer()
completed = fields.Boolean(default=False)
completed = fields.Boolean()
completed_on = fields.Datetime(readonly=True)
completed_by = fields.Many2one("res.users", readonly=True)
ref = fields.Char(
"Reference", readonly=True, states={"todo": [("readonly", False)]}
)
ref = fields.Char("Reference")
fsm_order_id = fields.Many2one("fsm.order", "FSM Order")
fsm_template_id = fields.Many2one("fsm.template", "FSM Template")
state = fields.Selection(
Expand All @@ -31,6 +24,32 @@ class FSMActivity(models.Model):
default="todo",
)

@property
def _protected_fields(self) -> tuple[str]:
"""Fields that should not be modified on a "done" activity"""
return ("name", "required", "ref")

def write(self, vals):
if any(fname in vals for fname in self._protected_fields) and any(
rec.state != "todo" for rec in self.with_context(prefetch_fields=False)
):
fnames = list(set(self._protected_fields) & set(vals.keys()))
field_strings = [

Check warning on line 37 in fieldservice_activity/models/fsm_activity.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_activity/models/fsm_activity.py#L36-L37

Added lines #L36 - L37 were not covered by tests
self._fields[fname]._description_string(self.env) for fname in fnames
]
state_string = dict(

Check warning on line 40 in fieldservice_activity/models/fsm_activity.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_activity/models/fsm_activity.py#L40

Added line #L40 was not covered by tests
self._fields["state"]._description_selection(self.env)
).get(self.state)
raise ValidationError(

Check warning on line 43 in fieldservice_activity/models/fsm_activity.py

View check run for this annotation

Codecov / codecov/patch

fieldservice_activity/models/fsm_activity.py#L43

Added line #L43 was not covered by tests
_(
"It is forbidden to modify the following fields in a %(state)r "
"Activity:\n%(fields)s",
state=state_string,
fields="\n".join(field_strings),
)
)
return super().write(vals)

def action_done(self):
self.write(
{
Expand Down
6 changes: 3 additions & 3 deletions fieldservice_activity/views/fsm_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<field name="order_activity_ids" nolabel="1">
<tree editable="bottom">
<field name="sequence" widget="handle" />
<field name="name" />
<field name="required" />
<field name="ref" />
<field name="name" readonly="state != 'todo'" />
<field name="required" readonly="state != 'todo'" />
<field name="ref" readonly="state != 'todo'" />
<field name="completed" column_invisible="1" />
<field name="completed_on" />
<field name="completed_by" />
Expand Down
6 changes: 3 additions & 3 deletions fieldservice_activity/views/fsm_template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<field name="temp_activity_ids" nolabel="1">
<tree editable="bottom">
<field name="sequence" widget="handle" />
<field name="name" />
<field name="required" />
<field name="ref" />
<field name="name" readonly="state != 'todo'" />
<field name="required" readonly="state != 'todo'" />
<field name="ref" readonly="state != 'todo'" />
<field name="completed" invisible="1" />
<field name="completed_on" invisible="1" />
<field name="completed_by" invisible="1" />
Expand Down

0 comments on commit 25f6f86

Please sign in to comment.