Skip to content

Commit

Permalink
Merge pull request openedx#132 from edx-solutions/mattdrayer/api-user…
Browse files Browse the repository at this point in the history
…-preferences-delete

mattdrayer/api-user-preferences-delete: Added new Detail view, GET/DELET...
  • Loading branch information
mattdrayer committed Jul 16, 2014
2 parents 34af8e8 + 460bbbf commit 5ada4d3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
45 changes: 39 additions & 6 deletions lms/djangoapps/api_manager/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,12 +903,12 @@ def test_user_course_grades_user_not_found(self):
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)

def test_get_user_preferences_user_not_found(self):
def test_user_preferences_user_list_get_not_found(self):
test_uri = '/api/users/{}/preferences'.format('999999')
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)

def test_get_user_preferences_default(self):
def test_user_preferences_list_get_default(self):
# By default newly created users will have one initial preference settings:
# 'pref-lang' = 'en'
user_id = self._create_test_user()
Expand All @@ -918,12 +918,12 @@ def test_get_user_preferences_default(self):
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data['pref-lang'], 'en')

def test_post_user_preferences_user_not_found(self):
def test_user_preferences_list_post_user_not_found(self):
test_uri = '/api/users/{}/preferences'.format('999999')
response = self.do_post(test_uri, {"foo": "bar"})
self.assertEqual(response.status_code, 404)

def test_post_user_preferences_bad_request(self):
def test_user_preferences_list_post_bad_request(self):
user_id = self._create_test_user()
test_uri = '/api/users/{}/preferences'.format(user_id)
response = self.do_post(test_uri, {})
Expand All @@ -938,7 +938,7 @@ def test_post_user_preferences_bad_request(self):
response = self.do_post(test_uri, {"a_boolean": False})
self.assertEqual(response.status_code, 400)

def test_post_user_preferences(self):
def test_user_preferences_list_post(self):
user_id = self._create_test_user()
test_uri = '/api/users/{}/preferences'.format(user_id)
response = self.do_post(test_uri, {"foo": "bar"})
Expand All @@ -949,7 +949,7 @@ def test_post_user_preferences(self):
self.assertEqual(response.data['pref-lang'], 'en')
self.assertEqual(response.data['foo'], 'bar')

def test_update_user_preferences(self):
def test_user_preferences_list_update(self):
user_id = self._create_test_user()
test_uri = '/api/users/{}/preferences'.format(user_id)
response = self.do_post(test_uri, {"foo": "bar"})
Expand All @@ -962,6 +962,39 @@ def test_update_user_preferences(self):
self.assertEqual(response.data['pref-lang'], 'en')
self.assertEqual(response.data['foo'], 'updated')

def test_user_preferences_detail_get(self):
user_id = self._create_test_user()
test_uri = '/api/users/{}/preferences'.format(user_id)
response = self.do_post(test_uri, {"foo": "bar"})
self.assertEqual(response.status_code, 201)
test_uri = '{}/{}'.format(test_uri, 'foo')
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['foo'], 'bar')

def test_user_preferences_detail_get_invalid_user(self):
test_uri = '/api/users/12345/preferences/foo'
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)

def test_user_preferences_detail_delete(self):
user_id = self._create_test_user()
test_uri = '/api/users/{}/preferences'.format(user_id)
response = self.do_post(test_uri, {"foo": "bar"})
self.assertEqual(response.status_code, 201)
test_uri = '{}/{}'.format(test_uri, 'foo')
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 200)
response = self.do_delete(test_uri)
self.assertEqual(response.status_code, 204)
response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404)

def test_user_preferences_detail_delete_invalid_user(self):
test_uri = '/api/users/12345/preferences/foo'
response = self.do_delete(test_uri)
self.assertEqual(response.status_code, 404)

def test_course_grades(self):
test_uri = '/api/users'
local_username = self.test_username + str(randint(11, 99))
Expand Down
1 change: 1 addition & 0 deletions lms/djangoapps/api_manager/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
url(r'^(?P<user_id>[a-zA-Z0-9]+)/groups/*$', users_views.UsersGroupsList.as_view(), name='users-groups-list'),
url(r'^(?P<user_id>[a-zA-Z0-9]+)/groups/(?P<group_id>[0-9]+)$', users_views.UsersGroupsDetail.as_view(), name='users-groups-detail'),
url(r'^(?P<user_id>[a-zA-Z0-9]+)/preferences$', users_views.UsersPreferences.as_view(), name='users-preferences-list'),
url(r'^(?P<user_id>[a-zA-Z0-9]+)/preferences/(?P<preference_id>[a-zA-Z0-9_]+)$', users_views.UsersPreferencesDetail.as_view(), name='users-preferences-detail'),
url(r'^(?P<user_id>[a-zA-Z0-9]+)/organizations/$', users_views.UsersOrganizationsList.as_view(), name='users-organizations-list'),
url(r'^(?P<user_id>[a-zA-Z0-9]+)/workgroups/$', users_views.UsersWorkgroupsList.as_view(), name='users-workgroups-list'),
)
Expand Down
44 changes: 41 additions & 3 deletions lms/djangoapps/api_manager/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,16 +863,16 @@ def post(self, request, user_id):
try:
user = User.objects.get(id=user_id)
except ObjectDoesNotExist:
return Response({}, status.HTTP_404_NOT_FOUND)
return Response({}, status=status.HTTP_404_NOT_FOUND)

if not len(request.DATA):
return Response({}, status.HTTP_400_BAD_REQUEST)
return Response({}, status=status.HTTP_400_BAD_REQUEST)

# do a quick inspection to make sure we're only getting strings as values
for key in request.DATA.keys():
value = request.DATA[key]
if not isinstance(value, basestring):
return Response({}, status.HTTP_400_BAD_REQUEST)
return Response({}, status=status.HTTP_400_BAD_REQUEST)

status_code = status.HTTP_200_OK
for key in request.DATA.keys():
Expand All @@ -895,6 +895,44 @@ def post(self, request, user_id):

return Response({}, status_code)

class UsersPreferencesDetail(SecureAPIView):
"""
### The UsersPreferencesDetail view allows clients to interact with the User's preferences
- URI: ```/api/users/{user_id}/preferences/{preference_id}```
- DELETE: Removes the specified preference from the user's record
### Use Cases/Notes:
* Use DELETE to remove the last-visited course for a user (for example)
"""

def get(self, request, user_id, preference_id): # pylint: disable=W0613
"""
GET returns the specified preference for the specified user
"""
try:
user = User.objects.get(id=user_id)
except ObjectDoesNotExist:
return Response({}, status.HTTP_404_NOT_FOUND)
response_data = {}
try:
response_data[preference_id] = user.preferences.get(key=preference_id).value
except ObjectDoesNotExist:
return Response({}, status=status.HTTP_404_NOT_FOUND)
return Response(response_data)

def delete(self, request, user_id, preference_id): # pylint: disable=W0613
"""
DELETE /api/users/{user_id}/preferences/{preference_id}
"""
try:
user = User.objects.get(id=user_id)
except ObjectDoesNotExist:
return Response({}, status.HTTP_404_NOT_FOUND)
for preference in user.preferences.all():
if preference.key == preference_id:
UserPreference.objects.get(user_id=user_id, key=preference.key).delete()
break
return Response({}, status=status.HTTP_204_NO_CONTENT)


class UsersOrganizationsList(SecureListAPIView):
"""
Expand Down

0 comments on commit 5ada4d3

Please sign in to comment.