Commit f536c373 by Matt Drayer Committed by Jonathan Piacenti

mattdrayer-api-user-avatar-city: Added fields

parent ee18a8b7
""" Django REST Framework Serializers """
from api_manager.models import APIUser
from rest_framework import serializers
from api_manager.models import APIUser
from api_manager.organizations.serializers import BasicOrganizationSerializer
from student.models import UserProfile
class DynamicFieldsModelSerializer(serializers.ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):
# Instantiate the superclass normally
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)
fields = self.context['request'].QUERY_PARAMS.get('fields', None) if 'request' in self.context else None
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)
class UserSerializer(DynamicFieldsModelSerializer):
class UserSerializer(serializers.ModelSerializer):
""" Serializer for User model interactions """
organizations = BasicOrganizationSerializer(many=True, required=False)
created = serializers.DateTimeField(source='date_joined', required=False)
avatar_url = serializers.CharField(source='profile.avatar_url')
city = serializers.CharField(source='profile.city')
title = serializers.CharField(source='profile.title')
country = serializers.CharField(source='profile.country')
full_name = serializers.CharField(source='profile.name')
class Meta:
""" Serializer/field specification """
model = APIUser
fields = ("id", "email", "username", "first_name", "last_name", "created", "is_active", "organizations")
fields = ("id", "email", "username", "first_name", "last_name", "created", "is_active", "organizations", "avatar_url", "city", "title", "country", "full_name")
read_only_fields = ("id", "email", "username")
class UserCountByCitySerializer(serializers.Serializer):
""" Serializer for user count by city """
city = serializers.CharField(source='profile__city')
......
......@@ -156,7 +156,10 @@ class UsersApiTests(ModuleStoreTestCase):
'username': 'test_user{}'.format(i),
'password': self.test_password,
'first_name': 'John{}'.format(i),
'last_name': 'Doe{}'.format(i)
'last_name': 'Doe{}'.format(i),
'avatar_url': 'http://avatar.com/{}.jpg'.format(i),
'city': 'Boston',
'title': "The King",
}
response = self.do_post(test_uri, data)
......@@ -210,6 +213,15 @@ class UsersApiTests(ModuleStoreTestCase):
response = self.do_get('{}?email={}'.format(test_uri, 'john@example.com'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 0)
# add some additional fields and filter the response to only these fields
response = self.do_get('{}?email=test2@example.com&fields=avatar_url,city,title'.format(test_uri))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), 1)
self.assertEqual(response.data['results'][0]['avatar_url'], 'http://avatar.com/2.jpg')
self.assertEqual(response.data['results'][0]['city'], 'Boston')
self.assertEqual(response.data['results'][0]['title'], 'The King')
if 'id' in response.data['results'][0]:
self.fail("Dynamic field filtering error in UserSerializer")
def test_user_list_get_with_org_filter(self):
test_uri = self.users_base_uri
......
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