From bfc757cace8999287b1c7289b60c7378fa5bde7f Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Wed, 29 Jan 2020 23:42:10 +0000 Subject: [PATCH] Add script to parse old paypal txs into member hints --- apiserver/.gitignore | 1 + apiserver/apiserver/api/models.py | 4 ++ apiserver/old_paypal/.gitkeep | 0 apiserver/train_paypal_ids.py | 91 +++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 apiserver/old_paypal/.gitkeep create mode 100755 apiserver/train_paypal_ids.py diff --git a/apiserver/.gitignore b/apiserver/.gitignore index 823135d..5b8252a 100644 --- a/apiserver/.gitignore +++ b/apiserver/.gitignore @@ -109,3 +109,4 @@ old_models.py migrations/ data/ old_photos/ +old_paypal/ diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index ba27ce1..8352fe1 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -53,6 +53,10 @@ class Transaction(models.Model): account_type = models.TextField(blank=True, null=True) info_source = models.TextField(blank=True, null=True) +class PayPalHint(models.Model): + account = models.CharField(unique=True, max_length=13) + member_id = models.IntegerField() + class Card(models.Model): user = models.ForeignKey(User, related_name='cards', blank=True, null=True, on_delete=models.SET_NULL) diff --git a/apiserver/old_paypal/.gitkeep b/apiserver/old_paypal/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apiserver/train_paypal_ids.py b/apiserver/train_paypal_ids.py new file mode 100755 index 0000000..0b8126e --- /dev/null +++ b/apiserver/train_paypal_ids.py @@ -0,0 +1,91 @@ +import django, sys, os +os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings' +django.setup() + +import datetime +import json +from apiserver.api import models, old_models, utils + +PAYPAL_FOLDER = 'old_paypal/' +transactions = models.Transaction.objects.all() + +paypal_files = os.listdir(PAYPAL_FOLDER) +paypal_json = [x for x in paypal_files if x.endswith('.json')] + +if paypal_json: + print('Found paypal json files:', paypal_json) +else: + print('Couldnt find any paypal json files in', PAYPAL_FOLDER) + exit(1) + +paypal_txs = [] + +for filename in paypal_json: + with open(PAYPAL_FOLDER + filename) as f: + j = json.load(f) + paypal_txs.extend(j['transaction_details']) + +print('Num transactions found:', len(paypal_txs)) +print('Linking with portal transactions...') + +paypal_accounts = {} + +for t in paypal_txs: + t_info = t['transaction_info'] + + + account_id = t_info.get('paypal_account_id', None) + if not account_id: + print('Skipping tx id: {}, no payer (could be bank tx)'.format( + t_info['transaction_id'], + )) + continue + + if account_id not in paypal_accounts: + paypal_accounts[account_id] = [] + + + reference = t_info['transaction_id'][:11] + try: + portal_tx = transactions.get(reference_number=reference) + paypal_accounts[account_id].append(portal_tx.member_id) + except models.Transaction.DoesNotExist: + print('Unable to find portal transaction for id: {}, ref: {}, date: {}, name: {} {}, email: {}'.format( + t_info['transaction_id'], + reference, + t_info['transaction_initiation_date'][:10], + t['payer_info']['payer_name'].get('given_name', 'unknown'), + t['payer_info']['payer_name'].get('surname', 'unknown'), + t['payer_info'].get('email_address', 'unknown'), + )) + +print('Num paypal accounts found:', len(paypal_accounts)) +print('Linking with portal members...') +count = 0 + +for account_id, member_ids in paypal_accounts.items(): + if len(member_ids) == 0: + print('Skipping account {}, no members found'.format( + account_id, + )) + continue + + member_id = member_ids[0] + + if len(set(member_ids)) > 1: + print('Account {} has multiple members {}, assuming {}'.format( + account_id, + str(set(member_ids)), + member_id, + )) + + print(account_id, '-->', member_id) + + models.PayPalHint.objects.update_or_create( + account=account_id, + defaults=dict(member_id=member_id), + ) + count += 1 + +print('Num paypal hints processed:', count) +print('Done.')