Skip to content

Commit

Permalink
New historical donation tracking and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronTraas committed Mar 4, 2019
1 parent 4f5332c commit 480318c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crtools/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.15.0"
__version__ = "1.15.1"
49 changes: 36 additions & 13 deletions crtools/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,34 @@ def get_role_change_status(old_role, new_role):

return False

def cleanup_member_history(member, history, timestamp):
""" make sure member history entry has all the necessary fields.
This is here to make upgrades smooth """
if 'join_date' not in history:
history['join_date'] = timestamp
if 'last_activity_date' not in history:
history['last_activity_date'] = timestamp
if 'last_donation_date' not in history:
history['last_donation_date'] = timestamp
if 'role' not in history:
history['role'] = member['role']
if 'status' not in history:
history['status'] = 'present'
if 'donations' not in history:
history['donations'] = member['donations']
if 'donations_last_week' not in history:
history['donations_last_week'] = 0
if 'events' not in history:
history['events'] = [{
'event': 'join',
'type': 'new',
'role': member['role'],
'date': timestamp
}]
return history

def create_new_member(member, timestamp):
# member has a join date,
return {
'join_date': timestamp,
'role': member['role'],
'status': 'present',
'events': [{
'event': 'join',
'type': 'new',
'role': member['role'],
'date': timestamp
}]
}
return cleanup_member_history(member, {}, timestamp)

def member_rejoin(historical_member, member, timestamp):
updated_member = copy.deepcopy(historical_member)
Expand All @@ -68,6 +83,7 @@ def member_rejoin(historical_member, member, timestamp):
})
updated_member['role'] = member['role']
updated_member['status'] = 'present'
updated_member['last_activity_date'] = timestamp

return updated_member

Expand All @@ -80,6 +96,7 @@ def member_role_change(historical_member, member, timestamp):
'date': timestamp
})
updated_member['role'] = member['role']
updated_member['last_activity_date'] = timestamp

return updated_member

Expand Down Expand Up @@ -139,14 +156,20 @@ def get_member_history(members, old_history=None, date=datetime.now()):
# Create record for user.
history['members'][tag] = create_new_member(member, timestamp)
else:
historical_member = history['members'][tag]
historical_member = cleanup_member_history(member, history['members'][tag], timestamp)
if historical_member['status'] == 'absent':
# If member exists, but is absent in history, the
# member has re-joined
history['members'][tag] = member_rejoin(historical_member, member, timestamp)
elif historical_member['role'] != member['role']:
# Member's role has changed
history['members'][tag] = member_role_change(historical_member, member, timestamp)
if member['donations'] < historical_member['donations']:
historical_member['donations_last_week'] = historical_member['donations']
if member['donations'] != historical_member['donations']:
historical_member['donations'] = member['donations']
history['last_donation_date'] = timestamp
history['last_activity_date'] = timestamp

# Look for missing members. If they're missing, they quit
for tag, member in history['members'].copy().items():
Expand Down
16 changes: 16 additions & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from crtools import history
from datetime import datetime
import copy

__fake_members__ = [
{
"tag": "#AAAAAA",
"role": "leader",
"donations": 100
},
{
"tag": "#CCCCCC",
"role": "member",
"donations": 10
},
{
"tag": "#DDDDDD",
"role": "elder",
"donations": 0
}
]

Expand Down Expand Up @@ -130,6 +134,18 @@ def test_get_member_history_role_change():
assert members[__fake_members__[2]['tag']]['events'][1]['role'] == __fake_members__[2]['role']
assert members[__fake_members__[2]['tag']]['events'][1]['date'] == timestamp

def test_get_member_history_donations_reset():
date = datetime(2019, 2, 12, 7, 32, 1, 0)
timestamp = datetime.timestamp(date)
members_copy = copy.deepcopy(__fake_members__)
members_copy[0]['donations'] = 5
h = history.get_member_history(__fake_members__, __fake_history__, date)
h2 = history.get_member_history(members_copy, h, date)

members = h2['members']
assert members[__fake_members__[0]['tag']]['donations'] == 5
assert members[__fake_members__[0]['tag']]['donations_last_week'] == __fake_members__[0]['donations']

def test_get_member_history_unchanged():
""" Getting history twice. Nothing should be changed except the
timestamp """
Expand Down

0 comments on commit 480318c

Please sign in to comment.