Browse Source

[ADD] parent_type_id for domain in parent-child(s) relation.

pull/1431/head
Nikos Tsirintanis 6 years ago
committed by Laurent Mignon (ACSONE)
parent
commit
5042760a36
  1. 9
      date_range/models/date_range.py
  2. 17
      date_range/models/date_range_type.py
  3. 65
      date_range/tests/test_date_range.py
  4. 4
      date_range/views/date_range_view.xml

9
date_range/models/date_range.py

@ -31,8 +31,15 @@ class DateRange(models.Model):
active = fields.Boolean( active = fields.Boolean(
help="The active field allows you to hide the date range without " help="The active field allows you to hide the date range without "
"removing it.", default=True) "removing it.", default=True)
parent_type_id = fields.Many2one(
related='type_id.parent_type_id',
store=True,
readonly=True)
parent_id = fields.Many2one( parent_id = fields.Many2one(
comodel_name='date.range', string="Parent", index=1)
comodel_name='date.range', string="Parent",
domain="['|', ('type_id.parent_type_id', '!=', parent_type_id), "
"('parent_type_id', '=', False)]",
index=1)
_sql_constraints = [ _sql_constraints = [
('date_range_uniq', 'unique (name,type_id, company_id)', ('date_range_uniq', 'unique (name,type_id, company_id)',

17
date_range/models/date_range_type.py

@ -26,6 +26,9 @@ class DateRangeType(models.Model):
comodel_name='res.company', string='Company', index=1, comodel_name='res.company', string='Company', index=1,
default=_default_company) default=_default_company)
date_range_ids = fields.One2many('date.range', 'type_id', string='Ranges') date_range_ids = fields.One2many('date.range', 'type_id', string='Ranges')
parent_type_id = fields.Many2one(
comodel_name='date.range.type',
index=1)
_sql_constraints = [ _sql_constraints = [
('date_range_type_uniq', 'unique (name,company_id)', ('date_range_type_uniq', 'unique (name,company_id)',
@ -44,3 +47,17 @@ class DateRangeType(models.Model):
_('You cannot change the company, as this ' _('You cannot change the company, as this '
'Date Range Type is assigned to Date Range ' 'Date Range Type is assigned to Date Range '
'(%s).') % (rec.date_range_ids.name_get()[0][1])) '(%s).') % (rec.date_range_ids.name_get()[0][1]))
@api.constrains('parent_type_id')
def _validate_parent_type_id(self):
for record in self:
parent = record
while parent:
if not parent.parent_type_id:
break
if record.parent_type_id == parent:
raise ValidationError(
_("A type parent can not have a parent:"
" %s can not have %s as parent") % (
parent.name, record.name))
parent = parent.parent_type_id

65
date_range/tests/test_date_range.py

@ -184,7 +184,7 @@ class DateRangeTest(TransactionCase):
'FS2018-period1 not a subrange of FS2018: ' 'FS2018-period1 not a subrange of FS2018: '
'FS2018-period1 range not in 2018-01-01 - 2018-12-31' 'FS2018-period1 range not in 2018-01-01 - 2018-12-31'
) )
period1 = self.date_range.create({
self.date_range.create({
'name': 'FS2018-period1', 'name': 'FS2018-period1',
'date_start': '2018-01-01', 'date_start': '2018-01-01',
'date_end': '2018-04-30', 'date_end': '2018-04-30',
@ -205,6 +205,65 @@ class DateRangeTest(TransactionCase):
cm.exception.name, cm.exception.name,
'FS2018-period2 overlaps FS2018-period1' 'FS2018-period2 overlaps FS2018-period1'
) )
def test_parent_type_id(self):
"""Check domain and constraint between parent and child types"""
date_range_type = self.env['date.range.type']
# First create a parent type
parent_type = date_range_type.create({
'name': 'FS2018_parent_type',
'parent_type_id': False,
})
# catch here the validation error when assigning
# a date_range_type parent, to another parent(self included)
with self.assertRaises(ValidationError)as cm, self.env.cr.savepoint():
parent_type.write({
'parent_type_id': parent_type.id,
})
self.assertEqual(
cm.exception.name,
'A type parent can not have a parent:'
' FS2018_parent_type can not have FS2018_parent_type as parent'
)
# Then, add a child type
child_type = date_range_type.create({
'name': 'FS2018_child_type',
'parent_type_id': parent_type.id,
})
# Now create a parent range
parent_range = self.date_range.create({
'name': 'FS2018',
'date_start': '2018-01-01',
'date_end': '2018-12-31',
'type_id': parent_type.id,
})
# and two child ranges
child_range1 = self.date_range.create({
'name': 'FS2018-child1',
'date_start': '2018-01-01',
'date_end': '2018-04-30',
'type_id': child_type.id,
'parent_id': parent_range.id,
})
child_range2 = self.date_range.create({
'name': 'FS2018-child2',
'date_start': '2018-05-01',
'date_end': '2018-06-30',
'type_id': child_type.id,
'parent_id': parent_range.id,
})
# and check how parent_type_id behaves
self.assertEqual(parent_type, child_range1.parent_type_id)
self.assertEqual(
child_range1.parent_type_id,
child_range2.parent_type_id
)
# Ensure here that parent and children are of different type # Ensure here that parent and children are of different type
self.assertNotEqual(parent.type_id, period1.type_id)
self.assertEqual(parent.type_id, period1.parent_id.type_id)
self.assertNotEqual(
parent_range.type_id,
child_range1.type_id
)
self.assertEqual(
parent_range.type_id,
child_range2.parent_id.type_id
)

4
date_range/views/date_range_view.xml

@ -10,6 +10,7 @@
<field name="date_start"/> <field name="date_start"/>
<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_id"/> <field name="parent_id"/>
<field name="active"/> <field name="active"/>
</tree> </tree>
@ -26,6 +27,7 @@
<field name="date_start"/> <field name="date_start"/>
<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_id"/> <field name="parent_id"/>
<field name="active"/> <field name="active"/>
</group> </group>
@ -40,6 +42,7 @@
<field name="name"/> <field name="name"/>
<field name="allow_overlap"/> <field name="allow_overlap"/>
<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"/>
<field name="active"/> <field name="active"/>
</tree> </tree>
</field> </field>
@ -53,6 +56,7 @@
<field name="name"/> <field name="name"/>
<field name="allow_overlap"/> <field name="allow_overlap"/>
<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"/>
<field name="active"/> <field name="active"/>
</group> </group>
</form> </form>

Loading…
Cancel
Save