def table_query(cls): Opportunity = Pool().get('sale.opportunity') opportunity_history = Opportunity.__table_history__() columns = [ Min(Column(opportunity_history, '__id')).as_('id'), opportunity_history.id.as_('opportunity'), Min( Coalesce(opportunity_history.write_date, opportunity_history.create_date)).as_('date'), Coalesce(opportunity_history.write_uid, opportunity_history.create_uid).as_('user'), ] group_by = [ opportunity_history.id, Coalesce(opportunity_history.write_uid, opportunity_history.create_uid), ] for name, field in cls._fields.iteritems(): if name in ('id', 'opportunity', 'date', 'user'): continue if hasattr(field, 'set'): continue column = Column(opportunity_history, name) columns.append(column.as_(name)) group_by.append(column) return opportunity_history.select(*columns, group_by=group_by)
def table_query(cls): Opportunity = Pool().get('sale.opportunity') opportunity_history = Opportunity.__table_history__() columns = [ Min(Column(opportunity_history, '__id')).as_('id'), opportunity_history.id.as_('opportunity'), Min(Coalesce(opportunity_history.write_date, opportunity_history.create_date)).as_('date'), Coalesce(opportunity_history.write_uid, opportunity_history.create_uid).as_('user'), ] group_by = [ opportunity_history.id, Coalesce(opportunity_history.write_uid, opportunity_history.create_uid), ] for name, field in cls._fields.iteritems(): if name in ('id', 'opportunity', 'date', 'user'): continue if hasattr(field, 'set'): continue column = Column(opportunity_history, name) columns.append(column.as_(name)) group_by.append(column) return opportunity_history.select(*columns, group_by=group_by)
def merge_into(self, target): """Merge current record to target party. """ ModelField = Pool().get('ir.model.field') # Inactive party first self.active = False self.save() cursor = Transaction().connection.cursor() if self._history: # Update the party history first. # # The approach is to make the history of all merged records # also the history of the target record. party_history_table = self.__table_history__() cursor.execute( *party_history_table.update( columns=[party_history_table.id], values=[target.id], where=(party_history_table.id == self.id) ) ) party_fields = ModelField.search([ ('relation', '=', 'party.party'), ('ttype', '=', 'many2one'), ]) for field in party_fields: Model = Pool().get(field.model.model) if isinstance(getattr(Model, field.name), fields.Function): continue if not hasattr(Model, '__table__'): continue if Model.table_query(): continue sql_table = Model.__table__() # Update direct foreign key references cursor.execute(*sql_table.update( columns=[getattr(sql_table, field.name)], values=[target.id], where=(getattr(sql_table, field.name) == self.id) )) if Model._history: # If historization is enabled on the model # then the party value in the history should # now point to the target party id since the # history of the current party is already the history of # target party. history_table = Model.__table_history__() cursor.execute(*history_table.update( columns=[getattr(history_table, field.name)], values=[target.id], where=(getattr(history_table, field.name) == self.id) ))
def merge_into(self, target): """Merge current record to target party. """ ModelField = Pool().get('ir.model.field') # Inactive party first self.active = False self.save() cursor = Transaction().connection.cursor() if self._history: # Update the party history first. # # The approach is to make the history of all merged records # also the history of the target record. party_history_table = self.__table_history__() cursor.execute(*party_history_table.update( columns=[party_history_table.id], values=[target.id], where=(party_history_table.id == self.id))) party_fields = ModelField.search([ ('relation', '=', 'party.party'), ('ttype', '=', 'many2one'), ]) for field in party_fields: Model = Pool().get(field.model.model) if isinstance(getattr(Model, field.name), fields.Function): continue if not hasattr(Model, '__table__'): continue if Model.table_query(): continue sql_table = Model.__table__() # Update direct foreign key references cursor.execute(*sql_table.update( columns=[getattr(sql_table, field.name)], values=[target.id], where=(getattr(sql_table, field.name) == self.id))) if Model._history: # If historization is enabled on the model # then the party value in the history should # now point to the target party id since the # history of the current party is already the history of # target party. history_table = Model.__table_history__() cursor.execute(*history_table.update( columns=[getattr(history_table, field.name)], values=[target.id], where=(getattr(history_table, field.name) == self.id)))