From 7acad94f6864c897e8a150a63bf66b09709b7042 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 23 Jan 2022 21:24:29 +0000 Subject: [PATCH] Add script to calculate subscriber membership lengths --- apiserver/calc_subs_membership_length.py | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 apiserver/calc_subs_membership_length.py diff --git a/apiserver/calc_subs_membership_length.py b/apiserver/calc_subs_membership_length.py new file mode 100755 index 0000000..a49e017 --- /dev/null +++ b/apiserver/calc_subs_membership_length.py @@ -0,0 +1,42 @@ +import django, sys, os +os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings' +django.setup() + +from django.db.models import Prefetch, Sum +from apiserver.api import models, utils + +today = utils.today_alberta_tz() + +members = models.Member.objects.filter(paused_date__isnull=True) +related_tx = Prefetch( + 'user__transactions', + queryset=models.Transaction.objects.filter(category='Membership'), +) + +sub_total = 0 +sub_count = 0 +other_total = 0 +other_count = 0 + +for member in members.prefetch_related(related_tx): + name = member.preferred_name + ' ' + member.last_name[0] + start = member.application_date + length = today - member.application_date + days = length.days + + if member.user.transactions.count(): + if member.user.transactions.latest('date').paypal_txn_type == 'subscr_payment': + print('subscriber,{},{},{}'.format(name, start, days)) + sub_total += days + sub_count += 1 + continue + + print('non-subscr,{},{},{}'.format(name, start, days)) + other_total += days + other_count += 1 + + +print('subscriber avg:', int(sub_total / sub_count)) +print('non-subscr avg:', int(other_total / other_count)) + +