Import old courses, classes and link to members

This commit is contained in:
Tanner Collin 2020-01-09 23:58:44 +00:00
parent 06c111dbbe
commit 42f3594e38
3 changed files with 142 additions and 30 deletions

View File

@ -6,7 +6,7 @@ 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, 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) first_name = models.CharField(max_length=32)
last_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) emergency_contact_phone = models.CharField(max_length=32, blank=True)
class Transaction(models.Model): class Transaction(models.Model):
user = models.ForeignKey(User, related_name='transactions', 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.PROTECT) recorder = models.ForeignKey(User, related_name='+', blank=True, null=True, on_delete=models.SET_NULL)
member_id = models.IntegerField(blank=True, null=True) member_id = models.IntegerField(blank=True, null=True)
date = models.DateField(default=date.today) date = models.DateField(default=date.today)
@ -36,10 +36,32 @@ class Transaction(models.Model):
info_source = models.TextField(blank=True, null=True) info_source = models.TextField(blank=True, null=True)
class Card(models.Model): 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) member_id = models.IntegerField(blank=True, null=True)
card_number = models.CharField(max_length=16, blank=True, null=True) card_number = models.CharField(max_length=16, blank=True, null=True)
notes = models.TextField(blank=True, null=True) notes = models.TextField(blank=True, null=True)
last_seen_at = models.DateField(default=date.today, 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) 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)

View File

@ -7,10 +7,18 @@ from . import models, old_models
#custom_error = lambda x: ValidationError(dict(non_field_errors=x)) #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): class UserSerializer(serializers.ModelSerializer):
training = UserTrainingSerializer(many=True)
class Meta: class Meta:
model = User model = User
fields = ['id', 'username', 'email', 'member', 'transactions', 'cards'] fields = ['id', 'username', 'email', 'member', 'transactions', 'cards', 'training']
depth = 1 depth = 1
@ -47,7 +55,7 @@ class RegistrationSerializer(RegisterSerializer):
old_member = old_members.get(email=data['email']) old_member = old_members.get(email=data['email'])
except old_models.Members.DoesNotExist: except old_models.Members.DoesNotExist:
user.delete() 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) member = models.Member.objects.get(id=old_member.id)
@ -70,6 +78,11 @@ class RegistrationSerializer(RegisterSerializer):
c.user = user c.user = user
c.save() c.save()
training = models.Training.objects.filter(member_id=member.id)
for t in training:
t.user = user
t.save()
else: else:
models.Member.objects.create( models.Member.objects.create(
user=user, user=user,

View File

@ -39,57 +39,134 @@ CARD_FIELDS = [
'active_status', '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...') print('Deleting all members...')
models.Member.objects.all().delete() models.Member.objects.all().delete()
print('Importing old members...') 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: for o in old:
new_member = {} new = {}
for f in MEMBER_FIELDS: 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) models.Member.objects.create(**new)
print('Imported #{} - {} {}'.format( print('Imported member #{} - {} {}'.format(
m.id, m.first_name, m.last_name o.id, o.first_name, o.last_name
)) ))
print('Deleting all transactions...') print('Deleting all transactions...')
models.Transaction.objects.all().delete() models.Transaction.objects.all().delete()
print('Importing old transactions...') 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: for o in old:
new_transaction = {} new = {}
for f in TRANSACTION_FIELDS: 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) models.Transaction.objects.create(**new)
print('Imported #{} - {} {}'.format( print('Imported transaction #{} - {} {}'.format(
t.id, t.member_id, t.category o.id, o.member_id, o.category
)) ))
print('Deleting all cards...') print('Deleting all cards...')
models.Card.objects.all().delete() models.Card.objects.all().delete()
print('Importing old cards...') 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: for o in old:
new_card = {} new = {}
for f in CARD_FIELDS: 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) models.Card.objects.create(**new)
print('Imported #{} - {} {}'.format( print('Imported card #{} - {} {}'.format(
c.id, c.card_number, c.notes 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
)) ))