Repository: Cloud-CV/EvalAI

Issue:
Add support to update the user profile.



Discussion:

Resolving patch:
diff --git a/apps/accounts/serializers.py b/apps/accounts/serializers.py
--- a/apps/accounts/serializers.py
+++ b/apps/accounts/serializers.py
@@ -11,3 +11,26 @@ class Meta:
         model = get_user_model()
         fields = ('pk', 'email', 'username', 'first_name', 'last_name')
         read_only_fields = ('email', 'username')
+
+
+class ProfileSerializer(UserDetailsSerializer):
+    """
+    Serializer to update the user profile.
+    """
+
+    affiliation = serializers.CharField(source="profile.affiliation")
+
+    class Meta(UserDetailsSerializer.Meta):
+        fields = UserDetailsSerializer.Meta.fields + ('affiliation',)
+
+    def update(self, instance, validated_data):
+        profile_data = validated_data.pop('profile', {})
+        affiliation = profile_data.get('affiliation')
+
+        instance = super(ProfileSerializer, self).update(instance, validated_data)
+
+        profile = instance.profile
+        if profile_data and affiliation:
+            profile.affiliation = affiliation
+            profile.save()
+        return instance
diff --git a/settings/common.py b/settings/common.py
--- a/settings/common.py
+++ b/settings/common.py
@@ -276,5 +276,5 @@
 
 # To make usermame field read-only, customized serializer is defined.
 REST_AUTH_SERIALIZERS = {
-    'USER_DETAILS_SERIALIZER': 'accounts.serializers.UserDetailsSerializer',
+    'USER_DETAILS_SERIALIZER': 'accounts.serializers.ProfileSerializer',
 }


Test patch:
diff --git a/tests/unit/accounts/test_urls.py b/tests/unit/accounts/test_urls.py
--- a/tests/unit/accounts/test_urls.py
+++ b/tests/unit/accounts/test_urls.py
@@ -30,14 +30,3 @@ class TestStringMethods(BaseAPITestClass):
     def test_disable_user(self):
         url = reverse_lazy('accounts:disable_user')
         self.assertEqual(unicode(url), '/api/accounts/user/disable')
-
-
-class TestUpdateUser(BaseAPITestClass):
-
-    def test_cannot_update_username(self):
-        response = self.client.put(
-            '/api/auth/user/',
-            {'username': 'anotheruser'},
-            format='json')
-        self.assertNotContains(response, 'anotheruser')
-        self.assertContains(response, 'someuser')
diff --git a/tests/unit/accounts/test_views.py b/tests/unit/accounts/test_views.py
--- a/tests/unit/accounts/test_views.py
+++ b/tests/unit/accounts/test_views.py
@@ -1,3 +1,5 @@
+import os
+
 from django.core.urlresolvers import reverse_lazy
 from django.contrib.auth.models import User
 
@@ -32,3 +34,17 @@ class DisableUserTest(BaseAPITestClass):
     def test_disable_user(self):
         response = self.client.post(self.url, {})
         self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+
+class TestUpdateUser(BaseAPITestClass):
+
+    def test_cannot_update_username(self):
+        self.url = reverse_lazy('rest_user_details')
+        self.data = {
+            'username': 'anotheruser',
+            'affiliation': 'some_affiliation',
+        }
+        response = self.client.put(os.path.join('api', 'auth', self.url), self.data)
+        self.assertNotContains(response, 'anotheruser')
+        self.assertContains(response, 'someuser')
+        self.assertEqual(response.status_code, status.HTTP_200_OK)