1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
25 super class for all mapper class
26 They are call before import data
27 to transform the mapping into real value that we
30 the call function receive a dictionary with external data
31 'external_field' : value
33 def __call__(self, external_values):
34 raise NotImplementedError()
36 class dbmapper(mapper):
38 Super class for mapper that need to access to
39 data base or any function of the import_framework
41 self.parent contains a reference to the instance of
44 def set_parent(self, parent):
50 Use : contact('field_name1', 'field_name2', delimiter='_')
51 concat value of fields using the delimiter, delimiter is optional
52 and by default is a space
54 def __init__(self, *arg, **delimiter):
56 self.delimiter = delimiter and delimiter.get('delimiter', ' ') or ' '
58 def __call__(self, external_values):
59 return self.delimiter.join(map(lambda x : tools.ustr(external_values.get(x,'')), self.arg))
61 class ppconcat(mapper):
63 Use : contact('field_name1', 'field_name2', delimiter='_')
64 concat external field name and value of fields using the delimiter,
65 delimiter is optional and by default is a two line feeds
68 def __init__(self, *arg, **delimiter):
70 self.delimiter = delimiter and delimiter.get('delimiter', ' ') or '\n\n'
72 def __call__(self, external_values):
73 return self.delimiter.join(map(lambda x : x + ": " + tools.ustr(external_values.get(x,'')), self.arg))
80 def __init__(self, val):
83 def __call__(self, external_values):
88 Use : value(external_field_name)
89 Return the value of the external field name
90 this is equivalent to the a single string
92 usefull for call if you want your call get the value
93 and don't care about the name of the field
94 call(self.method, value('field1'))
96 def __init__(self, val, default='', fallback=False):
98 self.default = default
99 self.fallback = fallback
101 def __call__(self, external_values):
102 val = external_values.get(self.val, self.default)
103 if self.fallback and (not val or val == self.default):
104 val = external_values.get(self.fallback, self.default)
108 class map_val(mapper):
110 Use : map_val(external_field, val_mapping)
111 where val_mapping is a dictionary
112 with external_val : openerp_val
114 usefull for selection field like state
117 def __init__(self, val, map, default='draft'):
118 self.val = value(val)
120 self.default = default
122 def __call__(self, external_values):
123 return self.map.get(self.val(external_values), self.default)
127 Use : ref(table_name, external_id)
128 return the xml_id of the ressource
130 to associate an already imported object with the current object
132 def __init__(self, table, field_name):
134 self.field_name = field_name
136 def __call__(self, external_values):
137 return self.parent.xml_id_exist(self.table, external_values.get(self.field_name))
139 class refbyname(dbmapper):
141 Use : refbyname(table_name, external_name, res.model)
142 same as ref but use the name of the ressource to find it
144 def __init__(self, table, field_name, model):
146 self.field_name = field_name
149 def __call__(self, external_values):
150 v = external_values.get(self.field_name, '')
151 return self.parent.name_exist(self.table, v , self.model)
155 Use : call(function, arg1, arg2)
156 to call the function with external val follow by the arg specified
158 def __init__(self, fun, *arg):
162 def __call__(self, external_values):
165 if isinstance(arg, mapper):
166 args.append(arg(external_values))
169 return self.fun(external_values, *args)
171 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: