Commit 8f01756c by Greg Price

Include preferences in users returned by user API

parent 601b5a8c
...@@ -4,7 +4,7 @@ from django.db import models ...@@ -4,7 +4,7 @@ from django.db import models
class UserPreference(models.Model): class UserPreference(models.Model):
"""A user's preference, stored as generic text to be processed by client""" """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) key = models.CharField(max_length=255, db_index=True)
value = models.TextField() value = models.TextField()
......
...@@ -6,15 +6,19 @@ from user_api.models import UserPreference ...@@ -6,15 +6,19 @@ from user_api.models import UserPreference
class UserSerializer(serializers.HyperlinkedModelSerializer): class UserSerializer(serializers.HyperlinkedModelSerializer):
name = serializers.SerializerMethodField("get_name") name = serializers.SerializerMethodField("get_name")
preferences = serializers.SerializerMethodField("get_preferences")
def get_name(self, user): def get_name(self, user):
profile = UserProfile.objects.get(user=user) profile = UserProfile.objects.get(user=user)
return profile.name return profile.name
def get_preferences(self, user):
return dict([(pref.key, pref.value) for pref in user.preferences.all()])
class Meta: class Meta:
model = User model = User
# This list is the minimal set required by the notification service # 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") read_only_fields = ("id", "email", "username")
......
...@@ -66,7 +66,11 @@ class ApiTestCase(TestCase): ...@@ -66,7 +66,11 @@ class ApiTestCase(TestCase):
def assertUserIsValid(self, user): def assertUserIsValid(self, user):
"""Assert that the given user result is valid""" """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) self.assertSelfReferential(user)
def assertPrefIsValid(self, pref): def assertPrefIsValid(self, pref):
...@@ -221,6 +225,11 @@ class UserViewSetTest(UserApiTestCase): ...@@ -221,6 +225,11 @@ class UserViewSetTest(UserApiTestCase):
"id": user.id, "id": user.id,
"name": user.profile.name, "name": user.profile.name,
"username": user.username, "username": user.username,
"preferences": dict([
(user_pref.key, user_pref.value)
for user_pref in self.prefs
if user_pref.user == user
]),
"url": uri "url": uri
} }
) )
...@@ -352,6 +361,11 @@ class UserPreferenceViewSetTest(UserApiTestCase): ...@@ -352,6 +361,11 @@ class UserPreferenceViewSetTest(UserApiTestCase):
"id": pref.user.id, "id": pref.user.id,
"name": pref.user.profile.name, "name": pref.user.profile.name,
"username": pref.user.username, "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), "url": self.get_uri_for_user(pref.user),
}, },
"key": pref.key, "key": pref.key,
......
...@@ -28,7 +28,7 @@ class ApiKeyHeaderPermission(permissions.BasePermission): ...@@ -28,7 +28,7 @@ class ApiKeyHeaderPermission(permissions.BasePermission):
class UserViewSet(viewsets.ReadOnlyModelViewSet): class UserViewSet(viewsets.ReadOnlyModelViewSet):
authentication_classes = (authentication.SessionAuthentication,) authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (ApiKeyHeaderPermission,) permission_classes = (ApiKeyHeaderPermission,)
queryset = User.objects.all() queryset = User.objects.all().prefetch_related("preferences")
serializer_class = UserSerializer serializer_class = UserSerializer
paginate_by = 10 paginate_by = 10
paginate_by_param = "page_size" paginate_by_param = "page_size"
......
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