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.

248 lines
11 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Daniel Reis
  5. # 2011
  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. import sys
  22. from datetime import datetime
  23. from openerp.osv import orm, fields
  24. import logging
  25. _logger = logging.getLogger(__name__)
  26. _loglvl = _logger.getEffectiveLevel()
  27. SEP = '|'
  28. class import_odbc_dbtable(orm.Model):
  29. _name = "import.odbc.dbtable"
  30. _description = 'Import Table Data'
  31. _order = 'exec_order'
  32. _columns = {
  33. 'name': fields.char('Datasource name', required=True, size=64),
  34. 'enabled': fields.boolean('Execution enabled'),
  35. 'dbsource_id': fields.many2one('base.external.dbsource',
  36. 'Database source', required=True),
  37. 'sql_source': fields.text('SQL', required=True,
  38. help='Column names must be valid \
  39. "import_data" columns.'),
  40. 'model_target': fields.many2one('ir.model', 'Target object'),
  41. 'noupdate': fields.boolean('No updates',
  42. help="Only create new records;\
  43. disable updates to existing records."),
  44. 'exec_order': fields.integer('Execution order',
  45. help="Defines the order to perform \
  46. the import"),
  47. 'last_sync': fields.datetime('Last sync date',
  48. help="Datetime for the last succesfull \
  49. sync. \nLater changes on the source may \
  50. not be replicated on the destination"),
  51. 'start_run': fields.datetime('Time started', readonly=True),
  52. 'last_run': fields.datetime('Time ended', readonly=True),
  53. 'last_record_count': fields.integer('Last record count',
  54. readonly=True),
  55. 'last_error_count': fields.integer('Last error count', readonly=True),
  56. 'last_warn_count': fields.integer('Last warning count', readonly=True),
  57. 'last_log': fields.text('Last run log', readonly=True),
  58. 'ignore_rel_errors': fields.boolean('Ignore relationship errors',
  59. help="On error try to reimport \
  60. rows ignoring relationships."),
  61. 'raise_import_errors': fields.boolean('Raise import errors',
  62. help="Import errors not \
  63. handled, intended for \
  64. debugging purposes. \nAlso \
  65. forces debug messages to be \
  66. written to the server log."),
  67. }
  68. _defaults = {
  69. 'enabled': True,
  70. 'exec_order': 10,
  71. }
  72. def _import_data(self, cr, uid, flds, data, model_obj, table_obj, log):
  73. """Import data and returns error msg or empty string"""
  74. def find_m2o(field_list):
  75. """"Find index of first column with a one2many field"""
  76. for i, x in enumerate(field_list):
  77. if len(x) > 3 and x[-3:] == ':id' or x[-3:] == '/id':
  78. return i
  79. return -1
  80. def append_to_log(log, level, obj_id='', msg='', rel_id=''):
  81. if '_id_' in obj_id:
  82. obj_id = ('.'.join(obj_id.split('_')[:-2]) + ': ' +
  83. obj_id.split('_')[-1])
  84. if ': .' in msg and not rel_id:
  85. rel_id = msg[msg.find(': .')+3:]
  86. if '_id_' in rel_id:
  87. rel_id = ('.'.join(rel_id.split('_')[:-2]) +
  88. ': ' + rel_id.split('_')[-1])
  89. msg = msg[:msg.find(': .')]
  90. log['last_log'].append('%s|%s\t|%s\t|%s' % (level.ljust(5),
  91. obj_id, rel_id, msg))
  92. _logger.debug(data)
  93. cols = list(flds) # copy to avoid side effects
  94. errmsg = str()
  95. if table_obj.raise_import_errors:
  96. model_obj.import_data(cr, uid, cols, [data],
  97. noupdate=table_obj.noupdate)
  98. else:
  99. try:
  100. model_obj.import_data(cr, uid, cols, [data],
  101. noupdate=table_obj.noupdate)
  102. except:
  103. errmsg = str(sys.exc_info()[1])
  104. if errmsg and not table_obj.ignore_rel_errors:
  105. # Fail
  106. append_to_log(log, 'ERROR', data, errmsg)
  107. log['last_error_count'] += 1
  108. return False
  109. if errmsg and table_obj.ignore_rel_errors:
  110. # Warn and retry ignoring many2one fields...
  111. append_to_log(log, 'WARN', data, errmsg)
  112. log['last_warn_count'] += 1
  113. # Try ignoring each many2one
  114. # (tip: in the SQL sentence select more problematic FKs first)
  115. i = find_m2o(cols)
  116. if i >= 0:
  117. # Try again without the [i] column
  118. del cols[i]
  119. del data[i]
  120. self._import_data(cr, uid, cols, data, model_obj,
  121. table_obj, log)
  122. else:
  123. # Fail
  124. append_to_log(log, 'ERROR', data,
  125. 'Removed all m2o keys and still fails.')
  126. log['last_error_count'] += 1
  127. return False
  128. return True
  129. def import_run(self, cr, uid, ids=None, context=None):
  130. db_model = self.pool.get('base.external.dbsource')
  131. actions = self.read(cr, uid, ids, ['id', 'exec_order'])
  132. actions.sort(key=lambda x: (x['exec_order'], x['id']))
  133. # Consider each dbtable:
  134. for action_ref in actions:
  135. obj = self.browse(cr, uid, action_ref['id'])
  136. if not obj.enabled:
  137. continue # skip
  138. _logger.setLevel(obj.raise_import_errors and
  139. logging.DEBUG or _loglvl)
  140. _logger.debug('Importing %s...' % obj.name)
  141. # now() microseconds are stripped
  142. # to avoid problem with SQL smalldate
  143. # TODO: convert UTC Now to local timezone
  144. # http://stackoverflow.com/questions/4770297/python-convert-utc-datetime-string-to-local-datetime
  145. model_name = obj.model_target.model
  146. model_obj = self.pool.get(model_name)
  147. xml_prefix = model_name.replace('.', '_') + "_id_"
  148. log = {'start_run': datetime.now().replace(microsecond=0),
  149. 'last_run': None,
  150. 'last_record_count': 0,
  151. 'last_error_count': 0,
  152. 'last_warn_count': 0,
  153. 'last_log': list()}
  154. self.write(cr, uid, [obj.id], log)
  155. # Prepare SQL sentence; replace "%s" with the last_sync date
  156. if obj.last_sync:
  157. sync = datetime.strptime(obj.last_sync, "%Y-%m-%d %H:%M:%S")
  158. else:
  159. sync = datetime.datetime(1900, 1, 1, 0, 0, 0)
  160. params = {'sync': sync}
  161. res = db_model.execute(cr, uid, [obj.dbsource_id.id],
  162. obj.sql_source, params, metadata=True)
  163. # Exclude columns titled "None"; add (xml_)"id" column
  164. cidx = ([i for i, x in enumerate(res['cols'])
  165. if x.upper() != 'NONE'])
  166. cols = ([x for i, x in enumerate(res['cols'])
  167. if x.upper() != 'NONE'] + ['id'])
  168. # Import each row:
  169. for row in res['rows']:
  170. # Build data row;
  171. # import only columns present in the "cols" list
  172. data = list()
  173. for i in cidx:
  174. # TODO: Handle imported datetimes properly
  175. # convert from localtime to UTC!
  176. v = row[i]
  177. if isinstance(v, str):
  178. v = v.strip()
  179. data.append(v)
  180. data.append(xml_prefix + str(row[0]).strip())
  181. # Import the row; on error, write line to the log
  182. log['last_record_count'] += 1
  183. self._import_data(cr, uid, cols, data, model_obj, obj, log)
  184. if log['last_record_count'] % 500 == 0:
  185. _logger.info('...%s rows processed...'
  186. % (log['last_record_count']))
  187. # Finished importing all rows
  188. # If no errors, write new sync date
  189. if not (log['last_error_count'] or log['last_warn_count']):
  190. log['last_sync'] = log['start_run']
  191. level = logging.DEBUG
  192. if log['last_warn_count']:
  193. level = logging.WARN
  194. if log['last_error_count']:
  195. level = logging.ERROR
  196. _logger.log(level,
  197. 'Imported %s , %d rows, %d errors, %d warnings.' %
  198. (model_name, log['last_record_count'],
  199. log['last_error_count'],
  200. log['last_warn_count']))
  201. # Write run log, either if the table import is active or inactive
  202. if log['last_log']:
  203. log['last_log'].insert(0,
  204. 'LEVEL|== Line == |== Relationship \
  205. ==|== Message ==')
  206. log.update({'last_log': '\n'.join(log['last_log'])})
  207. log.update({'last_run': datetime.now().replace(microsecond=0)})
  208. self.write(cr, uid, [obj.id], log)
  209. # Finished
  210. _logger.debug('Import job FINISHED.')
  211. return True
  212. def import_schedule(self, cr, uid, ids, context=None):
  213. cron_obj = self.pool.get('ir.cron')
  214. new_create_id = cron_obj.create(cr, uid, {
  215. 'name': 'Import ODBC tables',
  216. 'interval_type': 'hours',
  217. 'interval_number': 1,
  218. 'numbercall': -1,
  219. 'model': 'import.odbc.dbtable',
  220. 'function': 'import_run',
  221. 'doall': False,
  222. 'active': True
  223. })
  224. return {
  225. 'name': 'Import ODBC tables',
  226. 'view_type': 'form',
  227. 'view_mode': 'form,tree',
  228. 'res_model': 'ir.cron',
  229. 'res_id': new_create_id,
  230. 'type': 'ir.actions.act_window',
  231. }