Files
tryton/category.py
T

80 lines
2.6 KiB
Python
Raw Normal View History

2008-10-22 20:07:32 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2009-02-28 17:00:12 +01:00
from trytond.model import ModelView, ModelSQL, fields
2011-08-21 11:20:44 +02:00
from trytond.pyson import Eval
2008-10-22 20:07:32 +02:00
2008-02-26 12:41:13 +01:00
STATES = {
2011-08-21 11:20:44 +02:00
'readonly': ~Eval('active'),
2008-02-26 12:41:13 +01:00
}
2011-08-04 16:57:31 +02:00
DEPENDS = ['active']
2008-02-26 12:41:13 +01:00
SEPARATOR = ' / '
2008-02-15 17:42:25 +01:00
2009-02-28 17:00:12 +01:00
class Category(ModelSQL, ModelView):
2008-05-21 18:12:20 +02:00
"Category"
2008-10-22 20:07:32 +02:00
_name = "party.category"
2008-02-15 17:42:25 +01:00
_description = __doc__
2011-08-04 16:57:31 +02:00
name = fields.Char('Name', required=True, states=STATES, translate=True,
depends=DEPENDS)
2008-10-22 20:07:32 +02:00
parent = fields.Many2One('party.category', 'Parent',
2011-08-04 16:57:31 +02:00
select=1, states=STATES, depends=DEPENDS)
2008-10-22 20:07:32 +02:00
childs = fields.One2Many('party.category', 'parent',
2011-08-04 16:57:31 +02:00
'Children', states=STATES, depends=DEPENDS)
2008-02-26 12:41:13 +01:00
active = fields.Boolean('Active')
2008-03-04 11:50:22 +01:00
def __init__(self):
super(Category, self).__init__()
self._sql_constraints = [
('name_parent_uniq', 'UNIQUE(name, parent)',
'The name of a party category must be unique by parent!'),
]
2008-03-04 11:50:22 +01:00
self._constraints += [
2008-08-05 11:36:25 +02:00
('check_recursion', 'recursive_categories'),
('check_name', 'wrong_name'),
2008-03-04 11:50:22 +01:00
]
2008-08-05 11:36:25 +02:00
self._error_messages.update({
'recursive_categories': 'You can not create recursive categories!',
'wrong_name': 'You can not use "%s" in name field!' % SEPARATOR,
2008-08-05 11:36:25 +02:00
})
2008-04-22 17:48:57 +02:00
self._order.insert(1, ('name', 'ASC'))
2008-02-15 17:42:25 +01:00
2010-08-10 11:59:01 -04:00
def default_active(self):
2008-02-26 16:23:30 +01:00
return 1
2010-08-10 11:59:01 -04:00
def check_name(self, ids):
for category in self.browse(ids):
if SEPARATOR in category.name:
return False
return True
2010-08-10 11:59:01 -04:00
def get_rec_name(self, ids, name):
2008-07-02 12:25:25 +02:00
if not ids:
return {}
2009-02-28 22:37:08 +01:00
res = {}
def _name(category):
if category.id in res:
return res[category.id]
elif category.parent:
return _name(category.parent) + SEPARATOR + category.name
2008-02-15 18:08:25 +01:00
else:
return category.name
2010-08-10 11:59:01 -04:00
for category in self.browse(ids):
res[category.id] = _name(category)
2008-02-15 18:08:25 +01:00
return res
2010-08-10 11:59:01 -04:00
def search_rec_name(self, name, clause):
if isinstance(clause[2], basestring):
values = clause[2].split(SEPARATOR)
values.reverse()
domain = []
field = 'name'
for name in values:
domain.append((field, clause[1], name))
field = 'parent.' + field
2010-08-10 11:59:01 -04:00
ids = self.search(domain, order=[])
return [('id', 'in', ids)]
#TODO Handle list
return [('name',) + tuple(clause[1:])]
2008-02-15 17:42:25 +01:00
Category()