You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  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.
  11. #
  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.
  16. #
  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/>.
  19. #
  20. ##############################################################################
  21. from openerp import tools
  22. class mapper(object):
  23. """
  24. super class for all mapper class
  25. They are call before import data
  26. to transform the mapping into real value that we
  27. will import
  28. the call function receive a dictionary with external data
  29. 'external_field' : value
  30. """
  31. def __call__(self, external_values):
  32. raise NotImplementedError()
  33. def get_fields(self):
  34. return []
  35. class dbmapper(mapper):
  36. """
  37. Super class for mapper that need to access to
  38. data base or any function of the import_framework
  39. self.parent contains a reference to the instance of
  40. the import framework
  41. """
  42. def set_parent(self, parent):
  43. self.parent = parent
  44. class concat(mapper):
  45. """
  46. Use : contact('field_name1', 'field_name2', delimiter='_')
  47. concat value of fields using the delimiter, delimiter is optional
  48. and by default is a space
  49. """
  50. def __init__(self, *arg, **delimiter):
  51. self.arg = arg
  52. self.delimiter = delimiter and delimiter.get('delimiter', ' ') or ' '
  53. def __call__(self, external_values):
  54. return self.delimiter.join(map(lambda x : tools.ustr(external_values.get(x,'')), self.arg))
  55. def get_fields(self):
  56. return self.arg
  57. class ppconcat(concat):
  58. """
  59. Use : contact('field_name1', 'field_name2', delimiter='_')
  60. concat external field name and value of fields using the delimiter,
  61. delimiter is optional and by default is a two line feeds
  62. """
  63. def __init__(self, *arg, **delimiter):
  64. self.arg = arg
  65. self.delimiter = delimiter and delimiter.get('delimiter', ' ') or '\n\n'
  66. def __call__(self, external_values):
  67. return self.delimiter.join(map(lambda x : x + ": " + tools.ustr(external_values.get(x,'')), self.arg))
  68. class const(mapper):
  69. """
  70. Use : const(arg)
  71. return always arg
  72. """
  73. def __init__(self, val):
  74. self.val = val
  75. def __call__(self, external_values):
  76. return self.val
  77. class value(mapper):
  78. """
  79. Use : value(external_field_name)
  80. Return the value of the external field name
  81. this is equivalent to the a single string
  82. usefull for call if you want your call get the value
  83. and don't care about the name of the field
  84. call(self.method, value('field1'))
  85. """
  86. def __init__(self, val, default='', fallback=False):
  87. self.val = val
  88. self.default = default
  89. self.fallback = fallback
  90. def __call__(self, external_values):
  91. val = external_values.get(self.val, self.default)
  92. if self.fallback and (not val or val == self.default):
  93. val = external_values.get(self.fallback, self.default)
  94. return val
  95. def get_fields(self):
  96. return [self.val]
  97. class map_val(mapper):
  98. """
  99. Use : map_val(external_field, val_mapping)
  100. where val_mapping is a dictionary
  101. with external_val : openerp_val
  102. usefull for selection field like state
  103. to map value
  104. """
  105. def __init__(self, val, map, default='draft'):
  106. self.val = value(val)
  107. self.map = map
  108. self.default = default
  109. def __call__(self, external_values):
  110. return self.map.get(self.val(external_values), self.default)
  111. def get_fields(self):
  112. return self.val.get_fields()
  113. class map_val_default(mapper):
  114. """
  115. Use : map_val(external_field, val_mapping)
  116. where val_mapping is a dictionary
  117. with external_val : openerp_val
  118. usefull for selection field like state
  119. to map value
  120. """
  121. def __init__(self, val, map):
  122. self.val = value(val)
  123. self.map = map
  124. def __call__(self, external_values):
  125. value = self.map.get(self.val(external_values), self.val(external_values))
  126. return value
  127. def get_fields(self):
  128. return self.val.get_fields()
  129. class ref(dbmapper):
  130. """
  131. Use : ref(table_name, external_id)
  132. return the xml_id of the ressource
  133. to associate an already imported object with the current object
  134. """
  135. def __init__(self, table, field_name):
  136. self.table = table
  137. self.field_name = field_name
  138. def __call__(self, external_values):
  139. return self.parent.xml_id_exist(self.table, external_values.get(self.field_name))
  140. def get_fields(self):
  141. return self.field_name
  142. class refbyname(dbmapper):
  143. """
  144. Use : refbyname(table_name, external_name, res.model)
  145. same as ref but use the name of the ressource to find it
  146. """
  147. def __init__(self, table, field_name, model):
  148. self.table = table
  149. self.field_name = field_name
  150. self.model = model
  151. def __call__(self, external_values):
  152. v = external_values.get(self.field_name, '')
  153. return self.parent.name_exist(self.table, v , self.model)
  154. def get_fields(self):
  155. return self.field_name
  156. class call(mapper):
  157. """
  158. Use : call(function, arg1, arg2)
  159. to call the function with external val follow by the arg specified
  160. """
  161. def __init__(self, fun, *arg):
  162. self.fun = fun
  163. self.arg = arg
  164. def __call__(self, external_values):
  165. args = []
  166. for arg in self.arg:
  167. if isinstance(arg, mapper):
  168. args.append(arg(external_values))
  169. else:
  170. args.append(arg)
  171. return self.fun(external_values, *args)
  172. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
  173. def val(field_name):
  174. def val_fun(data_line):
  175. return data_line[field_name]
  176. return val_fun
  177. def const(const):
  178. def val_fun(data_line):
  179. return const