Repository: Cloud-CV/EvalAI

Issue:
Modify contact_us api to fetch name and email, if the user is logged in.
This issue is related to #853 as discussed with @deshraj .


Discussion:

Resolving patch:
diff --git a/apps/web/views.py b/apps/web/views.py
--- a/apps/web/views.py
+++ b/apps/web/views.py
@@ -35,29 +35,33 @@ def internal_server_error(request):
 
 
 @throttle_classes([AnonRateThrottle, ])
-@api_view(['POST', ])
+@api_view(['GET', 'POST'])
 @permission_classes((permissions.AllowAny,))
 def contact_us(request):
+    user_does_not_exist = False
     try:
         user = User.objects.get(username=request.user)
         name = user.username
         email = user.email
-        request_data = {"name": name, "email": email}
-        request_data['message'] = request.data['message']
-        serializer = ContactSerializer(data=request_data)
-        if serializer.is_valid():
-            serializer.save()
-            response_data = {'message': 'Your message has been successfully recorded. We will contact you shortly.'}
-            return Response(response_data, status=status.HTTP_201_CREATED)
-        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
+        request_data = {'name': name, 'email': email}
     except:
-        serializer = ContactSerializer(data=request.data)
+        request_data = request.data
+        user_does_not_exist = True
+
+    if request.method == 'POST' or user_does_not_exist:
+        if request.POST.get('message'):
+            request_data['message'] = request.POST.get('message')
+        serializer = ContactSerializer(data=request_data)
         if serializer.is_valid():
             serializer.save()
-            response_data = {'message': 'Your message has been successfully recorded. We will contact you shortly.'}
+            response_data = {'message': 'We have received your request and will contact you shortly.'}
             return Response(response_data, status=status.HTTP_201_CREATED)
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 
+    elif request.method == 'GET':
+        response_data = {"name": name, "email": email}
+        return Response(response_data, status=status.HTTP_200_OK)
+
 
 @throttle_classes([AnonRateThrottle])
 @api_view(['GET', 'POST'])


Test patch:
diff --git a/tests/unit/web/test_views.py b/tests/unit/web/test_views.py
--- a/tests/unit/web/test_views.py
+++ b/tests/unit/web/test_views.py
@@ -1,3 +1,4 @@
+from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse_lazy
 from django.core.files.uploadedfile import SimpleUploadedFile
 
@@ -30,15 +31,34 @@ def setUp(self):
         super(CreateContactMessage, self).setUp()
         self.url = reverse_lazy('web:contact_us')
 
+        self.user = User.objects.create(
+            username='someuser',
+            email="user@test.com",
+            password='secret_password')
+
         self.data = {
-            'name': 'New Message',
-            'email': 'newuser@test.com',
-            'message': 'New Message for Conatct'
+            'name': self.user.username,
+            'email': self.user.email,
+            'message': 'Sample contact message'
+        }
+
+    def test_get_user_data(self):
+        expected = {
+            'name': self.user.username,
+            'email': self.user.email,
         }
 
+        self.client.force_authenticate(user=self.user)
+        response = self.client.get(self.url)
+        self.assertEqual(expected, response.data)
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
     def test_create_contact_message_with_all_data(self):
-        response = self.client.post(self.url, self.data)
-        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+        if self.data['message']:
+            response = self.client.post(self.url, self.data)
+            expected = {'message': 'We have received your request and will contact you shortly.'}
+            self.assertEqual(response.data, expected)
+            self.assertEqual(response.status_code, status.HTTP_201_CREATED)
 
     def test_create_contact_message_with_no_data(self):
         del self.data['message']