Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ECE651_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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Chris Li
ECE651_Backend
Commits
4cf77e1b
Commit
4cf77e1b
authored
2 years ago
by
Chris Li
Browse files
Options
Downloads
Patches
Plain Diff
Enable modify username and email.
parent
9889b7ac
No related branches found
No related tags found
1 merge request
!3
Enable modify username and email.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/serializers/profile.py
+43
-2
43 additions, 2 deletions
core/serializers/profile.py
core/signals.py
+1
-1
1 addition, 1 deletion
core/signals.py
with
44 additions
and
3 deletions
core/serializers/profile.py
+
43
−
2
View file @
4cf77e1b
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
django_countries.serializers
import
CountryFieldMixin
from
django.contrib.auth.models
import
User
from
core.models.profile
import
Profile
from
core.models.profile
import
Profile
class
UserUpdateSerializer
(
serializers
.
Serializer
):
username
=
serializers
.
CharField
(
required
=
False
)
email
=
serializers
.
EmailField
(
required
=
False
)
def
validate
(
self
,
data
):
if
'
username
'
in
data
and
User
.
objects
.
filter
(
username
=
data
[
'
username
'
]).
count
()
!=
0
:
raise
serializers
.
ValidationError
({
'
username
'
:
"
This username is being used by others.
"
})
if
'
email
'
in
data
and
User
.
objects
.
filter
(
username
=
data
[
'
email
'
]).
count
()
!=
0
:
raise
serializers
.
ValidationError
({
'
email
'
:
"
This email address is being used by others.
"
})
return
data
def
update
(
self
,
instance
,
validated_data
):
instance
.
username
=
validated_data
.
get
(
'
username
'
,
instance
.
username
)
instance
.
email
=
validated_data
.
get
(
'
email
'
,
instance
.
email
)
if
validated_data
.
get
(
'
email
'
,
None
):
instance
.
is_active
=
False
instance
.
save
()
return
instance
class
ProfileSerializer
(
CountryFieldMixin
,
serializers
.
ModelSerializer
):
user
=
UserUpdateSerializer
(
required
=
False
)
class
ProfileSerializer
(
serializers
.
ModelSerializer
):
country
=
serializers
.
CharField
()
class
Meta
:
class
Meta
:
model
=
Profile
model
=
Profile
fields
=
(
'
user
'
,
'
bio
'
,
'
birthday
'
,
'
country
'
,
'
city
'
,
'
affiliation
'
,
'
photo
'
)
fields
=
(
'
user
'
,
'
bio
'
,
'
birthday
'
,
'
country
'
,
'
city
'
,
'
affiliation
'
,
'
photo
'
)
def
update
(
self
,
instance
,
validated_data
):
user_data
=
validated_data
.
get
(
'
user
'
,
None
)
if
user_data
:
user
=
instance
.
user
user_serializer
=
UserUpdateSerializer
(
data
=
user_data
)
user_serializer
.
is_valid
(
raise_exception
=
True
)
user_serializer
.
update
(
user
,
user_data
)
for
attr
in
self
.
Meta
.
fields
:
if
attr
==
'
user
'
:
continue
setattr
(
instance
,
attr
,
validated_data
.
get
(
attr
,
getattr
(
instance
,
attr
)))
instance
.
save
()
return
instance
This diff is collapsed.
Click to expand it.
core/signals.py
+
1
−
1
View file @
4cf77e1b
...
@@ -20,7 +20,7 @@ def create_related_profile(sender, instance, created, *args, **kwargs):
...
@@ -20,7 +20,7 @@ def create_related_profile(sender, instance, created, *args, **kwargs):
@receiver
(
post_save
,
sender
=
User
)
@receiver
(
post_save
,
sender
=
User
)
def
send_activation_email
(
sender
,
instance
,
created
,
*
args
,
**
kwargs
):
def
send_activation_email
(
sender
,
instance
,
created
,
*
args
,
**
kwargs
):
# send an e-mail to the user
# send an e-mail to the user
if
instance
and
created
:
if
instance
and
(
created
or
not
instance
.
is_active
)
:
context
=
{
context
=
{
'
current_user
'
:
instance
,
'
current_user
'
:
instance
,
'
username
'
:
instance
.
username
,
'
username
'
:
instance
.
username
,
...
...
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