From 8f01756c6c47d9a71c73206d9839ec53cea7b8fd Mon Sep 17 00:00:00 2001 From: Greg Price Date: Wed, 5 Mar 2014 14:09:27 -0500 Subject: [PATCH] Include preferences in users returned by user API --- common/djangoapps/user_api/models.py | 2 +- common/djangoapps/user_api/serializers.py | 6 +++++- common/djangoapps/user_api/tests/test_views.py | 16 +++++++++++++++- common/djangoapps/user_api/views.py | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/common/djangoapps/user_api/models.py b/common/djangoapps/user_api/models.py index 77ab4c6c95..ab34de001c 100644 --- a/common/djangoapps/user_api/models.py +++ b/common/djangoapps/user_api/models.py @@ -4,7 +4,7 @@ from django.db import models class UserPreference(models.Model): """A user's preference, stored as generic text to be processed by client""" - user = models.ForeignKey(User, db_index=True, related_name="+") + user = models.ForeignKey(User, db_index=True, related_name="preferences") key = models.CharField(max_length=255, db_index=True) value = models.TextField() diff --git a/common/djangoapps/user_api/serializers.py b/common/djangoapps/user_api/serializers.py index 8822817933..edd9b1e7cc 100644 --- a/common/djangoapps/user_api/serializers.py +++ b/common/djangoapps/user_api/serializers.py @@ -6,15 +6,19 @@ from user_api.models import UserPreference class UserSerializer(serializers.HyperlinkedModelSerializer): name = serializers.SerializerMethodField("get_name") + preferences = serializers.SerializerMethodField("get_preferences") def get_name(self, user): profile = UserProfile.objects.get(user=user) return profile.name + def get_preferences(self, user): + return dict([(pref.key, pref.value) for pref in user.preferences.all()]) + class Meta: model = User # This list is the minimal set required by the notification service - fields = ("id", "email", "name", "username") + fields = ("id", "email", "name", "username", "preferences") read_only_fields = ("id", "email", "username") diff --git a/common/djangoapps/user_api/tests/test_views.py b/common/djangoapps/user_api/tests/test_views.py index 4143a467d7..7308e2f0cf 100644 --- a/common/djangoapps/user_api/tests/test_views.py +++ b/common/djangoapps/user_api/tests/test_views.py @@ -66,7 +66,11 @@ class ApiTestCase(TestCase): def assertUserIsValid(self, user): """Assert that the given user result is valid""" - self.assertItemsEqual(user.keys(), ["email", "id", "name", "username", "url"]) + self.assertItemsEqual(user.keys(), ["email", "id", "name", "username", "preferences", "url"]) + self.assertItemsEqual( + user["preferences"].items(), + [(pref.key, pref.value) for pref in self.prefs if pref.user.id == user["id"]] + ) self.assertSelfReferential(user) def assertPrefIsValid(self, pref): @@ -221,6 +225,11 @@ class UserViewSetTest(UserApiTestCase): "id": user.id, "name": user.profile.name, "username": user.username, + "preferences": dict([ + (user_pref.key, user_pref.value) + for user_pref in self.prefs + if user_pref.user == user + ]), "url": uri } ) @@ -352,6 +361,11 @@ class UserPreferenceViewSetTest(UserApiTestCase): "id": pref.user.id, "name": pref.user.profile.name, "username": pref.user.username, + "preferences": dict([ + (user_pref.key, user_pref.value) + for user_pref in self.prefs + if user_pref.user == pref.user + ]), "url": self.get_uri_for_user(pref.user), }, "key": pref.key, diff --git a/common/djangoapps/user_api/views.py b/common/djangoapps/user_api/views.py index 23066920d8..c9a6920f4e 100644 --- a/common/djangoapps/user_api/views.py +++ b/common/djangoapps/user_api/views.py @@ -28,7 +28,7 @@ class ApiKeyHeaderPermission(permissions.BasePermission): class UserViewSet(viewsets.ReadOnlyModelViewSet): authentication_classes = (authentication.SessionAuthentication,) permission_classes = (ApiKeyHeaderPermission,) - queryset = User.objects.all() + queryset = User.objects.all().prefetch_related("preferences") serializer_class = UserSerializer paginate_by = 10 paginate_by_param = "page_size"