Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][FIX] res_partner_operating_unit - Fix warning on constraint using dot notation #175

Open
wants to merge 1 commit into
base: 12.0.project_MI_465
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions res_partner_operating_unit/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ def search_count(self, args):
('operating_unit_ids', 'in', ou_ids),
('operating_unit_ids', '=', False)]
return super().search_count(domain + args)

@api.model
def create(self, vals):
res = super().create(vals)
user = self.env['res.users'].search([('partner_id', '=', res.id)])
if user:
user._check_partner_operating_unit()
return res

@api.multi
def write(self, vals):
res = super().write(vals)
for partner in self:
if vals.get('operating_unit_ids'):
user = self.env['res.users'].search(
[('partner_id', '=', partner.id)])
if user:
user._check_partner_operating_unit()
return res
21 changes: 11 additions & 10 deletions res_partner_operating_unit/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ class ResUsers(models.Model):
@api.model
def create(self, vals):
res = super().create(vals)
res.partner_id.operating_unit_ids = \
[(4, res.operating_unit_default_id.id)]
res.partner_id.operating_unit_ids |= res.operating_unit_default_id
res.partner_id.operating_unit_ids |= res.operating_unit_ids
self._check_partner_operating_unit()
return res

@api.multi
def write(self, vals):
res = super().write(vals)
if vals.get('default_operating_unit_id'):
# Add the new OU
self.partner_id.operating_unit_ids = \
[(4, res.default_operating_unit_id.id)]
for user in self:
if vals.get('operating_unit_default_id'):
user.partner_id.operating_unit_ids |= user.operating_unit_default_id
user._check_partner_operating_unit()
if vals.get('operating_unit_ids'):
user.partner_id.operating_unit_ids |= user.operating_unit_ids
return res

@api.constrains('partner_id.operating_unit_ids',
'default_operating_unit_id')
def check_partner_operating_unit(self):
def _check_partner_operating_unit(self):
if self.partner_id.operating_unit_ids and \
self.default_operating_unit_id.id not in \
self.operating_unit_default_id.id not in \
self.partner_id.operating_unit_ids.ids:
raise UserError(_(
"The operating units of the partner must include the default "
Expand Down