diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index f3b4d8e..a6e0c26 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -1,20 +1,36 @@ +from datetime import date from django.db import models from django.contrib.auth.models import User +from django.utils.timezone import now from . import old_models 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) last_name = models.CharField(max_length=32) - old_member_id = models.IntegerField(null=True, blank=True) set_details = models.BooleanField(default=False) preferred_name = 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) - application_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(default=date.today, blank=True, null=True) vetted_date = models.DateField(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_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) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index 6d0dc52..0626389 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -5,23 +5,18 @@ from rest_auth.registration.serializers import RegisterSerializer 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)) +class TransactionSerializer(serializers.ModelSerializer): + class Meta: + model = models.Transaction + fields = '__all__' + class UserSerializer(serializers.ModelSerializer): + class Meta: model = User - fields = ['id', 'username', 'email', 'member'] + fields = ['id', 'username', 'email', 'member', 'transactions'] depth = 1 @@ -45,8 +40,6 @@ class RegistrationSerializer(RegisterSerializer): def custom_signup(self, request, user): data = request.data - old_member_id = None - old_member_fields = dict(preferred_name=data['first_name']) if data['existing_member'] == 'true': old_members = old_models.Members.objects.using('old_portal') @@ -56,15 +49,26 @@ class RegistrationSerializer(RegisterSerializer): user.delete() 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: - old_member_fields[f] = old_member.__dict__.get(f, None) + if member.user: + raise ValidationError(dict(email='Old member already claimed.')) - models.Member.objects.create( - user=user, - first_name=data['first_name'], - last_name=data['last_name'], - old_member_id=old_member_id, - **old_member_fields - ) + member.user = user + member.first_name = data['first_name'] + member.last_name = data['last_name'] + member.preferred_name = data['first_name'] + member.save() + + 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'], + ) diff --git a/apiserver/apiserver/settings.py b/apiserver/apiserver/settings.py index 9c71c11..ab81e5a 100644 --- a/apiserver/apiserver/settings.py +++ b/apiserver/apiserver/settings.py @@ -217,3 +217,4 @@ SITE_ID = 1 ACCOUNT_EMAIL_REQUIRED = True ACCOUNT_EMAIL_VERIFICATION = 'none' ACCOUNT_USERNAME_MIN_LENGTH = 3 +ACCOUNT_AUTHENTICATION_METHOD = 'username' diff --git a/apiserver/import_old_portal.py b/apiserver/import_old_portal.py new file mode 100755 index 0000000..c2fefa2 --- /dev/null +++ b/apiserver/import_old_portal.py @@ -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.') diff --git a/webclient/src/App.js b/webclient/src/App.js index 0811e4b..864f9de 100644 --- a/webclient/src/App.js +++ b/webclient/src/App.js @@ -198,7 +198,7 @@ function DetailsForm(props) { return (