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.

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