make recharge_failed_transactions a method on account

There were four places in the code that triggered the
recharge_failed_transactions signal and only one of them mattered. I
think this layout makes the code much easier to understand.
pull/1/head
eric 2013-08-05 14:37:46 -04:00
parent 99c24f5fbf
commit c30c0de604
2 changed files with 20 additions and 22 deletions

View File

@ -364,6 +364,7 @@ class Account(models.Model):
def deactivate(self):
"""Don't allow more than one active Account of given host to be associated with a given user"""
self.date_deactivated = now()
self.status = 'DEACTIVATED'
self.save()
def calculated_status(self):
@ -443,6 +444,21 @@ class Account(models.Model):
# nothing needs to happen here
pass
def recharge_failed_transactions(self):
"""When a new Account is saved, check whether this is the new active account for a user. If so, recharge any
outstanding failed transactions
"""
transactions_to_recharge = self.user.transaction_set.filter((Q(status=TRANSACTION_STATUS_FAILED) | Q(status=TRANSACTION_STATUS_ERROR)) & Q(campaign__status='SUCCESSFUL')).all()
if transactions_to_recharge:
from regluit.payment.manager import PaymentManager
pm = PaymentManager()
for transaction in transactions_to_recharge:
# check whether we are still within the window to recharge
if (now() - transaction.campaign.deadline) < datetime.timedelta(settings.RECHARGE_WINDOW):
logger.info("Recharging transaction {0} w/ status {1}".format(transaction.id, transaction.status))
pm.execute_transaction(transaction, [])
# handle any save, updates to a payment.Transaction
@ -465,25 +481,4 @@ post_delete.connect(handle_transaction_delete,sender=Transaction)
# handle recharging failed transactions
def recharge_failed_transactions(sender, created, instance, **kwargs):
"""When a new Account is saved, check whether this is the new active account for a user. If so, recharge any
outstanding failed transactions
"""
# make sure the new account is active
if instance.date_deactivated is not None:
return False
transactions_to_recharge = instance.user.transaction_set.filter((Q(status=TRANSACTION_STATUS_FAILED) | Q(status=TRANSACTION_STATUS_ERROR)) & Q(campaign__status='SUCCESSFUL')).all()
if transactions_to_recharge:
from regluit.payment.manager import PaymentManager
pm = PaymentManager()
for transaction in transactions_to_recharge:
# check whether we are still within the window to recharge
if (now() - transaction.campaign.deadline) < datetime.timedelta(settings.RECHARGE_WINDOW):
logger.info("Recharging transaction {0} w/ status {1}".format(transaction.id, transaction.status))
pm.execute_transaction(transaction, [])
post_save.connect(recharge_failed_transactions, sender=Account)

View File

@ -590,7 +590,10 @@ class Processor(baseprocessor.Processor):
)
if user.profile.account:
user.profile.account.deactivate()
account.save()
account.save()
account.recharge_failed_transactions()
else:
account.save()
return account
class Preapproval(StripePaymentRequest, baseprocessor.Processor.Preapproval):