diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index b966600..6881f2e 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -6,7 +6,7 @@ from django.utils.timezone import now from . import old_models class Member(models.Model): - user = models.OneToOneField(User, blank=True, null=True, on_delete=models.PROTECT) + user = models.OneToOneField(User, blank=True, null=True, on_delete=models.SET_NULL) first_name = models.CharField(max_length=32) last_name = models.CharField(max_length=32) @@ -21,8 +21,8 @@ class Member(models.Model): 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) + user = models.ForeignKey(User, related_name='transactions', blank=True, null=True, on_delete=models.SET_NULL) + recorder = models.ForeignKey(User, related_name='+', blank=True, null=True, on_delete=models.SET_NULL) member_id = models.IntegerField(blank=True, null=True) date = models.DateField(default=date.today) @@ -36,10 +36,32 @@ class Transaction(models.Model): 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) + user = models.ForeignKey(User, related_name='cards', blank=True, null=True, on_delete=models.SET_NULL) 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) + +class Course(models.Model): + name = models.TextField(blank=True, null=True) + description = models.TextField(blank=True, null=True) + is_old = models.BooleanField(default=False) + +class Session(models.Model): + instructor = models.ForeignKey(User, related_name='teaching', blank=True, null=True, on_delete=models.SET_NULL) + course = models.ForeignKey(Course, blank=True, null=True, on_delete=models.SET_NULL) + + old_instructor = models.TextField(blank=True, null=True) + datetime = models.DateTimeField(blank=True, null=True) + cost = models.DecimalField(max_digits=5, decimal_places=2) + +class Training(models.Model): + user = models.ForeignKey(User, related_name='training', blank=True, null=True, on_delete=models.SET_NULL) + session = models.ForeignKey(Session, blank=True, null=True, on_delete=models.SET_NULL) + + member_id = models.IntegerField(blank=True, null=True) + attendance_status = models.TextField(blank=True, null=True) + sign_up_date = models.DateField(default=date.today, blank=True, null=True) + paid_date = models.DateField(default=date.today, blank=True, null=True) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index e946554..88264a7 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -7,10 +7,18 @@ from . import models, old_models #custom_error = lambda x: ValidationError(dict(non_field_errors=x)) +class UserTrainingSerializer(serializers.ModelSerializer): + class Meta: + model = models.Training + exclude = ['user'] + depth = 2 + class UserSerializer(serializers.ModelSerializer): + training = UserTrainingSerializer(many=True) + class Meta: model = User - fields = ['id', 'username', 'email', 'member', 'transactions', 'cards'] + fields = ['id', 'username', 'email', 'member', 'transactions', 'cards', 'training'] depth = 1 @@ -47,7 +55,7 @@ class RegistrationSerializer(RegisterSerializer): old_member = old_members.get(email=data['email']) except old_models.Members.DoesNotExist: user.delete() - raise ValidationError(dict(email='Unable to find in old database.')) + raise ValidationError(dict(email='Unable to find email in old database.')) member = models.Member.objects.get(id=old_member.id) @@ -70,6 +78,11 @@ class RegistrationSerializer(RegisterSerializer): c.user = user c.save() + training = models.Training.objects.filter(member_id=member.id) + for t in training: + t.user = user + t.save() + else: models.Member.objects.create( user=user, diff --git a/apiserver/import_old_portal.py b/apiserver/import_old_portal.py index 1ccddba..883c7b4 100755 --- a/apiserver/import_old_portal.py +++ b/apiserver/import_old_portal.py @@ -39,57 +39,134 @@ CARD_FIELDS = [ 'active_status', ] +COURSE_FIELDS = [ + 'id', + 'name', + 'description', + # True -> is_old +] + +SESSION_FIELDS = [ + 'id', + # course_id -> course + # instructor -> old_instructor + 'datetime', + 'cost', +] + +TRAINING_FIELDS = [ + 'id', + # class_session_id -> session + 'member_id', + 'attendance_status', + 'sign_up_date', + 'paid_date', +] + print('Deleting all members...') models.Member.objects.all().delete() - print('Importing old members...') -old_members = old_models.Members.objects.using('old_portal').all() +old = old_models.Members.objects.using('old_portal').all() -for m in old_members: - new_member = {} +for o in old: + new = {} for f in MEMBER_FIELDS: - new_member[f] = m.__dict__.get(f, None) + new[f] = o.__dict__.get(f, None) - models.Member.objects.create(**new_member) - print('Imported #{} - {} {}'.format( - m.id, m.first_name, m.last_name + models.Member.objects.create(**new) + print('Imported member #{} - {} {}'.format( + o.id, o.first_name, o.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() +old = old_models.Transactions.objects.using('old_portal').all() -for t in old_transactions: - new_transaction = {} +for o in old: + new = {} for f in TRANSACTION_FIELDS: - new_transaction[f] = t.__dict__.get(f, None) + new[f] = o.__dict__.get(f, None) - models.Transaction.objects.create(**new_transaction) - print('Imported #{} - {} {}'.format( - t.id, t.member_id, t.category + models.Transaction.objects.create(**new) + print('Imported transaction #{} - {} {}'.format( + o.id, o.member_id, o.category )) print('Deleting all cards...') models.Card.objects.all().delete() - print('Importing old cards...') -old_cards = old_models.AccessKeys.objects.using('old_portal').all() +old = old_models.AccessKeys.objects.using('old_portal').all() -for c in old_cards: - new_card = {} +for o in old: + new = {} for f in CARD_FIELDS: - new_card[f] = c.__dict__.get(f, None) + new[f] = o.__dict__.get(f, None) - models.Card.objects.create(**new_card) - print('Imported #{} - {} {}'.format( - c.id, c.card_number, c.notes + models.Card.objects.create(**new) + print('Imported card #{} - {} {}'.format( + o.id, o.card_number, o.notes + )) + + +print('Deleting all courses...') +models.Course.objects.all().delete() +print('Importing old courses...') +old = old_models.Courses.objects.using('old_portal').all() + +for o in old: + new = {} + + for f in COURSE_FIELDS: + new[f] = o.__dict__.get(f, None) + new['name'] = new['name'].split('<',1)[0] + new['is_old'] = True + + models.Course.objects.create(**new) + print('Imported course #{} - {}'.format( + o.id, new['name'] + )) + + +print('Deleting all sessions...') +models.Session.objects.all().delete() +print('Importing old session...') +old = old_models.ClassSessions.objects.using('old_portal').all() + +for o in old: + new = {} + + for f in SESSION_FIELDS: + new[f] = o.__dict__.get(f, None) + new['course'] = models.Course.objects.get(id=o.course_id) + new['old_instructor'] = o.instructor + + models.Session.objects.create(**new) + print('Imported session #{} - {} {}'.format( + o.id, o.instructor, new['course'] + )) + + +print('Deleting all training...') +models.Training.objects.all().delete() +print('Importing old training...') +old = old_models.ClassRegistrants.objects.using('old_portal').all() + +for o in old: + new = {} + + for f in TRAINING_FIELDS: + new[f] = o.__dict__.get(f, None) + new['session'] = models.Session.objects.get(id=o.class_session_id) + + models.Training.objects.create(**new) + print('Imported training #{} - {} {}'.format( + o.id, new['session'], o.member_id ))