Skip to content

Commit

Permalink
[FIX] partner_multi_relation. Allow install on empty database.
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed Nov 2, 2017
1 parent f383bb6 commit eb6964d
Showing 1 changed file with 40 additions and 62 deletions.
102 changes: 40 additions & 62 deletions partner_multi_relation/models/res_partner_relation_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def register_select_specification(base_name, is_inverse, select_sql):
key_offset=_last_key_offset,
select_sql=select_sql % {
'key_offset': _last_key_offset,
'is_inverse': is_inverse
},
)
'is_inverse': is_inverse})


def get_select_specification(base_name, is_inverse):
Expand Down Expand Up @@ -105,68 +103,66 @@ class ResPartnerRelationAll(models.AbstractModel):
_log_access = False
_name = 'res.partner.relation.all'
_description = 'All (non-inverse + inverse) relations between partners'
_order = (
'this_partner_id, type_selection_id,'
'date_end desc, date_start desc'
)
_order = \
'this_partner_id, type_selection_id, date_end desc, date_start desc'

res_model = fields.Char(
string='Resource Model',
readonly=True,
required=True,
help="The database object this relation is based on."
)
help="The database object this relation is based on.")
res_id = fields.Integer(
string='Resource ID',
readonly=True,
required=True,
help="The id of the object in the model this relation is based on."
)
help="The id of the object in the model this relation is based on.")
this_partner_id = fields.Many2one(
comodel_name='res.partner',
string='One Partner',
required=True,
)
required=True)
other_partner_id = fields.Many2one(
comodel_name='res.partner',
string='Other Partner',
required=True,
)
required=True)
type_id = fields.Many2one(
comodel_name='res.partner.relation.type',
string='Underlying Relation Type',
readonly=True,
required=True,
)
required=True)
date_start = fields.Date('Starting date')
date_end = fields.Date('Ending date')
is_inverse = fields.Boolean(
string="Is reverse type?",
readonly=True,
help="Inverse relations are from right to left partner.",
)
help="Inverse relations are from right to left partner.")
type_selection_id = fields.Many2one(
comodel_name='res.partner.relation.type.selection',
string='Relation Type',
required=True,
)
required=True)
active = fields.Boolean(
string='Active',
readonly=True,
help="Records with date_end in the past are inactive",
)
help="Records with date_end in the past are inactive")
any_partner_id = fields.Many2many(
comodel_name='res.partner',
string='Partner',
compute=lambda self: None,
search='_search_any_partner_id'
)
search='_search_any_partner_id')

def _get_active_selects(self):
"""Return selects actually to be used.
Selects are registered from all modules PRESENT. But should only be
used to build view if module actually INSTALLED.
"""
return ['relation', 'relation_inverse']

def _get_statement(self):
"""Allow other modules to add to statement."""
active_selects = self._get_active_selects()
union_select = 'UNION '.join(
[x['select_sql']
for x in _specification_register.values()])
[_specification_register[key]['select_sql']
for key in active_selects])
return """\
CREATE OR REPLACE VIEW %%(table)s AS
WITH base_selection AS (%(union_select)s)
Expand Down Expand Up @@ -226,8 +222,7 @@ def _search_any_partner_id(self, operator, value):
return [
'|',
('this_partner_id', operator, value),
('other_partner_id', operator, value),
]
('other_partner_id', operator, value)]

@api.multi
def name_get(self):
Expand All @@ -236,9 +231,7 @@ def name_get(self):
this.this_partner_id.name,
this.type_selection_id.display_name,
this.other_partner_id.name,
)
for this in self
}
) for this in self}

@api.onchange('type_selection_id')
def onchange_type_selection_id(self):
Expand All @@ -260,41 +253,34 @@ def check_partner_domain(partner, partner_domain, side):
if partner:
warning['message'] = (
_('%s partner incompatible with relation type.') %
side.title()
)
side.title())
else:
warning['message'] = (
_('No %s partner available for relation type.') %
side
)
side)
return warning

this_partner_domain = []
other_partner_domain = []
if self.type_selection_id.contact_type_this:
this_partner_domain.append((
'is_company', '=',
self.type_selection_id.contact_type_this == 'c'
))
self.type_selection_id.contact_type_this == 'c'))
if self.type_selection_id.partner_category_this:
this_partner_domain.append((
'category_id', 'in',
self.type_selection_id.partner_category_this.ids
))
self.type_selection_id.partner_category_this.ids))
if self.type_selection_id.contact_type_other:
other_partner_domain.append((
'is_company', '=',
self.type_selection_id.contact_type_other == 'c'
))
self.type_selection_id.contact_type_other == 'c'))
if self.type_selection_id.partner_category_other:
other_partner_domain.append((
'category_id', 'in',
self.type_selection_id.partner_category_other.ids
))
self.type_selection_id.partner_category_other.ids))
result = {'domain': {
'this_partner_id': this_partner_domain,
'other_partner_id': other_partner_domain,
}}
'other_partner_id': other_partner_domain}}
# Check wether domain results in no choice or wrong choice of partners:
warning = {}
partner_model = self.env['res.partner']
Expand All @@ -312,20 +298,17 @@ def check_partner_domain(partner, partner_domain, side):
if this_partner_id:
this_partner = partner_model.browse(this_partner_id)
warning = check_partner_domain(
this_partner, this_partner_domain, _('this')
)
this_partner, this_partner_domain, _('this'))
if not warning and other_partner_domain:
warning = check_partner_domain(
self.other_partner_id, other_partner_domain, _('other')
)
self.other_partner_id, other_partner_domain, _('other'))
if warning:
result['warning'] = warning
return result

@api.onchange(
'this_partner_id',
'other_partner_id',
)
'other_partner_id')
def onchange_partner_id(self):
"""Set domain on type_selection_id based on partner(s) selected."""

Expand All @@ -340,15 +323,13 @@ def check_type_selection_domain(type_selection_domain):
return warning
test_domain = (
[('id', '=', self.type_selection_id.id)] +
type_selection_domain
)
type_selection_domain)
type_model = self.env['res.partner.relation.type.selection']
types_found = type_model.search(test_domain, limit=1)
if not types_found:
warning['title'] = _('Error!')
warning['message'] = _(
'Relation type incompatible with selected partner(s).'
)
'Relation type incompatible with selected partner(s).')
return warning

type_selection_domain = []
Expand All @@ -361,8 +342,7 @@ def check_type_selection_domain(type_selection_domain):
'|',
('partner_category_this', '=', False),
('partner_category_this', 'in',
self.this_partner_id.category_id.ids),
]
self.this_partner_id.category_id.ids)]
if self.other_partner_id:
type_selection_domain += [
'|',
Expand All @@ -372,11 +352,9 @@ def check_type_selection_domain(type_selection_domain):
'|',
('partner_category_other', '=', False),
('partner_category_other', 'in',
self.other_partner_id.category_id.ids),
]
self.other_partner_id.category_id.ids)]
result = {'domain': {
'type_selection_id': type_selection_domain,
}}
'type_selection_id': type_selection_domain}}
# Check wether domain results in no choice or wrong choice for
# type_selection_id:
warning = check_type_selection_domain(type_selection_domain)
Expand Down

0 comments on commit eb6964d

Please sign in to comment.