Skip to content
Snippets Groups Projects
Commit 78cdf055 authored by florimondmanca's avatar florimondmanca
Browse files

refactor: create registration_created signal where user and student are created

parent 99827505
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ from tutoring.models import School
from profiles.models import Student
from .models import EmergencyContact, Registration
from .signals import registration_created
User = get_user_model()
......@@ -75,7 +76,6 @@ class RegistrationSerializer(serializers.ModelSerializer):
password = validated_data.pop('password')
address_data = validated_data.pop('address', None)
emergency_contact_data = validated_data.pop('emergency_contact', None)
school = validated_data.get('school', None)
# The following block will create a bunch of objects and save them
# in the database. We don't want them to be saved separately.
......@@ -99,28 +99,17 @@ class RegistrationSerializer(serializers.ModelSerializer):
else:
emergency_contact = None
# Create the user
user = User.objects.create_user(
email=validated_data['email'],
password=password,
first_name=validated_data['first_name'],
last_name=validated_data['last_name'],
date_of_birth=validated_data.get('date_of_birth'),
phone_number=validated_data.get('phone'),
)
# Create the registration
registration = Registration.objects.create(
**validated_data,
address=address,
emergency_contact=emergency_contact,
)
# Create the student profile and tie the registration to it
Student.objects.create(
user=user,
school=school,
registration=registration,
# Fire a registration_created signal
registration_created.send(
sender=Registration,
instance=registration,
password=password,
)
return registration
......
"""Register signals."""
from django.contrib.auth import get_user_model
from django.dispatch import Signal, receiver
from profiles.models import Student
from .models import Registration
User = get_user_model()
registration_created = Signal(providing_args=('instance', 'password'))
@receiver(registration_created, sender=Registration)
def create_user_and_student(sender, instance: Registration,
password: str, **kwargs):
"""Create a user and student after on a registration_created signal."""
user = User.objects.create_user(
email=instance.email,
password=password,
first_name=instance.first_name,
last_name=instance.last_name,
date_of_birth=instance.date_of_birth,
phone_number=instance.phone,
)
Student.objects.create(
user=user,
school=instance.school,
registration=instance,
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment