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
58813701
Commit
58813701
authored
Dec 12, 2017
by
florimondmanca
Browse files
Options
Downloads
Patches
Plain Diff
improve users API
parent
5a27816c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
oser_cs/api/serializers/users.py
+34
-11
34 additions, 11 deletions
oser_cs/api/serializers/users.py
oser_cs/api/tests/test_users_api.py
+19
-16
19 additions, 16 deletions
oser_cs/api/tests/test_users_api.py
oser_cs/api/views/users.py
+7
-9
7 additions, 9 deletions
oser_cs/api/views/users.py
with
60 additions
and
36 deletions
oser_cs/api/serializers/users.py
+
34
−
11
View file @
58813701
...
...
@@ -4,18 +4,13 @@ from rest_framework import serializers
from
users.models
import
User
class
UserSerializer
(
serializers
.
ModelSerializer
):
"""
Serializer for User.
"""
class
UserSerializer
(
serializers
.
Hyperlinked
ModelSerializer
):
"""
Serializer for User.
date_of_birth
=
serializers
.
DateField
()
password
=
serializers
.
CharField
(
write_only
=
True
)
Actions: list, retrieve, update, partial_update, delete
"""
def
create
(
self
,
validated_data
):
password
=
validated_data
.
pop
(
'
password
'
)
user
=
User
.
objects
.
create
(
**
validated_data
)
user
.
set_password
(
password
)
user
.
save
()
return
user
date_of_birth
=
serializers
.
DateField
()
def
update
(
self
,
instance
,
validated_data
):
for
field
in
validated_data
:
...
...
@@ -28,6 +23,34 @@ class UserSerializer(serializers.ModelSerializer):
class
Meta
:
# noqa
model
=
User
fields
=
(
'
id
'
,
'
emai
l
'
,
'
password
'
,
fields
=
(
'
id
'
,
'
ur
l
'
,
'
email
'
,
'
first_name
'
,
'
last_name
'
,
'
phone_number
'
,
'
date_of_birth
'
,)
extra_kwargs
=
{
'
url
'
:
{
'
view_name
'
:
'
api:user-detail
'
},
'
email
'
:
{
'
read_only
'
:
True
},
}
class
UserCreateSerializer
(
UserSerializer
):
"""
Serializer for creating users.
Actions: create
"""
date_of_birth
=
serializers
.
DateField
()
password
=
serializers
.
CharField
(
write_only
=
True
)
def
create
(
self
,
validated_data
):
password
=
validated_data
.
pop
(
'
password
'
)
user
=
User
.
objects
.
create
(
**
validated_data
)
user
.
set_password
(
password
)
user
.
save
()
return
user
class
Meta
(
UserSerializer
.
Meta
):
# noqa
fields
=
(
*
UserSerializer
.
Meta
.
fields
,
'
password
'
)
extra_kwargs
=
{
**
UserSerializer
.
Meta
.
extra_kwargs
,
'
email
'
:
{
'
read_only
'
:
False
},
}
This diff is collapsed.
Click to expand it.
oser_cs/api/tests/test_users_api.py
+
19
−
16
View file @
58813701
...
...
@@ -48,19 +48,19 @@ class UserAPITest(ModelAPITestCase):
url
=
f
'
/api/users/
{
obj
.
pk
}
/
'
response
=
self
.
client
.
get
(
url
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
data
=
response
.
data
self
.
assertEqual
(
obj
.
email
,
data
[
'
email
'
])
self
.
assertEqual
(
obj
.
first_name
,
data
[
'
first_name
'
])
self
.
assertEqual
(
obj
.
last_name
,
data
[
'
last_name
'
])
self
.
assertEqual
(
obj
.
phone_number
,
data
[
'
phone_number
'
])
date_of_birth
=
date_format
(
obj
.
date_of_birth
,
'
d/m/Y
'
)
self
.
assertEqual
(
date_of_birth
,
data
[
'
date_of_birth
'
])
keys
=
(
'
id
'
,
'
url
'
,
'
email
'
,
'
first_name
'
,
'
last_name
'
,
'
phone_number
'
,
'
date_of_birth
'
,
)
for
key
in
keys
:
self
.
assertIn
(
key
,
response
.
data
)
def
test_create
(
self
):
# data = self.create_data()
# data['password'] = 'hello25'
url
=
'
/api/users/
'
data
=
{
'
email
'
:
'
john.doe@example.net
'
,
...
...
@@ -75,8 +75,11 @@ class UserAPITest(ModelAPITestCase):
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
User
.
objects
.
count
(),
1
)
user
=
User
.
objects
.
get
()
special_fields
=
(
'
password
'
,
'
date_of_birth
'
)
for
field
in
filter
(
lambda
f
:
f
not
in
special_fields
,
data
):
self
.
assertEqual
(
getattr
(
user
,
field
),
data
[
field
])
for
key
in
data
:
if
key
==
'
password
'
:
continue
elif
key
==
'
date_of_birth
'
:
date_of_birth
=
date_format
(
user
.
date_of_birth
,
'
d/m/Y
'
)
self
.
assertEqual
(
date_of_birth
,
data
[
'
date_of_birth
'
])
continue
self
.
assertEqual
(
getattr
(
user
,
key
),
data
[
key
])
This diff is collapsed.
Click to expand it.
oser_cs/api/views/users.py
+
7
−
9
View file @
58813701
...
...
@@ -2,20 +2,14 @@
from
django.contrib.auth
import
get_user_model
from
rest_framework
import
viewsets
from
rest_framework.mixins
import
(
ListModelMixin
,
RetrieveModelMixin
,
CreateModelMixin
,
)
from
api.serializers.users
import
UserSerializer
from
api.serializers.users
import
UserSerializer
,
UserCreateSerializer
# Create your views here.
User
=
get_user_model
()
class
UserViewSet
(
ListModelMixin
,
RetrieveModelMixin
,
CreateModelMixin
,
viewsets
.
GenericViewSet
):
class
UserViewSet
(
viewsets
.
ModelViewSet
):
"""
API endpoint that allows users to be viewed or edited.
retrieve:
...
...
@@ -29,4 +23,8 @@ class UserViewSet(ListModelMixin,
"""
queryset
=
User
.
objects
.
all
()
serializer_class
=
UserSerializer
def
get_serializer_class
(
self
):
if
self
.
action
==
'
create
'
:
return
UserCreateSerializer
return
UserSerializer
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