Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
oser-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hamza Touizrat
oser-backend
Commits
78cdf055
Commit
78cdf055
authored
May 12, 2018
by
florimondmanca
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
oser_backend/register/serializers.py
+6
-17
6 additions, 17 deletions
oser_backend/register/serializers.py
oser_backend/register/signals.py
+33
-0
33 additions, 0 deletions
oser_backend/register/signals.py
with
39 additions
and
17 deletions
oser_backend/register/serializers.py
+
6
−
17
View file @
78cdf055
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
oser_backend/register/signals.py
0 → 100644
+
33
−
0
View file @
78cdf055
"""
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
,
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment