def _create_transaction(status, **params): params.setdefault('amount', 10) params.setdefault('currency', 'USD') params.setdefault('provider', '_manual') params.setdefault('data', {}) transaction = PaymentTransaction(event_id=dummy_event.getId(), registrant_id=dummy_registrant.getId(), status=status, **params) db.session.add(transaction) db.session.flush() return transaction
def migrate_transactions(self): self.messages.append(cformat("%{magenta!} - Payment Transactions:")) print cformat("%{white!}migrating payment transactions") count = 0 errors = 0 warnings = 0 for event, registrant, transaction in committing_iterator( self._iter_transactions()): try: data = self._get_transaction_data(transaction, event) except ValueError as e: print cformat("%{red!}{0} (evt: {1}, reg: {2})").format( e, event.id, registrant._id) errors += 1 continue if data['provider'] == '_manual' and data['amount'] == 0.0: print cformat( "%{yellow!}Skipping {0[provider]} transaction (evt: {1}, reg: {2}) " "with zero amount: {0[amount]} {0[currency]}").format( data, event.id, registrant._id) warnings += 1 continue elif data['amount'] < 0.0: print cformat( "%{yellow!}Skipping {0[provider]} transaction (evt: {1}, reg: {2}) " "with negative amount: {0[amount]} {0[currency]}").format( data, event.id, registrant._id) warnings += 1 continue pt = PaymentTransaction(event_id=int(event.id), registrant_id=int(registrant._id), status=TransactionStatus.successful, **data) count += 1 print cformat("%{cyan}{0}").format(pt) db.session.add(pt) if warnings: warning_msg = cformat( "%{yellow!}There were {0} warnings during the migration " "of the payment transactions.").format(warnings) self.messages.append(' ' + warning_msg) print warning_msg if errors: msg = cformat( "%{red!}There were some errors during the migration of the payment transactions.\n" "{0} transactions were not migrated and will be lost.\n" ).format(errors) else: msg = cformat( "%{green!}migration of {0} payment transactions successful\n" ).format(count) self.messages.append(' ' + '\n '.join(msg.split('\n'))) print msg