|
2 | 2 | # Copyright (C) 2019 Serpent Consulting Services |
3 | 3 | # Copyright (C) 2019 Open Source Integrators |
4 | 4 | # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). |
5 | | -from odoo import _, api, models |
| 5 | +from odoo import api, models |
6 | 6 | from odoo.exceptions import UserError |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class ResUsers(models.Model): |
10 | 10 | _inherit = "res.users" |
11 | 11 |
|
12 | | - @api.model |
13 | | - def create(self, vals): |
14 | | - res = super().create(vals) |
| 12 | + @api.model_create_multi |
| 13 | + def create(self, vals_list): |
| 14 | + res = super().create(vals_list) |
15 | 15 | res.partner_id.operating_unit_ids = [(4, res.default_operating_unit_id.id)] |
| 16 | + res.check_partner_operating_unit() |
16 | 17 | return res |
17 | 18 |
|
18 | | - @api.multi |
19 | 19 | def write(self, vals): |
20 | | - res = super().write(vals) |
21 | | - if vals.get("default_operating_unit_id"): |
22 | | - # Add the new OU |
23 | | - self.partner_id.operating_unit_ids = [ |
24 | | - (4, vals["default_operating_unit_id"]) |
25 | | - ] |
26 | | - return res |
| 20 | + for user in self: |
| 21 | + res = super().write(vals) |
| 22 | + if vals.get("default_operating_unit_id"): |
| 23 | + # Add the new OU |
| 24 | + user.partner_id.operating_unit_ids = [ |
| 25 | + (4, user.default_operating_unit_id.id) |
| 26 | + ] |
| 27 | + user.check_partner_operating_unit() |
| 28 | + return res |
27 | 29 |
|
28 | | - @api.constrains("partner_id.operating_unit_ids", "default_operating_unit_id") |
29 | 30 | def check_partner_operating_unit(self): |
30 | 31 | if ( |
31 | 32 | self.partner_id.operating_unit_ids |
32 | 33 | and self.default_operating_unit_id.id |
33 | 34 | not in self.partner_id.operating_unit_ids.ids |
34 | 35 | ): |
35 | 36 | raise UserError( |
36 | | - _( |
| 37 | + self.env._( |
37 | 38 | "The operating units of the partner must include the default " |
38 | 39 | "one of the user." |
39 | 40 | ) |
|
0 commit comments