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
16603cc2
Commit
16603cc2
authored
Jun 13, 2014
by
Matt Drayer
Committed by
Jonathan Piacenti
Aug 20, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mattdrayer/api-organizationsusers: Added support for adding users to an organization
parent
d2154763
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
2 deletions
+106
-2
lms/djangoapps/api_manager/organizations/serializers.py
+10
-0
lms/djangoapps/api_manager/organizations/tests.py
+64
-0
lms/djangoapps/api_manager/organizations/views.py
+32
-2
No files found.
lms/djangoapps/api_manager/organizations/serializers.py
View file @
16603cc2
""" Django REST Framework Serializers """
from
django.contrib.auth.models
import
User
from
rest_framework
import
serializers
from
api_manager.models
import
Organization
class
UserSerializer
(
serializers
.
HyperlinkedModelSerializer
):
""" Serializer for model interactions """
class
Meta
:
""" Meta class for defining additional serializer characteristics """
model
=
User
fields
=
(
'id'
,
'url'
,
'username'
,
'email'
)
class
OrganizationSerializer
(
serializers
.
ModelSerializer
):
""" Serializer for Organization model interactions """
url
=
serializers
.
HyperlinkedIdentityField
(
view_name
=
'organization-detail'
)
...
...
lms/djangoapps/api_manager/organizations/tests.py
View file @
16603cc2
...
...
@@ -7,6 +7,7 @@ Run these tests @ Devstack:
import
json
import
uuid
from
django.contrib.auth.models
import
User
from
django.core.cache
import
cache
from
django.test
import
TestCase
,
Client
from
django.test.utils
import
override_settings
...
...
@@ -40,6 +41,13 @@ class OrganizationsApiTests(TestCase):
self
.
test_organization_contact_email
=
'john@test.org'
self
.
test_organization_contact_phone
=
'+1 332 232 24234'
self
.
test_user_email
=
str
(
uuid
.
uuid4
())
self
.
test_user_username
=
str
(
uuid
.
uuid4
())
self
.
test_user
=
User
.
objects
.
create
(
email
=
self
.
test_user_email
,
username
=
self
.
test_user_username
)
self
.
client
=
SecureClient
()
cache
.
clear
()
...
...
@@ -189,3 +197,59 @@ class OrganizationsApiTests(TestCase):
response
=
self
.
do_post
(
self
.
test_organizations_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
self
.
assertEqual
(
len
(
response
.
data
[
'groups'
]),
len
(
groups
))
def
test_organizations_users_post
(
self
):
data
=
{
'name'
:
self
.
test_organization_name
,
'display_name'
:
self
.
test_organization_display_name
,
'contact_name'
:
self
.
test_organization_contact_name
,
'contact_email'
:
self
.
test_organization_contact_email
,
'contact_phone'
:
self
.
test_organization_contact_phone
}
response
=
self
.
do_post
(
self
.
test_organizations_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
test_uri
=
'{}{}/'
.
format
(
self
.
test_organizations_uri
,
str
(
response
.
data
[
'id'
]))
users_uri
=
'{}users/'
.
format
(
test_uri
)
data
=
{
"id"
:
self
.
test_user
.
id
}
response
=
self
.
do_post
(
users_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
response
=
self
.
do_get
(
test_uri
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
data
[
'users'
][
0
],
self
.
test_user
.
id
)
def
test_organizations_users_post_invalid_user
(
self
):
data
=
{
'name'
:
self
.
test_organization_name
,
'display_name'
:
self
.
test_organization_display_name
,
'contact_name'
:
self
.
test_organization_contact_name
,
'contact_email'
:
self
.
test_organization_contact_email
,
'contact_phone'
:
self
.
test_organization_contact_phone
}
response
=
self
.
do_post
(
self
.
test_organizations_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
test_uri
=
'{}{}/'
.
format
(
self
.
test_organizations_uri
,
str
(
response
.
data
[
'id'
]))
users_uri
=
'{}users/'
.
format
(
test_uri
)
data
=
{
"id"
:
123456
}
response
=
self
.
do_post
(
users_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
400
)
def
test_organizations_users_get
(
self
):
data
=
{
'name'
:
self
.
test_organization_name
,
'display_name'
:
self
.
test_organization_display_name
,
'contact_name'
:
self
.
test_organization_contact_name
,
'contact_email'
:
self
.
test_organization_contact_email
,
'contact_phone'
:
self
.
test_organization_contact_phone
}
response
=
self
.
do_post
(
self
.
test_organizations_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
test_uri
=
'{}{}/'
.
format
(
self
.
test_organizations_uri
,
str
(
response
.
data
[
'id'
]))
users_uri
=
'{}users/'
.
format
(
test_uri
)
data
=
{
"id"
:
self
.
test_user
.
id
}
response
=
self
.
do_post
(
users_uri
,
data
)
self
.
assertEqual
(
response
.
status_code
,
201
)
response
=
self
.
do_get
(
users_uri
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
data
[
0
][
'id'
],
self
.
test_user
.
id
)
self
.
assertEqual
(
response
.
data
[
0
][
'username'
],
self
.
test_user
.
username
)
self
.
assertEqual
(
response
.
data
[
0
][
'email'
],
self
.
test_user
.
email
)
lms/djangoapps/api_manager/organizations/views.py
View file @
16603cc2
""" ORGANIZATIONS API VIEWS """
from
rest_framework
import
viewsets
from
django.contrib.auth.models
import
User
from
django.core.exceptions
import
ObjectDoesNotExist
from
rest_framework
import
status
,
viewsets
from
rest_framework.decorators
import
action
from
rest_framework.response
import
Response
from
api_manager.models
import
Organization
from
.serializers
import
OrganizationSerializer
from
.serializers
import
OrganizationSerializer
,
UserSerializer
class
OrganizationsViewSet
(
viewsets
.
ModelViewSet
):
...
...
@@ -12,3 +17,28 @@ class OrganizationsViewSet(viewsets.ModelViewSet):
"""
serializer_class
=
OrganizationSerializer
model
=
Organization
@action
(
methods
=
[
'get'
,
'post'
])
def
users
(
self
,
request
,
pk
):
"""
Add a User to an Organization
"""
if
request
.
method
==
'GET'
:
users
=
User
.
objects
.
filter
(
organizations
=
pk
)
response_data
=
[]
if
users
:
for
user
in
users
:
serializer
=
UserSerializer
(
user
)
response_data
.
append
(
serializer
.
data
)
return
Response
(
response_data
,
status
=
status
.
HTTP_200_OK
)
else
:
user_id
=
request
.
DATA
.
get
(
'id'
)
try
:
user
=
User
.
objects
.
get
(
id
=
user_id
)
except
ObjectDoesNotExist
:
message
=
'User {} does not exist'
.
format
(
user_id
)
return
Response
({
"detail"
:
message
},
status
.
HTTP_400_BAD_REQUEST
)
organization
=
self
.
get_object
()
organization
.
users
.
add
(
user
)
organization
.
save
()
return
Response
({},
status
=
status
.
HTTP_201_CREATED
)
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