~therp-nl/openobject-server/ronald@therp.nl_fix_orm_lp1009014-trunk-revision2

« back to all changes in this revision

Viewing changes to openerp/tests/addons/test_impex/models.py

  • Committer: Ronald Portier
  • Date: 2012-10-23 22:36:35 UTC
  • mfrom: (4185.1.318 trunk)
  • Revision ID: ronald@therp.nl-20121023223635-jxph07k7y1qf6bbx
[MERGE] Merge latest code from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.required', fields.char('unknown', size=None, required=True)),
 
21
    ('string', fields.char('unknown', size=None)),
 
22
    ('date', fields.date()),
 
23
    ('datetime', fields.datetime()),
 
24
    ('text', fields.text()),
 
25
    ('selection', fields.selection([(1, "Foo"), (2, "Bar"), (3, "Qux"), (4, '')])),
 
26
    ('selection.function', fields.selection(selection_fn)),
 
27
    # just relate to an integer
 
28
    ('many2one', fields.many2one('export.integer')),
 
29
    ('one2many', fields.one2many('export.one2many.child', 'parent_id')),
 
30
    ('many2many', fields.many2many('export.many2many.other')),
 
31
    ('function', fields.function(function_fn, fnct_inv=function_fn_write, type="integer")),
 
32
    # related: specialization of fields.function, should work the same way
 
33
    # TODO: reference
 
34
]
 
35
for name, field in models:
 
36
    attrs = {
 
37
        '_name': 'export.%s' % name,
 
38
        '_columns': {
 
39
            'const': fields.integer(),
 
40
            'value': field
 
41
        },
 
42
        '_defaults': {'const': 4},
 
43
        'name_get': (lambda self, cr, uid, ids, context=None:
 
44
            [(record.id, "%s:%s" % (self._name, record.value))
 
45
             for record in self.browse(cr, uid, ids, context=context)]),
 
46
        'name_search': (lambda self, cr, uid, name, operator, context=None:
 
47
                self.name_get(cr, uid,
 
48
                    self.search(cr, uid, [['value', operator, int(name.split(':')[1])]])
 
49
                    , context=context)
 
50
                if isinstance(name, basestring) and name.split(':')[0] == self._name
 
51
                else [])
 
52
    }
 
53
    NewModel = type(
 
54
        'Export%s' % ''.join(section.capitalize() for section in name.split('.')),
 
55
        (orm.Model,),
 
56
        attrs)
 
57
 
 
58
class One2ManyChild(orm.Model):
 
59
    _name = 'export.one2many.child'
 
60
    # FIXME: orm.py:1161, fix to name_get on m2o field
 
61
    _rec_name = 'value'
 
62
 
 
63
    _columns = {
 
64
        'parent_id': fields.many2one('export.one2many'),
 
65
        'str': fields.char('unknown', size=None),
 
66
        'value': fields.integer()
 
67
    }
 
68
    def name_get(self, cr, uid, ids, context=None):
 
69
        return [(record.id, "%s:%s" % (self._name, record.value))
 
70
            for record in self.browse(cr, uid, ids, context=context)]
 
71
    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
 
72
        return (self.name_get(cr, user,
 
73
                self.search(cr, user, [['value', operator, int(name.split(':')[1])]])
 
74
                , context=context)
 
75
            if isinstance(name, basestring) and name.split(':')[0] == self._name
 
76
            else [])
 
77
 
 
78
class One2ManyMultiple(orm.Model):
 
79
    _name = 'export.one2many.multiple'
 
80
 
 
81
    _columns = {
 
82
        'const': fields.integer(),
 
83
        'child1': fields.one2many('export.one2many.child.1', 'parent_id'),
 
84
        'child2': fields.one2many('export.one2many.child.2', 'parent_id'),
 
85
    }
 
86
    _defaults = { 'const': 36 }
 
87
 
 
88
class One2ManyChildMultiple(orm.Model):
 
89
    _name = 'export.one2many.multiple.child'
 
90
    # FIXME: orm.py:1161, fix to name_get on m2o field
 
91
    _rec_name = 'value'
 
92
 
 
93
    _columns = {
 
94
        'parent_id': fields.many2one('export.one2many.multiple'),
 
95
        'str': fields.char('unknown', size=None),
 
96
        'value': fields.integer()
 
97
    }
 
98
    def name_get(self, cr, uid, ids, context=None):
 
99
        return [(record.id, "%s:%s" % (self._name, record.value))
 
100
            for record in self.browse(cr, uid, ids, context=context)]
 
101
class One2ManyChild1(orm.Model):
 
102
    _name = 'export.one2many.child.1'
 
103
    _inherit = 'export.one2many.multiple.child'
 
104
class One2ManyChild2(orm.Model):
 
105
    _name = 'export.one2many.child.2'
 
106
    _inherit = 'export.one2many.multiple.child'
 
107
 
 
108
class Many2ManyChild(orm.Model):
 
109
    _name = 'export.many2many.other'
 
110
    # FIXME: orm.py:1161, fix to name_get on m2o field
 
111
    _rec_name = 'value'
 
112
 
 
113
    _columns = {
 
114
        'str': fields.char('unknown', size=None),
 
115
        'value': fields.integer()
 
116
    }
 
117
    def name_get(self, cr, uid, ids, context=None):
 
118
        return [(record.id, "%s:%s" % (self._name, record.value))
 
119
            for record in self.browse(cr, uid, ids, context=context)]
 
120
    def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
 
121
        return (self.name_get(cr, user,
 
122
                    self.search(cr, user, [['value', operator, int(name.split(':')[1])]])
 
123
                    , context=context)
 
124
                if isinstance(name, basestring) and name.split(':')[0] == self._name
 
125
                else [])
 
126
 
 
127
class SelectionWithDefault(orm.Model):
 
128
    _name = 'export.selection.withdefault'
 
129
 
 
130
    _columns = {
 
131
        'const': fields.integer(),
 
132
        'value': fields.selection([(1, "Foo"), (2, "Bar")]),
 
133
    }
 
134
    _defaults = {
 
135
        'const': 4,
 
136
        'value': 2,
 
137
    }