def migrate_modules(cursor): modules_in_dir = get_module_list() modules_to_migrate = {} for module_dir in modules_in_dir: try: with tools.file_open(OPJ(module_dir, '__migrated_modules')) as f: for line in f.readlines(): line = line.replace(' ', '').strip('\n') if not line: continue action, old_module = line.split(':') modules_to_migrate[old_module] = (action, module_dir) except IOError: continue cursor.execute(*ir_module.select(ir_module.name)) for module_in_db, in cursor.fetchall(): if (module_in_db in modules_in_dir or module_in_db in modules_to_migrate): continue else: modules_to_migrate[module_in_db] = ('to_drop', None) if (not AUTO_UNINSTALL): dropped = False for module in modules_to_migrate: if modules_to_migrate[module][0] == 'to_drop': logger.critical( 'To uninstall %s you should set' ' COOG_AUTO_UNINSTALL environnement variable' % module) dropped = True if dropped: sys.exit(1) else: for module in modules_to_migrate: if modules_to_migrate[module][0] == 'to_drop': logger.warning('%s is about to be uninstalled' % (module)) def rename(cursor, table_name, old_name, new_name, var_name): table = Table(table_name) fields = None # If the view already exists in destination module if table_name == 'ir_model_data': fields = ['fs_id', 'model'] if table_name == 'ir_ui_view': fields = ['model', 'name'] if fields: query = ('DELETE from %(table)s where ' '(%(fields)s) in (' 'SELECT %(fields)s FROM %(table)s WHERE ' '"module" IN (\'%(old_name)s\', \'%(new_name)s\') ' 'GROUP BY %(fields)s ' 'HAVING COUNT("module") > 1) ' 'and "module" = \'%(old_name)s\';' % { 'table': table_name, 'old_name': old_name, 'new_name': new_name, 'fields': (', '.join('"' + f + '"' for f in fields)) }) cursor.execute(query) query = table.update([getattr(table, var_name)], [new_name], where=(getattr(table, var_name) == old_name)) cursor.execute(*query) def delete(cursor, table_name, old_name, var_name): table = Table(table_name) cursor.execute(*table.delete( where=(getattr(table, var_name) == old_name))) for old_name, (action, new_name) in modules_to_migrate.iteritems(): cursor.execute(*ir_module.select(Count(ir_module.id), where=ir_module.name == old_name)) count, = cursor.fetchone() if not count: continue if action == 'to_drop': logger.info('%s directory has been removed from filesystem,' ' deleting entries from database...' % old_name) else: logger.info('%s has been %s %s, updating database...' % (old_name, { 'to_rename': 'renamed into', 'to_merge': 'merged with' }[action], new_name)) if new_name: rename(cursor, 'ir_model', old_name, new_name, 'module') rename(cursor, 'ir_action_report', old_name, new_name, 'module') rename(cursor, 'ir_model_field', old_name, new_name, 'module') rename(cursor, 'ir_model_data', old_name, new_name, 'module') rename(cursor, 'ir_translation', old_name, new_name, 'module') rename(cursor, 'ir_translation', old_name, new_name, 'overriding_module') rename(cursor, 'ir_ui_icon', old_name, new_name, 'module') rename(cursor, 'ir_ui_view', old_name, new_name, 'module') if action == 'to_rename': rename(cursor, 'ir_module_dependency', old_name, new_name, 'name') rename(cursor, 'ir_module', old_name, new_name, 'name') elif action == 'to_merge': delete(cursor, 'ir_module_dependency', old_name, 'name') delete(cursor, 'ir_module', old_name, 'name') elif action == 'to_drop': delete(cursor, 'ir_model', old_name, 'module') delete(cursor, 'ir_action_report', old_name, 'module') delete(cursor, 'ir_model_field', old_name, 'module') delete(cursor, 'ir_model_data', old_name, 'module') delete(cursor, 'ir_translation', old_name, 'module') delete(cursor, 'ir_translation', old_name, 'overriding_module') delete(cursor, 'ir_ui_icon', old_name, 'module') delete(cursor, 'ir_ui_view', old_name, 'module') delete(cursor, 'ir_module_dependency', old_name, 'name') delete(cursor, 'ir_module', old_name, 'name')
def trigger_action(cls, records, trigger): """ Trigger the action define on trigger for the records """ pool = Pool() TriggerLog = pool.get('ir.trigger.log') Model = pool.get(trigger.model.model) ActionModel = pool.get(trigger.action_model.model) cursor = Transaction().cursor trigger_log = TriggerLog.__table__() ids = map(int, records) # Filter on limit_number if trigger.limit_number: new_ids = [] for sub_ids in grouped_slice(ids): sub_ids = list(sub_ids) red_sql = reduce_ids(trigger_log.record_id, sub_ids) cursor.execute( *trigger_log.select(trigger_log.record_id, Count(Literal(1)), where=red_sql & (trigger_log.trigger == trigger.id), group_by=trigger_log.record_id)) number = dict(cursor.fetchall()) for record_id in sub_ids: if record_id not in number: new_ids.append(record_id) continue if number[record_id] < trigger.limit_number: new_ids.append(record_id) ids = new_ids # Filter on minimum_time_delay if trigger.minimum_time_delay: new_ids = [] for sub_ids in grouped_slice(ids): sub_ids = list(sub_ids) red_sql = reduce_ids(trigger_log.record_id, sub_ids) cursor.execute( *trigger_log.select(trigger_log.record_id, Max(trigger_log.create_date), where=red_sql & (trigger_log.trigger == trigger.id), group_by=trigger_log.record_id)) delay = dict(cursor.fetchall()) for record_id in sub_ids: if record_id not in delay: new_ids.append(record_id) continue # SQLite return string for MAX if isinstance(delay[record_id], basestring): datepart, timepart = delay[record_id].split(" ") year, month, day = map(int, datepart.split("-")) timepart_full = timepart.split(".") hours, minutes, seconds = map( int, timepart_full[0].split(":")) if len(timepart_full) == 2: microseconds = int(timepart_full[1]) else: microseconds = 0 delay[record_id] = datetime.datetime( year, month, day, hours, minutes, seconds, microseconds) if (datetime.datetime.now() - delay[record_id] >= trigger.minimum_time_delay): new_ids.append(record_id) ids = new_ids records = Model.browse(ids) if records: getattr(ActionModel, trigger.action_function)(records, trigger) if trigger.limit_number or trigger.minimum_time_delay: to_create = [] for record in records: to_create.append({ 'trigger': trigger.id, 'record_id': record.id, }) if to_create: TriggerLog.create(to_create)
def __register__(cls, module_name): TableHandler = backend.get('TableHandler') super(ActionReport, cls).__register__(module_name) transaction = Transaction() cursor = transaction.connection.cursor() table = TableHandler(cls, module_name) action_report = cls.__table__() # Migration from 1.0 report_name_uniq has been removed table.drop_constraint('report_name_uniq') # Migration from 1.0 output_format (m2o) is now extension (selection) if table.column_exist('output_format'): outputformat = Table('ir_action_report_outputformat') cursor.execute(*action_report.join( outputformat, condition=action_report.output_format == outputformat.id) .select(action_report.id, where=outputformat.format == 'pdf')) ids = [x[0] for x in cursor.fetchall()] cls.write(cls.browse(ids), {'extension': 'pdf'}) ids = cls.search([('id', 'not in', ids)]) cls.write(cls.browse(ids), {'extension': 'odt'}) table.drop_column("output_format") TableHandler.dropTable('ir.action.report.outputformat', 'ir_action_report_outputformat') # Migrate from 2.0 remove required on extension table.not_null_action('extension', action='remove') cursor.execute(*action_report.update( [action_report.extension], [''], where=action_report.extension == 'odt')) # Migration from 2.0 report_content_data renamed into # report_content_custom to remove base64 encoding if (table.column_exist('report_content_data') and table.column_exist('report_content_custom')): limit = transaction.database.IN_MAX cursor.execute(*action_report.select( Count(action_report.id))) report_count, = cursor.fetchone() for offset in range(0, report_count, limit): cursor.execute(*action_report.select( action_report.id, action_report.report_content_data, order_by=action_report.id, limit=limit, offset=offset)) for report_id, report in cursor.fetchall(): if report: report = fields.Binary.cast( base64.decodestring(bytes(report))) cursor.execute(*action_report.update( [action_report.report_content_custom], [report], where=action_report.id == report_id)) table.drop_column('report_content_data') # Migration from 3.4 remove report_name_module_uniq constraint table.drop_constraint('report_name_module_uniq') # Migration from 4.4 replace plain extension by txt cursor.execute(*action_report.update( [action_report.extension], ['txt'], where=action_report.extension == 'plain'))