Add django command for running member tally cron daily
This commit is contained in:
24
apiserver/apiserver/api/management/commands/run_daily.py
Normal file
24
apiserver/apiserver/api/management/commands/run_daily.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from apiserver.api import models, utils
|
||||
|
||||
import time
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Tasks to run on the portal daily. 7am UTC = 12am or 1am Calgary'
|
||||
|
||||
def tally_active_members(self):
|
||||
all_members = models.Member.objects
|
||||
active_members = all_members.filter(paused_date__isnull=True)
|
||||
|
||||
for member in active_members:
|
||||
utils.tally_membership_months(member)
|
||||
|
||||
return active_members.count()
|
||||
|
||||
|
||||
def handle(self, *args, **options):
|
||||
start = time.time()
|
||||
count = self.tally_active_members()
|
||||
self.stdout.write('Tallied {} active members in {} s'.format(
|
||||
count, str(time.time() - start)
|
||||
))
|
@@ -7,8 +7,9 @@ from . import models, old_models
|
||||
|
||||
def num_months_spanned(d1, d2):
|
||||
'''
|
||||
Return number of months thresholds two dates span.
|
||||
Return number of month thresholds two dates span.
|
||||
Order of arguments is same as subtraction
|
||||
ie. Feb 2, Jan 29 returns 1
|
||||
'''
|
||||
return (d1.year - d2.year) * 12 + d1.month - d2.month
|
||||
|
||||
@@ -16,13 +17,14 @@ def num_months_difference(d1, d2):
|
||||
'''
|
||||
Return number of whole months between two dates.
|
||||
Order of arguments is same as subtraction
|
||||
ie. Feb 2, Jan 29 returns 0
|
||||
'''
|
||||
r = relativedelta.relativedelta(d1, d2)
|
||||
return r.months + 12 * r.years
|
||||
|
||||
def calc_member_status(expire_date, fake_date=None):
|
||||
'''
|
||||
Returns the member's status and if their membership should stop
|
||||
Return: status, if we should pause them
|
||||
'''
|
||||
today = fake_date or datetime.date.today()
|
||||
difference = num_months_difference(expire_date, today)
|
||||
|
Reference in New Issue
Block a user