[IMP]Remove serverwarning
[odoo/odoo.git] / openerp / tests / addons / test_impex / models.py
1 # -*- coding: utf-8 -*-
2 from openerp.osv import orm, fields
3
4 def selection_fn(obj, cr, uid, context=None):
5     return list(enumerate(["Corge", "Grault", "Wheee", "Moog"]))
6
7 def function_fn(model, cr, uid, ids, field_name, arg, context):
8     return dict((id, 3) for id in ids)
9 def function_fn_write(model, cr, uid, id, field_name, field_value, fnct_inv_arg, context):
10     """ just so CreatorCase.export can be used
11     """
12     pass
13
14 models = [
15     ('boolean', fields.boolean()),
16     ('integer', fields.integer()),
17     ('float', fields.float()),
18     ('decimal', fields.float(digits=(16, 3))),
19     ('string.bounded', fields.char('unknown', size=16)),
20     ('string', fields.char('unknown', size=None)),
21     ('date', fields.date()),
22     ('datetime', fields.datetime()),
23     ('text', fields.text()),
24     ('selection', fields.selection([(1, "Foo"), (2, "Bar"), (3, "Qux")])),
25     ('selection.function', fields.selection(selection_fn)),
26     # just relate to an integer
27     ('many2one', fields.many2one('export.integer')),
28     ('one2many', fields.one2many('export.one2many.child', 'parent_id')),
29     ('many2many', fields.many2many('export.many2many.other')),
30     ('function', fields.function(function_fn, fnct_inv=function_fn_write, type="integer")),
31     # related: specialization of fields.function, should work the same way
32     # TODO: reference
33 ]
34 for name, field in models:
35     attrs = {
36         '_name': 'export.%s' % name,
37         '_columns': {
38             'const': fields.integer(),
39             'value': field
40         },
41         '_defaults': {'const': 4},
42         'name_get': (lambda self, cr, uid, ids, context=None:
43             [(record.id, "%s:%s" % (self._name, record.value))
44              for record in self.browse(cr, uid, ids, context=context)]),
45         'name_search': (lambda self, cr, uid, name, operator, context=None:
46                 self.name_get(cr, uid,
47                     self.search(cr, uid, [['value', operator, int(name.split(':')[1])]])
48                     , context=context)
49                 if isinstance(name, basestring) and name.split(':')[0] == self._name
50                 else [])
51     }
52     NewModel = type(
53         'Export%s' % ''.join(section.capitalize() for section in name.split('.')),
54         (orm.Model,),
55         attrs)
56
57 class One2ManyChild(orm.Model):
58     _name = 'export.one2many.child'
59     # FIXME: orm.py:1161, fix to name_get on m2o field
60     _rec_name = 'value'
61
62     _columns = {
63         'parent_id': fields.many2one('export.one2many'),
64         'str': fields.char('unknown', size=None),
65         'value': fields.integer()
66     }
67     def name_get(self, cr, uid, ids, context=None):
68         return [(record.id, "%s:%s" % (self._name, record.value))
69             for record in self.browse(cr, uid, ids, context=context)]
70
71 class One2ManyMultiple(orm.Model):
72     _name = 'export.one2many.multiple'
73
74     _columns = {
75         'const': fields.integer(),
76         'child1': fields.one2many('export.one2many.child.1', 'parent_id'),
77         'child2': fields.one2many('export.one2many.child.2', 'parent_id'),
78     }
79     _defaults = { 'const': 36 }
80
81 class One2ManyChildMultiple(orm.Model):
82     _name = 'export.one2many.multiple.child'
83     # FIXME: orm.py:1161, fix to name_get on m2o field
84     _rec_name = 'value'
85
86     _columns = {
87         'parent_id': fields.many2one('export.one2many.multiple'),
88         'str': fields.char('unknown', size=None),
89         'value': fields.integer()
90     }
91     def name_get(self, cr, uid, ids, context=None):
92         return [(record.id, "%s:%s" % (self._name, record.value))
93             for record in self.browse(cr, uid, ids, context=context)]
94 class One2ManyChild1(orm.Model):
95     _name = 'export.one2many.child.1'
96     _inherit = 'export.one2many.multiple.child'
97 class One2ManyChild2(orm.Model):
98     _name = 'export.one2many.child.2'
99     _inherit = 'export.one2many.multiple.child'
100
101 class Many2ManyChild(orm.Model):
102     _name = 'export.many2many.other'
103     # FIXME: orm.py:1161, fix to name_get on m2o field
104     _rec_name = 'value'
105
106     _columns = {
107         'str': fields.char('unknown', size=None),
108         'value': fields.integer()
109     }
110     def name_get(self, cr, uid, ids, context=None):
111         return [(record.id, "%s:%s" % (self._name, record.value))
112             for record in self.browse(cr, uid, ids, context=context)]
113     def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
114         return (self.name_get(cr, user,
115                     self.search(cr, user, [['value', operator, int(name.split(':')[1])]])
116                     , context=context)
117                 if isinstance(name, basestring) and name.split(':')[0] == self._name
118                 else [])