From cf7160b72de18225a8901333e130cf4222a8c089 Mon Sep 17 00:00:00 2001 From: Raymond Yee Date: Thu, 8 Aug 2013 13:31:38 -0400 Subject: [PATCH] Adding an optional flag to Account.update_status, which by default, send notices only if the status is *changing*. Set send_notice_on_change_only = False to send notice based on new_status regardless of old status. (Useful for initialization) --- payment/models.py | 7 +++++-- payment/tasks.py | 10 +++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/payment/models.py b/payment/models.py index e8f9000e..14255df8 100644 --- a/payment/models.py +++ b/payment/models.py @@ -397,9 +397,12 @@ class Account(models.Model): return 'ACTIVE' - def update_status( self, value=None): + def update_status( self, value=None, send_notice_on_change_only=True): """set Account.status = value unless value is None, in which case, we set Account.status=self.calculated_status() fire off associated notifications + + By default, send notices only if the status is *changing*. Set send_notice_on_change_only = False to + send notice based on new_status regardless of old status. (Useful for initialization) """ old_status = self.status @@ -411,7 +414,7 @@ class Account(models.Model): self.status = new_status self.save() - if old_status != new_status: + if not send_notice_on_change_only or (old_status != new_status): logger.info( "Account status change: %d %s %s", instance.pk, old_status, new_status) diff --git a/payment/tasks.py b/payment/tasks.py index 6d9b430f..2febd582 100644 --- a/payment/tasks.py +++ b/payment/tasks.py @@ -19,8 +19,12 @@ from regluit.utils.localdatetime import date_today #task to update the status of accounts @task -def update_account_status(all_accounts=True): - """update the status of all Accounts""" +def update_account_status(all_accounts=True, send_notice_on_change_only=True): + """update the status of all Accounts + + By default, send notices only if the status is *changing*. Set send_notice_on_change_only = False to + send notice based on new_status regardless of old status. (Useful for initialization) + """ errors = [] if all_accounts: @@ -34,7 +38,7 @@ def update_account_status(all_accounts=True): for account in accounts_to_calc: try: - account.update_status() + account.update_status(send_notice_on_change_only=send_notice_on_change_only) except Exception, e: errors.append(e)