2013-08-03 22:42:33 +00:00
|
|
|
"""
|
|
|
|
external library imports
|
|
|
|
"""
|
2020-02-28 20:54:09 +00:00
|
|
|
from celery.task import task
|
2013-08-03 22:42:33 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
django imports
|
|
|
|
"""
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from notification import models as notification
|
|
|
|
|
|
|
|
"""
|
|
|
|
regluit imports
|
|
|
|
"""
|
|
|
|
from regluit.payment.models import Account
|
2013-08-05 16:15:04 +00:00
|
|
|
from regluit.utils.localdatetime import date_today
|
2013-08-03 22:42:33 +00:00
|
|
|
|
|
|
|
#task to update the status of accounts
|
2020-02-28 20:54:09 +00:00
|
|
|
@task
|
2013-08-08 17:31:38 +00:00
|
|
|
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)
|
|
|
|
"""
|
2013-08-03 22:42:33 +00:00
|
|
|
errors = []
|
|
|
|
|
|
|
|
if all_accounts:
|
|
|
|
accounts_to_calc = Account.objects.all()
|
|
|
|
else:
|
|
|
|
# active accounts with expiration dates from this month earlier
|
|
|
|
today = date_today()
|
|
|
|
year = today.year
|
|
|
|
month = today.month
|
|
|
|
accounts_to_calc = Account.objects.filter(Q(date_deactivated__isnull=True)).filter((Q(card_exp_year__lt=year) | Q(card_exp_year=year, card_exp_month__lte = month)))
|
|
|
|
|
|
|
|
for account in accounts_to_calc:
|
|
|
|
try:
|
2013-08-08 17:31:38 +00:00
|
|
|
account.update_status(send_notice_on_change_only=send_notice_on_change_only)
|
2020-02-12 22:56:04 +00:00
|
|
|
except Exception as e:
|
2013-08-03 22:42:33 +00:00
|
|
|
errors.append(e)
|
|
|
|
|
|
|
|
# fire off notices
|
|
|
|
|
|
|
|
from regluit.core.tasks import emit_notifications
|
|
|
|
emit_notifications.delay()
|
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
# task run roughly 8 days ahead of card expirations
|
2020-02-28 20:54:09 +00:00
|
|
|
@task
|
2013-08-03 22:42:33 +00:00
|
|
|
def notify_expiring_accounts():
|
2018-01-20 00:18:50 +00:00
|
|
|
expiring_accounts = Account.objects.filter(status='EXPIRING', user__isnull=False)
|
2013-08-03 22:42:33 +00:00
|
|
|
for account in expiring_accounts:
|
|
|
|
notification.send_now([account.user], "account_expiring", {
|
|
|
|
'user': account.user,
|
|
|
|
'site':Site.objects.get_current()
|
|
|
|
}, True)
|
|
|
|
|
|
|
|
# used for bootstrapping our expired cc notification for first time
|
2020-02-28 20:54:09 +00:00
|
|
|
@task
|
2013-08-03 22:42:33 +00:00
|
|
|
def notify_expired_accounts():
|
|
|
|
expired_accounts = Account.objects.filter(status='EXPIRED')
|
|
|
|
for account in expired_accounts:
|
|
|
|
notification.send_now([account.user], "account_expired", {
|
|
|
|
'user': account.user,
|
|
|
|
'site':Site.objects.get_current()
|
|
|
|
}, True)
|
|
|
|
|