Import old transactions and link to members

This commit is contained in:
Tanner Collin 2020-01-09 05:21:18 +00:00
parent 3b6f15accd
commit e1ad30e481
5 changed files with 119 additions and 29 deletions

View File

@ -1,20 +1,36 @@
from datetime import date
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.timezone import now
from . import old_models from . import old_models
class Member(models.Model): class Member(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT) user = models.OneToOneField(User, blank=True, null=True, on_delete=models.PROTECT)
first_name = models.CharField(max_length=32) first_name = models.CharField(max_length=32)
last_name = models.CharField(max_length=32) last_name = models.CharField(max_length=32)
old_member_id = models.IntegerField(null=True, blank=True)
set_details = models.BooleanField(default=False) set_details = models.BooleanField(default=False)
preferred_name = models.CharField(max_length=32, blank=True) preferred_name = models.CharField(max_length=32, blank=True)
phone = models.CharField(max_length=32, blank=True) phone = models.CharField(max_length=32, blank=True)
current_start_date = models.DateField(auto_now_add=True, blank=True, null=True) current_start_date = models.DateField(default=date.today, blank=True, null=True)
application_date = models.DateField(auto_now_add=True, blank=True, null=True) application_date = models.DateField(default=date.today, blank=True, null=True)
vetted_date = models.DateField(blank=True, null=True) vetted_date = models.DateField(blank=True, null=True)
monthly_fees = models.IntegerField(default=55, blank=True, null=True) monthly_fees = models.IntegerField(default=55, blank=True, null=True)
emergency_contact_name = models.CharField(max_length=64, blank=True) emergency_contact_name = models.CharField(max_length=64, blank=True)
emergency_contact_phone = models.CharField(max_length=32, blank=True) emergency_contact_phone = models.CharField(max_length=32, blank=True)
class Transaction(models.Model):
user = models.ForeignKey(User, related_name='transactions', blank=True, null=True, on_delete=models.PROTECT)
recorder = models.ForeignKey(User, related_name='+', blank=True, null=True, on_delete=models.PROTECT)
member_id = models.IntegerField(blank=True, null=True)
date = models.DateTimeField(default=now)
amount = models.DecimalField(max_digits=7, decimal_places=2)
reference_number = models.CharField(max_length=32, blank=True, null=True)
memo = models.TextField(blank=True, null=True)
number_of_membership_months = models.TextField(blank=True, null=True)
payment_method = models.TextField(blank=True, null=True)
category = models.TextField(blank=True, null=True)
account_type = models.TextField(blank=True, null=True)
info_source = models.TextField(blank=True, null=True)

View File

@ -5,23 +5,18 @@ from rest_auth.registration.serializers import RegisterSerializer
from . import models, old_models from . import models, old_models
GRAB_FIELDS = [
'preferred_name',
'phone',
'current_start_date',
'application_date',
'vetted_date',
'monthly_fees',
'emergency_contact_name',
'emergency_contact_phone',
]
#custom_error = lambda x: ValidationError(dict(non_field_errors=x)) #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 UserSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = User model = User
fields = ['id', 'username', 'email', 'member'] fields = ['id', 'username', 'email', 'member', 'transactions']
depth = 1 depth = 1
@ -45,8 +40,6 @@ class RegistrationSerializer(RegisterSerializer):
def custom_signup(self, request, user): def custom_signup(self, request, user):
data = request.data data = request.data
old_member_id = None
old_member_fields = dict(preferred_name=data['first_name'])
if data['existing_member'] == 'true': if data['existing_member'] == 'true':
old_members = old_models.Members.objects.using('old_portal') old_members = old_models.Members.objects.using('old_portal')
@ -56,15 +49,26 @@ class RegistrationSerializer(RegisterSerializer):
user.delete() user.delete()
raise ValidationError(dict(email='Unable to find in old database.')) raise ValidationError(dict(email='Unable to find in old database.'))
old_member_id = old_member.id member = models.Member.objects.get(id=old_member.id)
for f in GRAB_FIELDS: if member.user:
old_member_fields[f] = old_member.__dict__.get(f, None) raise ValidationError(dict(email='Old member already claimed.'))
models.Member.objects.create( member.user = user
user=user, member.first_name = data['first_name']
first_name=data['first_name'], member.last_name = data['last_name']
last_name=data['last_name'], member.preferred_name = data['first_name']
old_member_id=old_member_id, member.save()
**old_member_fields
) transactions = models.Transaction.objects.filter(member_id=member.id)
for t in transactions:
t.user = user
t.save()
else:
models.Member.objects.create(
user=user,
first_name=data['first_name'],
last_name=data['last_name'],
preferred_name=data['first_name'],
)

View File

@ -217,3 +217,4 @@ SITE_ID = 1
ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'none' ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_USERNAME_MIN_LENGTH = 3 ACCOUNT_USERNAME_MIN_LENGTH = 3
ACCOUNT_AUTHENTICATION_METHOD = 'username'

69
apiserver/import_old_portal.py Executable file
View File

@ -0,0 +1,69 @@
import django, sys, os
os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings'
django.setup()
from apiserver.api import models, old_models
MEMBER_FIELDS = [
'id',
'preferred_name',
'phone',
'current_start_date',
'application_date',
'vetted_date',
'monthly_fees',
'emergency_contact_name',
'emergency_contact_phone',
]
TRANSACTION_FIELDS = [
'id',
'member_id',
'date',
'amount',
'reference_number',
'memo',
'number_of_membership_months',
'payment_method',
'category',
'account_type',
'info_source',
]
print('Deleting all members...')
models.Member.objects.all().delete()
print('Importing old members...')
old_members = old_models.Members.objects.using('old_portal').all()
for m in old_members:
new_member = {}
for f in MEMBER_FIELDS:
new_member[f] = m.__dict__.get(f, None)
models.Member.objects.create(**new_member)
print('Imported #{} - {} {}'.format(
m.id, m.first_name, m.last_name
))
print('Deleting all transactions...')
models.Transaction.objects.all().delete()
print('Importing old transactions...')
old_transactions = old_models.Transactions.objects.using('old_portal').all()
for t in old_transactions:
new_transaction = {}
for f in TRANSACTION_FIELDS:
new_transaction[f] = t.__dict__.get(f, None)
models.Transaction.objects.create(**new_transaction)
print('Imported #{} - {} {}'.format(
t.id, t.member_id, t.category
))
print('Done.')

View File

@ -198,7 +198,7 @@ function DetailsForm(props) {
return ( return (
<Form onSubmit={handleSubmit}> <Form onSubmit={handleSubmit}>
<Header size='medium'>Enter New Member Details</Header> <Header size='medium'>Enter Member Details</Header>
<Form.Input <Form.Input
label='Preferred Name' label='Preferred Name'
name='preferred_name' name='preferred_name'