Browse Source

[IMP] date_range. Provide sensible domain for parent_id.

pull/1431/head
Ronald Portier 6 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
c9aa4805b4
  1. 33
      date_range/models/date_range.py
  2. 10
      date_range/views/date_range_view.xml

33
date_range/models/date_range.py

@ -142,14 +142,27 @@ class DateRange(models.Model):
(field_name, '<=', self.date_end)] (field_name, '<=', self.date_end)]
@api.multi @api.multi
@api.onchange('type_id')
@api.onchange('company_id', 'type_id', 'date_start', 'date_end')
def onchange_type_id(self): def onchange_type_id(self):
return {
'domain': {
'parent_id': [
'|',
('parent_type_id', '=', False),
('type_id', '=', self.type_id.parent_type_id.id),
]
}
}
"""The type_id and the dates determine the choices for parent."""
domain = []
if self.company_id:
domain.append(('company_id', '=', self.company_id.id))
if self.parent_type_id:
domain.append(('type_id', '=', self.parent_type_id.id))
if self.date_start:
domain.append('|')
domain.append(('date_start', '<=', self.date_start))
domain.append(('date_start', '=', False))
if self.date_end:
domain.append('|')
domain.append(('date_end', '>=', self.date_end))
domain.append(('date_end', '=', False))
if domain:
# If user did not select a parent already, autoselect the last
# (ordered by date_start) or only parent that applies.
if self.type_id and self.date_start and not self.parent_id:
possible_parent = self.search(
domain, limit=1, order='date_start desc')
self.parent_id = possible_parent # can be empty!
return {'domain': {'parent_id': domain}}

10
date_range/views/date_range_view.xml

@ -11,7 +11,10 @@
<field name="date_end"/> <field name="date_end"/>
<field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/> <field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/>
<field name="parent_type_id" invisible="1"/> <field name="parent_type_id" invisible="1"/>
<field name="parent_id"/>
<field
name="parent_id"
options="{'no_create': True}"
/>
<field name="active"/> <field name="active"/>
</tree> </tree>
</field> </field>
@ -28,7 +31,10 @@
<field name="date_end"/> <field name="date_end"/>
<field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/> <field name="company_id" groups="base.group_multi_company" options="{'no_create': True}"/>
<field name="parent_type_id" invisible="1"/> <field name="parent_type_id" invisible="1"/>
<field name="parent_id"/>
<field
name="parent_id"
options="{'no_create': True}"
/>
<field name="active"/> <field name="active"/>
</group> </group>
</form> </form>

Loading…
Cancel
Save