Browse Source

[IMP] Added is_unique check (#688)

* [IMP] Added is_unique check
pull/702/head
Steve Campbell 6 years ago
committed by Maxime Chambreuil
parent
commit
a02ddf0dbb
  1. 33
      partner_multi_relation/models/res_partner_relation.py
  2. 6
      partner_multi_relation/models/res_partner_relation_type.py
  3. 2
      partner_multi_relation/views/res_partner_relation_type.xml

33
partner_multi_relation/models/res_partner_relation.py

@ -43,12 +43,22 @@ class ResPartnerRelation(models.Model):
@api.model
def create(self, vals):
"""Override create to correct values, before being stored."""
"""Override create to correct values, before being stored.
Check to make sure each unique relation stays unique"""
context = self.env.context
if 'left_partner_id' not in vals and context.get('active_id'):
vals['left_partner_id'] = context.get('active_id')
if not self.check_is_unique(vals):
raise ValidationError(_('Relation already exists!'))
return super(ResPartnerRelation, self).create(vals)
@api.model
def write(self, vals):
"""Check to make sure each unique relation stays unique"""
if not self.check_is_unique(vals):
raise ValidationError(_('Relation already exists!'))
return super(ResPartnerRelation, self).write(vals)
@api.constrains('date_start', 'date_end')
def _check_dates(self):
"""End date should not be before start date, if not filled
@ -154,3 +164,24 @@ class ResPartnerRelation(models.Model):
_('There is already a similar relation with '
'overlapping dates')
)
@api.multi
def check_is_unique(self, vals):
if 'type_id' in vals:
type_id = vals['type_id']
else:
type_id = self.type_id.id
type_rec = self.env['res.partner.relation.type'].\
search([('id', '=', type_id)])
if 'right_partner_id' in vals:
right = vals['right_partner_id']
else:
right = self.right_partner_id.id
res = self.search([('type_id', '=', type_id),
('right_partner_id', '=', right)])
if (type_rec.is_unique and len(res) > 0):
return False
for rec in res:
if rec.type_id.is_unique:
return False
return True

6
partner_multi_relation/models/res_partner_relation_type.py

@ -73,6 +73,11 @@ class ResPartnerRelationType(models.Model):
" that do not fit the new criteria.\n"
"Specify how this situation should be handled.",
)
is_unique = fields.Boolean(
string='Is Unique',
help="If a relation type is unique, then there cannot be 2"
"relations with the same parties and the same type.",
)
@api.model
def get_partner_types(self):
@ -255,7 +260,6 @@ class ResPartnerRelationType(models.Model):
def write(self, vals):
"""Handle existing relations if conditions change."""
self.check_existing(vals)
for rec in self:
rec_vals = vals.copy()
if rec_vals.get('is_symmetric', rec.is_symmetric):

2
partner_multi_relation/views/res_partner_relation_type.xml

@ -11,6 +11,7 @@
<field name="contact_type_right" />
<field name="allow_self" />
<field name="is_symmetric" />
<field name="is_unique" />
</tree>
</field>
</record>
@ -47,6 +48,7 @@
<field name="allow_self" />
<field name="is_symmetric" />
<field name="handle_invalid_onchange" />
<field name="is_unique" />
</group>
</sheet>
</form>

Loading…
Cancel
Save