From 73b6d19c19ec39d3064df7a0f626bfc1f96c7c36 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 19 Jan 2020 06:58:51 +0000 Subject: [PATCH] Add a fake membership transaction when importing old portal --- apiserver/apiserver/api/tests.py | 29 +++++++++++++++--------- apiserver/apiserver/api/utils.py | 29 ++++++++++++++---------- apiserver/import_old_portal.py | 38 +++++++++++++++++++++++++++++++- webclient/src/Home.js | 9 ++++---- 4 files changed, 76 insertions(+), 29 deletions(-) diff --git a/apiserver/apiserver/api/tests.py b/apiserver/apiserver/api/tests.py index 7c426db..1cf9683 100644 --- a/apiserver/apiserver/api/tests.py +++ b/apiserver/apiserver/api/tests.py @@ -136,51 +136,58 @@ class TestCalcStatus(TestCase): def test_calc_member_status_14_days(self): expire_date = datetime.date.today() + datetime.timedelta(days=14) - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) self.assertEqual(status, 'Current') + self.assertEqual(former, False) def test_calc_member_status_90_days(self): expire_date = datetime.date.today() + datetime.timedelta(days=90) - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) self.assertEqual(status, 'Prepaid') + self.assertEqual(former, False) def test_calc_member_status_tomorrow(self): expire_date = datetime.date.today() + datetime.timedelta(days=1) - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) self.assertEqual(status, 'Current') + self.assertEqual(former, False) def test_calc_member_status_today(self): expire_date = datetime.date.today() - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) - self.assertEqual(status, 'Current') + self.assertEqual(status, 'Due') + self.assertEqual(former, False) def test_calc_member_status_yesterday(self): expire_date = datetime.date.today() - datetime.timedelta(days=1) - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) self.assertEqual(status, 'Due') + self.assertEqual(former, False) def test_calc_member_status_85_days_ago(self): expire_date = datetime.date.today() - datetime.timedelta(days=85) - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) self.assertEqual(status, 'Overdue') + self.assertEqual(former, False) def test_calc_member_status_95_days_ago(self): expire_date = datetime.date.today() - datetime.timedelta(days=95) - status = utils.calc_member_status(expire_date) + status, former = utils.calc_member_status(expire_date) - self.assertEqual(status, 'Former Member') + self.assertEqual(status, 'Overdue') + self.assertEqual(former, True) class TestFakeMonths(TestCase): @@ -308,7 +315,7 @@ class TestTallyMembership(TestCase): self.assertEqual(member.expire_date, end_date) self.assertEqual(member.status, 'Overdue') - def test_tally_membership_months_overdue(self): + def test_tally_membership_months_overdue_pause(self): member = self.get_member_clear_transactions() test_num_months = 1 start_date = datetime.date.today() - relativedelta.relativedelta(months=6, days=14) @@ -328,7 +335,7 @@ class TestTallyMembership(TestCase): self.assertEqual(member.expire_date, end_date) self.assertEqual(member.paused_date, end_date) - self.assertEqual(member.status, 'Former Member') + self.assertEqual(member.status, 'Overdue') def test_tally_membership_months_dont_run(self): member = self.get_member_clear_transactions() diff --git a/apiserver/apiserver/api/utils.py b/apiserver/apiserver/api/utils.py index fd3e2a1..d329a87 100644 --- a/apiserver/apiserver/api/utils.py +++ b/apiserver/apiserver/api/utils.py @@ -20,20 +20,24 @@ def num_months_difference(d1, d2): r = relativedelta.relativedelta(d1, d2) return r.months + 12 * r.years -def calc_member_status(expire_date): - today = datetime.date.today() +def calc_member_status(expire_date, fake_date=None): + ''' + Returns the member's status and if their membership should stop + ''' + today = fake_date or datetime.date.today() difference = num_months_difference(expire_date, today) + #if today + datetime.timedelta(days=29) < expire_date: if difference >= 1: - return 'Prepaid' + return 'Prepaid', False elif difference <= -3: - return 'Former Member' + return 'Overdue', True elif difference <= -1: - return 'Overdue' - elif today <= expire_date: - return 'Current' - elif today > expire_date: - return 'Due' + return 'Overdue', False + elif today < expire_date: + return 'Current', False + elif today >= expire_date: + return 'Due', False else: raise() @@ -70,7 +74,7 @@ def fake_missing_membership_months(member): return tx -def tally_membership_months(member): +def tally_membership_months(member, fake_date=None): ''' Sum together member's dues and calculate their new expire date and status Doesn't work if member is paused. @@ -78,18 +82,19 @@ def tally_membership_months(member): if member.paused_date: return False start_date = member.current_start_date + if not start_date: return False txs = models.Transaction.objects.filter(member_id=member.id) total_months_agg = txs.aggregate(Sum('number_of_membership_months')) total_months = total_months_agg['number_of_membership_months__sum'] expire_date = add_months(start_date, total_months) - status = calc_member_status(expire_date) + status, former = calc_member_status(expire_date, fake_date) member.expire_date = expire_date member.status = status - if status == 'Former Member': + if former: member.paused_date = expire_date member.save() diff --git a/apiserver/import_old_portal.py b/apiserver/import_old_portal.py index ae7f117..397a660 100755 --- a/apiserver/import_old_portal.py +++ b/apiserver/import_old_portal.py @@ -2,7 +2,8 @@ import django, sys, os os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings' django.setup() -from apiserver.api import models, old_models +import datetime +from apiserver.api import models, old_models, utils from apiserver.api.serializers import process_image MEMBER_FIELDS = [ @@ -110,6 +111,40 @@ 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() @@ -125,6 +160,7 @@ for o in old: )) + print('Deleting all cards...') models.Card.objects.all().delete() print('Importing old cards...') diff --git a/webclient/src/Home.js b/webclient/src/Home.js index 50013bc..c223ac4 100644 --- a/webclient/src/Home.js +++ b/webclient/src/Home.js @@ -100,8 +100,7 @@ function MemberInfo(props) { }; export function Home(props) { - const { token, setTokenCache, user, setUserCache } = props; - + const { user } = props; return ( @@ -110,14 +109,14 @@ export function Home(props) { user.member.set_details ? : - + :
- + Or - +
}