Repository: DjangoGirls/djangogirls

Issue:
Applications CSV: scores need to be hidden
CSV shows average score even if user has not scored that application yet.

On the web interface, this is hidden until they have. The CSV shouldn't expose this information.



Discussion:

Resolving patch:
diff --git a/applications/models.py b/applications/models.py
--- a/applications/models.py
+++ b/applications/models.py
@@ -218,6 +218,13 @@ def get_by_rsvp_code(self, code, page):
     def is_accepted(self):
         return self.state == 'accepted'
 
+    def is_scored_by_user(self, user):
+        """
+        Returns true if the given user has scored this application
+        or false if they have not, or there is a zero score.
+        """
+        return self.scores.filter(user=user, score__gt=0).exists()
+
     def __str__(self):
         return str(self.pk)
 
diff --git a/applications/templatetags/applications_tags.py b/applications/templatetags/applications_tags.py
--- a/applications/templatetags/applications_tags.py
+++ b/applications/templatetags/applications_tags.py
@@ -1,18 +1,13 @@
 # -*- encoding: utf-8 -*-
 from django import template
 
-from applications.models import Score
-
 
 register = template.Library()
 
+
 @register.filter
-def scored_by_user(value, arg):
-    try:
-        score = Score.objects.get(application=value, user=arg)
-        return True if score.score else False
-    except Score.DoesNotExist:
-        return False
+def scored_by_user(application, user):
+    return application.is_scored_by_user(user)
 
 
 @register.simple_tag
diff --git a/applications/views.py b/applications/views.py
--- a/applications/views.py
+++ b/applications/views.py
@@ -85,6 +85,7 @@ def applications(request, city):
         'menu': get_organiser_menu(city),
     })
 
+
 @organiser_only
 def applications_csv(request, city):
     """
@@ -99,12 +100,13 @@ def applications_csv(request, city):
     response = HttpResponse(content_type='text/csv')
     response['Content-Disposition'] = u'attachment; filename="{}.csv"'.format(city)
     writer = csv.writer(response)
-    csv_header = ["Application Number","Application State", "RSVP Status", "Average Score"]
+    csv_header = ["Application Number", "Application State", "RSVP Status", "Average Score"]
     questions = page.form_set.first().question_set.values_list('title', flat=True)
     csv_header.extend(map(striptags, questions))
     writer.writerow(csv_header)
     for app in applications:
-        app_info = [app.number, app.state, app.rsvp_status, app.average_score]
+        score = app.average_score if app.is_scored_by_user(request.user) else '(hidden)'
+        app_info = [app.number, app.state, app.rsvp_status, score]
         app_info.extend(app.answer_set.values_list('answer', flat=True))
         writer.writerow(app_info)
     return response


Test patch:
diff --git a/applications/tests/tests_models.py b/applications/tests/tests_models.py
--- a/applications/tests/tests_models.py
+++ b/applications/tests/tests_models.py
@@ -133,6 +133,15 @@ def test_is_accepted(self):
             else:
                 self.assertFalse(self.application.is_accepted)
 
+    def test_is_scored_by_user(self):
+        Score.objects.create(user=self.user_1, application=self.application, score=random.randint(1, 5))
+
+        self.assertTrue(self.application.is_scored_by_user(self.user_1))
+        self.assertFalse(self.application.is_scored_by_user(self.user_2))
+
+        Score.objects.create(user=self.user_2, application=self.application, score=0)
+        self.assertFalse(self.application.is_scored_by_user(self.user_2))
+
 
 class EmailModel(TestCase):