Commit 64a976ab by Zia Fazal Committed by Jonathan Piacenti

new api to get list of workgroups by group

parent e9d2731d
...@@ -9,13 +9,13 @@ import uuid ...@@ -9,13 +9,13 @@ import uuid
import json import json
from django.core.cache import cache from django.core.cache import cache
from django.test import TestCase, Client from django.test import Client
from django.test.utils import override_settings from django.test.utils import override_settings
from api_manager.models import GroupRelationship, GroupProfile
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from api_manager.models import GroupRelationship, GroupProfile, Organization from api_manager.models import GroupRelationship, GroupProfile, Organization
from xmodule.modulestore.tests.factories import CourseFactory from projects.models import Project
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
TEST_API_KEY = str(uuid.uuid4()) TEST_API_KEY = str(uuid.uuid4())
...@@ -43,8 +43,16 @@ class GroupsApiTests(ModuleStoreTestCase): ...@@ -43,8 +43,16 @@ class GroupsApiTests(ModuleStoreTestCase):
self.base_users_uri = '/api/users' self.base_users_uri = '/api/users'
self.base_groups_uri = '/api/groups' self.base_groups_uri = '/api/groups'
self.test_course_data = '<html>{}</html>'.format(str(uuid.uuid4()))
self.course = CourseFactory.create() self.course = CourseFactory.create()
self.test_course_id = self.course.id self.test_course_id = self.course.id
self.course_content = ItemFactory.create(
category="videosequence",
parent_location=self.course.location,
data=self.test_course_data,
due="2016-05-16T14:30:00Z",
display_name="View_Sequence"
)
self.test_organization = Organization.objects.create( self.test_organization = Organization.objects.create(
name="Test Organization", name="Test Organization",
...@@ -54,6 +62,11 @@ class GroupsApiTests(ModuleStoreTestCase): ...@@ -54,6 +62,11 @@ class GroupsApiTests(ModuleStoreTestCase):
contact_phone = '+1 332 232 24234' contact_phone = '+1 332 232 24234'
) )
self.test_project = Project.objects.create(
course_id=self.course.id,
content_id=self.course_content.id
)
self.client = SecureClient() self.client = SecureClient()
cache.clear() cache.clear()
...@@ -939,3 +952,41 @@ class GroupsApiTests(ModuleStoreTestCase): ...@@ -939,3 +952,41 @@ class GroupsApiTests(ModuleStoreTestCase):
test_uri = self.base_groups_uri + '/1231241/organizations/' test_uri = self.base_groups_uri + '/1231241/organizations/'
response = self.do_get(test_uri) response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
def test_groups_workgroups_list(self):
data = {'name': self.test_group_name, 'type': 'test'}
response = self.do_post(self.base_groups_uri, data)
self.assertEqual(response.status_code, 201)
group_id = response.data['id']
test_workgroups_uri = '/api/workgroups/'
for i in xrange(1, 12):
project_id = self.test_project.id
data = {
'name': 'Workgroup ' + str(i),
'project': project_id
}
response = self.do_post(test_workgroups_uri, data)
self.assertEqual(response.status_code, 201)
test_uri = '{}{}/'.format(test_workgroups_uri, str(response.data['id']))
users_uri = '{}groups/'.format(test_uri)
data = {"id": group_id}
response = self.do_post(users_uri, data)
self.assertEqual(response.status_code, 201)
# test to get list of workgroups
test_uri = '/api/groups/{}/workgroups/?page_size=10'.format(group_id)
response = self.do_get(test_uri)
self.assertEqual(response.data['count'], 11)
self.assertEqual(len(response.data['results']), 10)
self.assertEqual(response.data['num_pages'], 2)
# test with course_id filter
response = self.do_get('/api/groups/{}/workgroups/?course_id={}'.format(group_id, self.course.id))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], 11)
self.assertIsNotNone(response.data['results'][0]['name'])
self.assertIsNotNone(response.data['results'][0]['project'])
# test with invalid group id
response = self.do_get('/api/groups/4356340/workgroups/')
self.assertEqual(response.status_code, 404)
...@@ -12,6 +12,7 @@ urlpatterns = patterns( ...@@ -12,6 +12,7 @@ urlpatterns = patterns(
url(r'^(?P<group_id>[0-9]+)/courses/*$', groups_views.GroupsCoursesList.as_view()), url(r'^(?P<group_id>[0-9]+)/courses/*$', groups_views.GroupsCoursesList.as_view()),
url(r'^(?P<group_id>[0-9]+)/courses/(?P<course_id>[a-zA-Z0-9/_:]+)$', groups_views.GroupsCoursesDetail.as_view()), url(r'^(?P<group_id>[0-9]+)/courses/(?P<course_id>[a-zA-Z0-9/_:]+)$', groups_views.GroupsCoursesDetail.as_view()),
url(r'^(?P<group_id>[0-9]+)/organizations/*$', groups_views.GroupsOrganizationsList.as_view()), url(r'^(?P<group_id>[0-9]+)/organizations/*$', groups_views.GroupsOrganizationsList.as_view()),
url(r'^(?P<group_id>[0-9]+)/workgroups/*$', groups_views.GroupsWorkgroupsList.as_view()),
url(r'^(?P<group_id>[0-9]+)/users/*$', groups_views.GroupsUsersList.as_view()), url(r'^(?P<group_id>[0-9]+)/users/*$', groups_views.GroupsUsersList.as_view()),
url(r'^(?P<group_id>[0-9]+)/users/(?P<user_id>[0-9]+)$', groups_views.GroupsUsersDetail.as_view()), url(r'^(?P<group_id>[0-9]+)/users/(?P<user_id>[0-9]+)$', groups_views.GroupsUsersDetail.as_view()),
url(r'^(?P<group_id>[0-9]+)/groups/*$', groups_views.GroupsGroupsList.as_view()), url(r'^(?P<group_id>[0-9]+)/groups/*$', groups_views.GroupsGroupsList.as_view()),
......
...@@ -5,15 +5,17 @@ from collections import OrderedDict ...@@ -5,15 +5,17 @@ from collections import OrderedDict
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
from django.utils import timezone from django.utils import timezone
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
from api_manager.models import GroupRelationship, CourseGroupRelationship, GroupProfile, APIUser as User from api_manager.models import GroupRelationship, CourseGroupRelationship, GroupProfile, APIUser as User
from api_manager.permissions import SecureAPIView from api_manager.permissions import SecureAPIView, SecureListAPIView
from api_manager.utils import str2bool, generate_base_uri from api_manager.utils import str2bool, generate_base_uri
from api_manager.organizations import serializers from api_manager.organizations import serializers
from projects.serializers import BasicWorkgroupSerializer
from xmodule.modulestore import Location, InvalidLocationError from xmodule.modulestore import Location, InvalidLocationError
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
...@@ -642,3 +644,29 @@ class GroupsOrganizationsList(SecureAPIView): ...@@ -642,3 +644,29 @@ class GroupsOrganizationsList(SecureAPIView):
serializer = serializers.OrganizationSerializer(org) serializer = serializers.OrganizationSerializer(org)
response_data.append(serializer.data) response_data.append(serializer.data)
return Response(response_data, status=status.HTTP_200_OK) return Response(response_data, status=status.HTTP_200_OK)
class GroupsWorkgroupsList(SecureListAPIView):
"""
### The GroupsWorkgroupsList view allows clients to retrieve a list of workgroups a group has
- URI: ```/api/groups/{group_id}/workgroups/```
- GET: Provides paginated list of workgroups for a group
To filter the group's workgroup set by course
GET ```/api/groups/{group_id}/workgroups/?course_id={course_id}```
"""
serializer_class = BasicWorkgroupSerializer
def get_queryset(self):
group_id = self.kwargs['group_id']
course_id = self.request.QUERY_PARAMS.get('course_id', None)
try:
group = Group.objects.get(id=group_id)
except ObjectDoesNotExist:
raise Http404
queryset = group.workgroups.all()
if course_id:
queryset = queryset.filter(project__course_id=course_id)
return queryset
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment