From c0c9fdb9e1abb6f151ece0ada37d425547451c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 25 Mar 2008 11:29:26 +0100 Subject: [PATCH] Use string for xxx2Many operator --- trytond/osv/fields.py | 62 +++++++++++++++++++++------------------- trytond/osv/orm.py | 16 +++++------ trytond/res/group.xml | 2 +- trytond/res/ir.xml | 16 +++++------ trytond/res/user.xml | 2 +- trytond/tools/convert.py | 6 ++-- 6 files changed, 54 insertions(+), 50 deletions(-) diff --git a/trytond/osv/fields.py b/trytond/osv/fields.py index d77bd3406d..189118f294 100644 --- a/trytond/osv/fields.py +++ b/trytond/osv/fields.py @@ -13,13 +13,13 @@ Relationals fields - Values: (0, 0, { fields }) create - (1, ID, { fields }) modification - (2, ID) remove (delete) - (3, ID) unlink one (target id or target of relation) - (4, ID) link - (5, ID) unlink all (only valid for one2many) - (6, ?, ids) set a list of links + Values: ('create', { fields }) create + ('write', ID, { fields }) modification + ('unlink', ID) remove (delete) + ('remove', ID) unlink one (target id or target of relation) + ('add', ID) link + ('remove_all') unlink all + ('set', ids) set a list of links """ import psycopg2 @@ -404,42 +404,44 @@ class One2Many(Column): _table = obj.pool.get(self._obj)._table obj = obj.pool.get(self._obj) for act in values: - if act[0] == 0: - act[2][self._field] = obj_id - obj.create(cursor, user, act[2], context=context) - elif act[0] == 1: + if act[0] == 'create': + act[1][self._field] = obj_id + obj.create(cursor, user, act[1], context=context) + elif act[0] == 'write': obj.write(cursor, user, [act[1]] , act[2], context=context) - elif act[0] == 2: + elif act[0] == 'unlink': obj.unlink(cursor, user, [act[1]], context=context) - elif act[0] == 3: + elif act[0] == 'remove': cursor.execute('UPDATE "' + _table + '" ' \ 'SET "' + self._field + '" = NULL ' \ 'WHERE id = %s', (act[1],)) - elif act[0] == 4: + elif act[0] == 'add': cursor.execute('UPDATE "' + _table + '" ' \ 'SET "' + self._field + '" = %s ' \ 'WHERE id = %s', (obj_id, act[1])) - elif act[0] == 5: + elif act[0] == 'remove_all': cursor.execute('UPDATE "' + _table + '" ' \ 'SET "' + self._field + '" = NULL ' \ 'WHERE "' + self._field + '" = %s', (obj_id,)) - elif act[0] == 6: - if not act[2]: + elif act[0] == 'set': + if not act[1]: ids2 = [0] else: - ids2 = act[2] + ids2 = act[1] cursor.execute('UPDATE "' + _table + '" ' \ 'SET "' + self._field + '" = NULL ' \ 'WHERE "' + self._field + '" = %s ' \ 'AND id not IN (' + \ ','.join([str(x) for x in ids2]) + ')', (obj_id,)) - if act[2]: + if act[1]: cursor.execute('UPDATE "' + _table + '" ' \ 'SET "' + self._field + '" = %s ' \ 'WHERE id IN (' + \ - ','.join([str(x) for x in act[2]]) + ')', + ','.join([str(x) for x in act[1]]) + ')', (obj_id,)) + else: + raise Exception('Bad arguments') one2many = One2Many @@ -494,20 +496,20 @@ class Many2Many(Column): return obj = obj.pool.get(self._obj) for act in values: - if act[0] == 0: - idnew = obj.create(cursor, user, act[2]) + if act[0] == 'create': + idnew = obj.create(cursor, user, act[1]) cursor.execute('INSERT INTO "' + self._rel + '" ' \ '(' + self._id1 + ', ' + self._id2 + ') ' \ 'VALUES (%s, %s)', (obj_id, idnew)) - elif act[0] == 1: + elif act[0] == 'write': obj.write(cursor, user, [act[1]] , act[2], context=context) - elif act[0] == 2: + elif act[0] == 'unlink': obj.unlink(cursor, user, [act[1]], context=context) - elif act[0] == 3: + elif act[0] == 'remove': cursor.execute('DELETE FROM "' + self._rel + '" ' \ 'WHERE "' + self._id1 + '" = %s ' \ 'AND "'+ self._id2 + '" = %s', (obj_id, act[1])) - elif act[0] == 4: + elif act[0] == 'add': cursor.execute('SELECT * FROM "' + self._rel + '" ' \ 'WHERE ' + self._id1 + ' = %s ' \ 'AND ' + self._id2 + ' = %s ', @@ -517,11 +519,11 @@ class Many2Many(Column): cursor.execute('INSERT INTO "' + self._rel + '" ' \ '(' + self._id1 + ', ' + self._id2 + ') ' \ 'VALUES (%s, %s)', (obj_id, act[1])) - elif act[0] == 5: + elif act[0] == 'remove_all': cursor.execute('UPDATE "' + self._rel + '" ' \ 'SET "' + self._id2 + '" = NULL ' \ 'WHERE "' + self._id2 + '" = %s', (obj_id,)) - elif act[0] == 6: + elif act[0] == 'set': domain1, domain2 = obj.pool.get('ir.rule').domain_get(cursor, user, obj._name) if domain1: @@ -536,10 +538,12 @@ class Many2Many(Column): obj._table + '.id ' + domain1 + ')', [obj_id, obj_id] + domain2) - for act_nbr in act[2]: + for act_nbr in act[1]: cursor.execute('INSERT INTO "' + self._rel + '" ' \ '(' + self._id1 + ', ' + self._id2 + ') ' \ 'VALUES (%s, %s)', (obj_id, act_nbr)) + else: + raise Exception('Bad arguments') many2many = Many2Many diff --git a/trytond/osv/orm.py b/trytond/osv/orm.py index d3b5d4f194..198af99a35 100644 --- a/trytond/osv/orm.py +++ b/trytond/osv/orm.py @@ -890,7 +890,7 @@ class ORM(object): if res_id2: res_id.append(res_id2) if len(res_id): - res_id = [(6, 0, res_id)] + res_id = [('set', res_id)] else: if '.' in line[i]: module, xml_id = line[i].rsplit('.', 1) @@ -965,7 +965,7 @@ class ORM(object): else: res.append(res3) if len(res): - res = [(6, 0, res)] + res = [('set', res)] else: res = line[i] or False row[field[len(prefix)]] = res @@ -985,7 +985,7 @@ class ORM(object): warning = warning + warning2 reduce(lambda x, y: x and y, newrow) row[field] = (reduce(lambda x, y: x or y, newrow.values()) and \ - [(0,0,newrow)]) or [] + [('create', newrow)]) or [] i = max2 while (position+i)Admin - + res.group diff --git a/trytond/res/ir.xml b/trytond/res/ir.xml index e1a5effbbf..11ffaf0baa 100644 --- a/trytond/res/ir.xml +++ b/trytond/res/ir.xml @@ -394,28 +394,28 @@ - + - + - + - + - + - + - + - + ir.module.module diff --git a/trytond/res/user.xml b/trytond/res/user.xml index b73f017ab7..d5842f1c42 100644 --- a/trytond/res/user.xml +++ b/trytond/res/user.xml @@ -8,7 +8,7 @@ Administrator - + res.user diff --git a/trytond/tools/convert.py b/trytond/tools/convert.py index 72171f4309..cab956d423 100644 --- a/trytond/tools/convert.py +++ b/trytond/tools/convert.py @@ -106,10 +106,10 @@ class MenuitemTagHandler: for group in g_names: if group.startswith('-'): group_id = self.mh.get_id(group[1:]) - groups_value.append((3, group_id)) + groups_value.append(('remove', group_id)) else: group_id = self.mh.get_id(group) - groups_value.append((4, group_id)) + groups_value.append(('add', group_id)) values['groups'] = groups_value if not values.get('name'): @@ -199,7 +199,7 @@ class RecordTagHandler: if not answer: return if self.model._columns[field_name]._type == 'many2many': - self.values[field_name] = [(6, 0, [x['id'] for x in answer])] + self.values[field_name] = [('set', [x['id'] for x in answer])] elif self.model._columns[field_name]._type == 'many2one': self.values[field_name] = answer[0]['id']