Repository: Cloud-CV/EvalAI

Issue:
Text not being displayed for the 'star challenge feature'
Please see the below image for the illustration:

<img width="1438" alt="screen shot 2017-09-06 at 6 09 47 pm" src="https://user-images.githubusercontent.com/2945708/30137179-2a443974-9330-11e7-82c2-63658825255c.png">



Discussion:
@deshraj  can I take this?
@Ram81 yes sure. go ahead
@RishabhJain2018 you haven't created an entry for each new challenge's Star Challenge model. That's why when we try to fetch initially we can't get any message on the button.
For now I have done changes in controller & whenever we get an error fetching star challenge for any challenge I've set the message as 'Star'
@Ram81 I didn't get your point of creating the entry. Can you please explain a bit further ?
@RishabhJain2018 whenever you create a challenge there is no record entry made in 'Star Challenge' model that's why whenever the user opens the challenge page before being starred by anyone we can't fetch any entry for the particular challenge from 'Star Challenge'.
Did you get my point?
I suggest for each challenge you create an entry in 'Star Challenge' with initial values as 0 stars. 
@Ram81 Just initialise star count with 0 at the start of controller and it will solve the problem. 
@RishabhJain2018 please change the star count variable name from 'count' to 'starCount'.
@aka-jain count variable is being fetched from response that's why initially when there is no record of star challenge it fails. And this bug occurs only once, suppose if a person stars a challenge then he "unstar's" it then controller returns 0 as expected
Got your point @Ram81 , @RishabhJain2018 please do the require changes.
Also @Ram81 it will be good if we initialise the count variable with 0 value. Otherwise it might create problem somewhere.
@aka-jain Sure I will do it.

Resolving patch:
diff --git a/apps/challenges/views.py b/apps/challenges/views.py
--- a/apps/challenges/views.py
+++ b/apps/challenges/views.py
@@ -882,6 +882,11 @@ def star_challenge(request, challenge_pk):
             return Response(response_data, status=status.HTTP_200_OK)
         except:
             starred_challenge = StarChallenge.objects.filter(challenge=challenge)
+            if not starred_challenge:
+                response_data = {'is_starred': False,
+                                 'count': 0}
+                return Response(response_data, status=status.HTTP_200_OK)
+
             serializer = StarChallengeSerializer(starred_challenge, many=True)
             response_data = {'is_starred': False,
                              'count': serializer.data[0]['count']}


Test patch:
diff --git a/tests/unit/challenges/test_views.py b/tests/unit/challenges/test_views.py
--- a/tests/unit/challenges/test_views.py
+++ b/tests/unit/challenges/test_views.py
@@ -1931,6 +1931,21 @@ def setUp(self):
             primary=True,
             verified=True)
 
+        self.challenge1 = Challenge.objects.create(
+            title='Test Challenge1',
+            short_description='Short description for test challenge1',
+            description='Description for test challenge1',
+            terms_and_conditions='Terms and conditions for test challenge1',
+            submission_guidelines='Submission guidelines for test challenge1',
+            creator=self.challenge_host_team,
+            published=False,
+            enable_forum=True,
+            anonymous_leaderboard=False,
+            start_date=timezone.now() - timedelta(days=2),
+            end_date=timezone.now() + timedelta(days=1),
+            approved_by_admin=False,
+        )
+
         self.star_challenge = StarChallenge.objects.create(user=self.user,
                                                            challenge=self.challenge,
                                                            is_starred=True)
@@ -1972,6 +1987,17 @@ def test_get_challenge_when_user_hasnt_starred(self):
         self.assertEqual(response.data, expected)
         self.assertEqual(response.status_code, status.HTTP_200_OK)
 
+    def test_get_challenge_when_no_user_has_starred_or_unstarred_it(self):
+        self.url = reverse_lazy('challenges:star_challenge',
+                                kwargs={'challenge_pk': self.challenge1.pk})
+        expected = {
+            'is_starred': False,
+            'count': 0,
+        }
+        response = self.client.get(self.url, {})
+        self.assertEqual(response.data, expected)
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
     def test_unstar_challenge(self):
         self.url = reverse_lazy('challenges:star_challenge',
                                 kwargs={'challenge_pk': self.challenge.pk})