From 1bf5dd182c899009b07639651bf231a6f5f3dfcb Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 9 Jan 2020 06:45:32 +0000 Subject: [PATCH] Import old cards and link to members --- apiserver/apiserver/api/models.py | 9 +++++++++ apiserver/apiserver/api/serializers.py | 19 +++++++++++------- apiserver/import_old_portal.py | 27 ++++++++++++++++++++++++++ webclient/src/App.js | 11 +++++++++-- 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index 6054910..b966600 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -34,3 +34,12 @@ class Transaction(models.Model): category = models.TextField(blank=True, null=True) account_type = models.TextField(blank=True, null=True) info_source = models.TextField(blank=True, null=True) + +class Card(models.Model): + user = models.ForeignKey(User, related_name='cards', blank=True, null=True, on_delete=models.PROTECT) + + member_id = models.IntegerField(blank=True, null=True) + card_number = models.CharField(max_length=16, blank=True, null=True) + notes = models.TextField(blank=True, null=True) + last_seen_at = models.DateField(default=date.today, blank=True, null=True) + active_status = models.CharField(max_length=32, blank=True, null=True) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index 0626389..e946554 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -7,16 +7,10 @@ from . import models, old_models #custom_error = lambda x: ValidationError(dict(non_field_errors=x)) -class TransactionSerializer(serializers.ModelSerializer): - class Meta: - model = models.Transaction - fields = '__all__' - class UserSerializer(serializers.ModelSerializer): - class Meta: model = User - fields = ['id', 'username', 'email', 'member', 'transactions'] + fields = ['id', 'username', 'email', 'member', 'transactions', 'cards'] depth = 1 @@ -33,6 +27,12 @@ class AdminMemberSerializer(serializers.ModelSerializer): read_only_fields = ['id', 'user'] +class TransactionSerializer(serializers.ModelSerializer): + class Meta: + model = models.Transaction + fields = '__all__' + + class RegistrationSerializer(RegisterSerializer): first_name = serializers.CharField(max_length=32) last_name = serializers.CharField(max_length=32) @@ -65,6 +65,11 @@ class RegistrationSerializer(RegisterSerializer): t.user = user t.save() + cards = models.Card.objects.filter(member_id=member.id) + for c in cards: + c.user = user + c.save() + else: models.Member.objects.create( user=user, diff --git a/apiserver/import_old_portal.py b/apiserver/import_old_portal.py index c2fefa2..1ccddba 100755 --- a/apiserver/import_old_portal.py +++ b/apiserver/import_old_portal.py @@ -30,6 +30,15 @@ TRANSACTION_FIELDS = [ 'info_source', ] +CARD_FIELDS = [ + 'id', + 'member_id', + 'card_number', + 'notes', + 'last_seen_at', + 'active_status', +] + print('Deleting all members...') models.Member.objects.all().delete() @@ -66,4 +75,22 @@ for t in old_transactions: )) +print('Deleting all cards...') +models.Card.objects.all().delete() + +print('Importing old cards...') +old_cards = old_models.AccessKeys.objects.using('old_portal').all() + +for c in old_cards: + new_card = {} + + for f in CARD_FIELDS: + new_card[f] = c.__dict__.get(f, None) + + models.Card.objects.create(**new_card) + print('Imported #{} - {} {}'.format( + c.id, c.card_number, c.notes + )) + + print('Done.') diff --git a/webclient/src/App.js b/webclient/src/App.js index 16c4f87..ed19937 100644 --- a/webclient/src/App.js +++ b/webclient/src/App.js @@ -239,7 +239,8 @@ function MemberInfo(props) { const user = props.user; const member = user.member; - const lastTrans = user.transactions.slice(-3); + const lastTrans = user.transactions && user.transactions.slice(-3).reverse(); + const lastCard = user.cards && user.cards.sort((a, b) => a.last_seen_at > b.last_seen_at)[0]; return (
@@ -282,9 +283,13 @@ function MemberInfo(props) { {member.vetted_date || 'Not vetted'} - Monthly + Monthly: ${member.monthly_fees || '???'} + + Card Number: + {lastCard && lastCard.card_number || 'None'} + @@ -365,7 +370,9 @@ function App() { + }