def sql_format(self, value): if value is not None and self.strip: if isinstance(value, (Query, Expression)): value = Trim(value, characters=string.whitespace) else: value = value.strip() return super().sql_format(value)
def __set__(self, inst, value): if value is not None and self.strip: if isinstance(value, (Query, Expression)): value = Trim(value, characters=string.whitespace) else: value = value.strip() super().__set__(inst, value)
def table_query(cls): pool = Pool() Property = pool.get('ir.property') Field = pool.get('ir.model.field') property_history = Property.__table_history__() field = Field.__table__() return property_history.join( field, condition=field.id == property_history.field).select( Max(Column(property_history, '__id')).as_('id'), Max(property_history.create_uid).as_('create_uid'), Max(property_history.create_date).as_('create_date'), Max(property_history.write_uid).as_('write_uid'), Max(property_history.write_date).as_('write_date'), Coalesce(property_history.write_date, property_history.create_date).as_('date'), Trim(Substring(property_history.res, ',.*'), 'LEADING', ',').cast(cls.template.sql_type().base).as_('template'), Trim(property_history.value, 'LEADING', ',').cast( cls.cost_price.sql_type().base).as_('cost_price'), where=(field.name == 'cost_price') & property_history.res.like('product.template,%'), group_by=(property_history.id, Coalesce(property_history.write_date, property_history.create_date), property_history.res, property_history.value))
def test_trim(self): trim = Trim(' test ') self.assertEqual(str(trim), 'TRIM(BOTH %s FROM %s)') self.assertEqual(trim.params, ( ' ', ' test ', )) trim = Trim(self.table.c1) self.assertEqual(str(trim), 'TRIM(BOTH %s FROM "c1")') self.assertEqual(trim.params, (' ', ))
def test_mapping(self): class MyAbs(Function): _function = 'MY_ABS' params = ('test', ) class MyOverlay(FunctionKeyword): _function = 'MY_OVERLAY' _keywords = ('', 'PLACING', 'FROM', 'FOR') class MyCurrentTime(FunctionNotCallable): _function = 'MY_CURRENT_TIME' class MyTrim(Trim): _function = 'MY_TRIM' abs_ = Abs(self.table.c1) overlay = Overlay(self.table.c1, 'test', 2) current_time = CurrentTime() trim = Trim(' test ') flavor = Flavor( function_mapping={ Abs: MyAbs, Overlay: MyOverlay, CurrentTime: MyCurrentTime, Trim: MyTrim, }) Flavor.set(flavor) try: self.assertEqual(str(abs_), 'MY_ABS("c1")') self.assertEqual(abs_.params, ('test', )) self.assertEqual(str(overlay), 'MY_OVERLAY("c1" PLACING %s FROM %s)') self.assertEqual(overlay.params, ('test', 2)) self.assertEqual(str(current_time), 'MY_CURRENT_TIME') self.assertEqual(current_time.params, ()) self.assertEqual(str(trim), 'MY_TRIM(BOTH %s FROM %s)') self.assertEqual(trim.params, ( ' ', ' test ', )) finally: Flavor.set(Flavor())
def __register__(cls, module_name): Party = Pool().get('party.party') Model = Pool().get('ir.model') ModelField = Pool().get('ir.model.field') Property = Pool().get('ir.property') PaymentProfile = Pool().get('party.payment_profile') TableHandler = backend.get('TableHandler') cursor = Transaction().cursor table = TableHandler(cursor, cls, module_name) migration_needed = False if not table.column_exist('credit_account'): migration_needed = True migrate_last_four_digits = False if not table.column_exist('last_four_digits'): migrate_last_four_digits = True super(PaymentTransaction, cls).__register__(module_name) if migration_needed and not Pool.test: # Migration # Set party's receivable account as the credit_account on # transactions transaction = cls.__table__() party = Party.__table__() property = Property.__table__() account_model, = Model.search([ ('model', '=', 'party.party'), ]) account_receivable_field, = ModelField.search([ ('model', '=', account_model.id), ('name', '=', 'account_receivable'), ('ttype', '=', 'many2one'), ]) update = transaction.update( columns=[transaction.credit_account], values=[ Trim( Substring(property.value, ',.*'), 'LEADING', ',' ).cast(cls.credit_account.sql_type().base) ], from_=[party, property], where=( transaction.party == party.id ) & ( property.res == Concat(Party.__name__ + ',', party.id) ) & ( property.field == account_receivable_field.id ) & ( property.company == transaction.company ) ) cursor.execute(*update) if migrate_last_four_digits and not Pool.test: transaction = cls.__table__() payment_profile = PaymentProfile.__table__() cursor.execute(*transaction.update( columns=[transaction.last_four_digits], values=[payment_profile.last_4_digits], from_=[payment_profile], where=(transaction.payment_profile == payment_profile.id) ))