Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
f536c373
Commit
f536c373
authored
Aug 26, 2014
by
Matt Drayer
Committed by
Jonathan Piacenti
Aug 20, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mattdrayer-api-user-avatar-city: Added fields
parent
ee18a8b7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
5 deletions
+42
-5
lms/djangoapps/api_manager/users/serializers.py
+29
-4
lms/djangoapps/api_manager/users/tests.py
+13
-1
No files found.
lms/djangoapps/api_manager/users/serializers.py
View file @
f536c373
""" Django REST Framework Serializers """
from
api_manager.models
import
APIUser
from
rest_framework
import
serializers
from
api_manager.models
import
APIUser
from
api_manager.organizations.serializers
import
BasicOrganizationSerializer
from
student.models
import
UserProfile
class
DynamicFieldsModelSerializer
(
serializers
.
ModelSerializer
):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
# Instantiate the superclass normally
super
(
DynamicFieldsModelSerializer
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
fields
=
self
.
context
[
'request'
]
.
QUERY_PARAMS
.
get
(
'fields'
,
None
)
if
'request'
in
self
.
context
else
None
if
fields
:
fields
=
fields
.
split
(
','
)
# Drop any fields that are not specified in the `fields` argument.
allowed
=
set
(
fields
)
existing
=
set
(
self
.
fields
.
keys
())
for
field_name
in
existing
-
allowed
:
self
.
fields
.
pop
(
field_name
)
class
UserSerializer
(
DynamicFieldsModelSerializer
):
class
UserSerializer
(
serializers
.
ModelSerializer
):
""" Serializer for User model interactions """
organizations
=
BasicOrganizationSerializer
(
many
=
True
,
required
=
False
)
created
=
serializers
.
DateTimeField
(
source
=
'date_joined'
,
required
=
False
)
avatar_url
=
serializers
.
CharField
(
source
=
'profile.avatar_url'
)
city
=
serializers
.
CharField
(
source
=
'profile.city'
)
title
=
serializers
.
CharField
(
source
=
'profile.title'
)
country
=
serializers
.
CharField
(
source
=
'profile.country'
)
full_name
=
serializers
.
CharField
(
source
=
'profile.name'
)
class
Meta
:
""" Serializer/field specification """
model
=
APIUser
fields
=
(
"id"
,
"email"
,
"username"
,
"first_name"
,
"last_name"
,
"created"
,
"is_active"
,
"organizations"
)
fields
=
(
"id"
,
"email"
,
"username"
,
"first_name"
,
"last_name"
,
"created"
,
"is_active"
,
"organizations"
,
"avatar_url"
,
"city"
,
"title"
,
"country"
,
"full_name"
)
read_only_fields
=
(
"id"
,
"email"
,
"username"
)
class
UserCountByCitySerializer
(
serializers
.
Serializer
):
""" Serializer for user count by city """
city
=
serializers
.
CharField
(
source
=
'profile__city'
)
...
...
lms/djangoapps/api_manager/users/tests.py
View file @
f536c373
...
...
@@ -156,7 +156,10 @@ class UsersApiTests(ModuleStoreTestCase):
'username'
:
'test_user{}'
.
format
(
i
),
'password'
:
self
.
test_password
,
'first_name'
:
'John{}'
.
format
(
i
),
'last_name'
:
'Doe{}'
.
format
(
i
)
'last_name'
:
'Doe{}'
.
format
(
i
),
'avatar_url'
:
'http://avatar.com/{}.jpg'
.
format
(
i
),
'city'
:
'Boston'
,
'title'
:
"The King"
,
}
response
=
self
.
do_post
(
test_uri
,
data
)
...
...
@@ -210,6 +213,15 @@ class UsersApiTests(ModuleStoreTestCase):
response
=
self
.
do_get
(
'{}?email={}'
.
format
(
test_uri
,
'john@example.com'
))
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
len
(
response
.
data
[
'results'
]),
0
)
# add some additional fields and filter the response to only these fields
response
=
self
.
do_get
(
'{}?email=test2@example.com&fields=avatar_url,city,title'
.
format
(
test_uri
))
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
len
(
response
.
data
[
'results'
]),
1
)
self
.
assertEqual
(
response
.
data
[
'results'
][
0
][
'avatar_url'
],
'http://avatar.com/2.jpg'
)
self
.
assertEqual
(
response
.
data
[
'results'
][
0
][
'city'
],
'Boston'
)
self
.
assertEqual
(
response
.
data
[
'results'
][
0
][
'title'
],
'The King'
)
if
'id'
in
response
.
data
[
'results'
][
0
]:
self
.
fail
(
"Dynamic field filtering error in UserSerializer"
)
def
test_user_list_get_with_org_filter
(
self
):
test_uri
=
self
.
users_base_uri
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment