From 2112a21113df43dfdc22bbce3bd0c41f972f0c40 Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Fri, 5 Dec 2014 18:43:36 +0100 Subject: [PATCH] Add domain on location's parent and childs issue4279 review9811002 --- CHANGELOG | 1 + location.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 2fd59f0e21..e3c22899ee 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add domain on location's parent and childs * Do not filter out inactive products from stock computation Version 3.4.0 - 2014-10-20 diff --git a/location.py b/location.py index fcdc590cc9..4a88e37557 100644 --- a/location.py +++ b/location.py @@ -5,7 +5,7 @@ from decimal import Decimal from trytond.model import ModelView, ModelSQL, fields from trytond.wizard import Wizard, StateView, Button, StateAction from trytond import backend -from trytond.pyson import Not, Bool, Eval, Equal, PYSONEncoder, Date +from trytond.pyson import Not, Bool, Eval, Equal, PYSONEncoder, Date, If from trytond.transaction import Transaction from trytond.pool import Pool, PoolMeta from trytond.tools import grouped_slice @@ -44,7 +44,11 @@ class Location(ModelSQL, ModelView): ('view', 'View'), ], 'Location type', states=STATES, depends=DEPENDS) parent = fields.Many2One("stock.location", "Parent", select=True, - left="left", right="right") + left="left", right="right", + states={ + 'invisible': Eval('type') == 'warehouse', + }, + depends=['type']) left = fields.Integer('Left', required=True, select=True) right = fields.Integer('Right', required=True, select=True) childs = fields.One2Many("stock.location", "parent", "Children") @@ -104,6 +108,41 @@ class Location(ModelSQL, ModelView): 'child of warehouse "%(warehouse)s".'), }) + parent_domain = [] + childs_domain = [] + childs_mapping = cls._childs_domain() + for type_, allowed_parents in cls._parent_domain().iteritems(): + parent_domain.append(If(Eval('type') == type_, + ('type', 'in', allowed_parents), ())) + childs_domain.append(If(Eval('type') == type_, + ('type', 'in', childs_mapping[type_]), ())) + cls.parent.domain = parent_domain + cls.childs.domain = childs_domain + cls.childs.depends.append('type') + + @classmethod + def _parent_domain(cls): + '''Returns a dict with location types as keys and a list of allowed + parent location types as values''' + return { + 'customer': ['customer'], + 'supplier': ['supplier'], + 'production': ['production'], + 'lost_found': ['lost_found'], + 'view': ['warehouse', 'view', 'storage'], + 'storage': ['warehouse', 'view', 'storage'], + 'warehouse': [''], + } + + @classmethod + def _childs_domain(cls): + childs_domain = {} + for type_, allowed_parents in cls._parent_domain().iteritems(): + for parent in allowed_parents: + childs_domain.setdefault(parent, []) + childs_domain[parent].append(type_) + return childs_domain + @classmethod def __register__(cls, module_name): TableHandler = backend.get('TableHandler')