Add django command for running member tally cron daily

This commit is contained in:
Tanner Collin 2020-01-19 10:57:56 +00:00
parent 712a6c533f
commit 8ca8970092
3 changed files with 66 additions and 36 deletions

View 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)
))

View File

@ -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)

View File

@ -95,6 +95,7 @@ for o in old:
new['city'] = '{}, {}'.format(o.city, o.province)
new['old_email'] = o.email
new['is_minor'] = o.minor
new['paused_date'] = None
small, medium, large = None, None, None
if str(o.id) in photo_folders:
@ -111,40 +112,6 @@ for o in old:
print('Deleting all transactions...')
models.Transaction.objects.all().delete()
print('Faking membership months...')
members = models.Member.objects.all()
bad_count = 0
for m in members:
old_status = m.status
old_expire = m.expire_date
if not m.current_start_date: continue
if 'Former' in old_status:
m.status = 'Old Portal ' + old_status
m.save()
continue
import_date = datetime.date(2020, 1, 3)
tx = utils.fake_missing_membership_months(m)
utils.tally_membership_months(m, import_date)
print(m.first_name, m.last_name, tx.memo)
if old_status != m.status or old_expire != m.expire_date:
print('Expire mismatch member:', m.__dict__)
print('New status:', m.status)
print('Old status:', old_status)
print('Old expire:', old_expire)
bad_count += 1
print('Import mismatch count:', bad_count)
print('Pausing former members...')
for m in members:
if 'Former' in m.status:
paused_date = m.expire_date or datetime.date.today()
print('Importing old transactions...')
old = old_models.Transactions.objects.using('old_portal').all()
@ -159,6 +126,43 @@ for o in old:
o.id, o.member_id, o.category
))
print('Faking membership months...')
members = models.Member.objects.all()
bad_count = 0
for m in members:
old_status = m.status
old_expire = m.expire_date
if 'Former' in old_status:
m.status = 'Old Portal ' + old_status
m.save()
continue
if not m.current_start_date: continue
import_date = datetime.date(2020, 1, 3)
tx = utils.fake_missing_membership_months(m)
utils.tally_membership_months(m, import_date)
print(m.first_name, m.last_name, tx.memo)
if old_status != m.status or old_expire != m.expire_date:
print('Expire / status mismatch member:', m.__dict__)
print('New status:', m.status)
print('Old status:', old_status)
print('Old expire:', old_expire)
bad_count += 1
print('Import mismatch count:', bad_count)
print('Pausing former members...')
for m in members:
if 'Former' in m.status:
paused_date = m.expire_date or datetime.date.today()
m.paused_date = paused_date
m.save()
print('Paused', m.first_name, m.last_name)
print('Deleting all cards...')