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
8904f880
Commit
8904f880
authored
Apr 23, 2014
by
chrisndodge
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #23 from edx-solutions/api
add GET endpoint to group/user relationships
parents
fef27cb1
7d3cf70a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
11 deletions
+45
-11
lms/djangoapps/api_manager/groups_views.py
+27
-10
lms/djangoapps/api_manager/tests/test_groups_views.py
+18
-1
No files found.
lms/djangoapps/api_manager/groups_views.py
View file @
8904f880
...
...
@@ -144,23 +144,41 @@ def group_detail(request, group_id):
return
Response
({})
@api_view
([
'POST'
])
@api_view
([
'
GET'
,
'
POST'
])
@permission_classes
((
ApiKeyHeaderPermission
,))
def
group_users_list
(
request
,
group_id
):
"""
POST creates a new group-user relationship in the system
"""
response_data
=
{}
group_id
=
group_id
user_id
=
request
.
DATA
[
'user_id'
]
base_uri
=
_generate_base_uri
(
request
)
try
:
existing_group
=
Group
.
objects
.
get
(
id
=
group_id
)
existing_user
=
User
.
objects
.
get
(
id
=
user_id
)
except
ObjectDoesNotExist
:
existing_group
=
None
existing_user
=
None
if
existing_group
and
existing_user
:
return
Response
({},
status
.
HTTP_404_NOT_FOUND
)
if
request
.
method
==
"GET"
:
users
=
existing_group
.
user_set
.
all
()
response_data
[
'users'
]
=
[]
for
user
in
users
:
user_data
=
{}
user_data
[
'id'
]
=
user
.
id
user_data
[
'email'
]
=
user
.
email
user_data
[
'username'
]
=
user
.
username
user_data
[
'first_name'
]
=
user
.
first_name
user_data
[
'last_name'
]
=
user
.
last_name
response_data
[
'users'
]
.
append
(
user_data
)
response_status
=
status
.
HTTP_200_OK
elif
request
.
method
==
"POST"
:
user_id
=
request
.
DATA
[
'user_id'
]
base_uri
=
_generate_base_uri
(
request
)
try
:
existing_user
=
User
.
objects
.
get
(
id
=
user_id
)
except
ObjectDoesNotExist
:
return
Response
({},
status
.
HTTP_404_NOT_FOUND
)
try
:
existing_relationship
=
Group
.
objects
.
get
(
user
=
existing_user
)
except
ObjectDoesNotExist
:
...
...
@@ -175,8 +193,7 @@ def group_users_list(request, group_id):
response_data
[
'uri'
]
=
'{}/{}'
.
format
(
base_uri
,
existing_user
.
id
)
response_data
[
'message'
]
=
"Relationship already exists."
response_status
=
status
.
HTTP_409_CONFLICT
else
:
response_status
=
status
.
HTTP_404_NOT_FOUND
return
Response
(
response_data
,
status
=
response_status
)
...
...
lms/djangoapps/api_manager/tests/test_groups_views.py
View file @
8904f880
...
...
@@ -182,7 +182,13 @@ class GroupsApiTests(TestCase):
def
test_group_users_list_post
(
self
):
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
}
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
,
'first_name'
:
'Joe'
,
'last_name'
:
'Smith'
}
response
=
self
.
do_post
(
self
.
base_users_uri
,
data
)
user_id
=
response
.
data
[
'id'
]
data
=
{
'name'
:
'Alpha Group'
}
...
...
@@ -199,6 +205,17 @@ class GroupsApiTests(TestCase):
self
.
assertEqual
(
response
.
data
[
'group_id'
],
str
(
group_id
))
self
.
assertEqual
(
response
.
data
[
'user_id'
],
str
(
user_id
))
# check to see if the user is listed after we associate it with the group
response
=
self
.
do_get
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
200
)
users
=
response
.
data
[
'users'
]
self
.
assertEqual
(
len
(
users
),
1
)
self
.
assertEqual
(
users
[
0
][
'id'
],
user_id
)
self
.
assertEqual
(
users
[
0
][
'username'
],
local_username
)
self
.
assertEqual
(
users
[
0
][
'email'
],
self
.
test_email
)
self
.
assertEqual
(
users
[
0
][
'first_name'
],
'Joe'
)
self
.
assertEqual
(
users
[
0
][
'last_name'
],
'Smith'
)
def
test_group_users_list_post_duplicate
(
self
):
local_username
=
self
.
test_username
+
str
(
randint
(
11
,
99
))
data
=
{
'email'
:
self
.
test_email
,
'username'
:
local_username
,
'password'
:
self
.
test_password
}
...
...
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