Skip to content
Browse files

[bug 727714] Discount askers replies when counting contributors (KPI).

* When an asker replies to own question, the reply will not count towards contributions.
  • Loading branch information...
1 parent 5db3db4 commit 763666583b49686b07a3358fc188caee80c96bb3 @rlr rlr committed Apr 18, 2012
Showing with 46 additions and 3 deletions.
  1. +11 −3 apps/kpi/api.py
  2. +35 −0 apps/kpi/tests/test_api.py
View
14 apps/kpi/api.py
@@ -281,9 +281,17 @@ def _add_users(monthly_dict, values, column):
k, v in d.items()]
# Build the support forum contributors count list aggregated by month
- qs = _monthly_qs_for(Answer).values('year', 'month', 'creator')
- qs = qs.annotate(count=Count('creator'))
- answerers = qs.filter(count__gte=10)
+ answerers = (Answer.objects
+ .exclude(creator=F('question__creator'))
+ .filter(created__gte=_start_date())
+ .extra(
+ select={
+ 'month': 'extract( month from questions_answer.created )',
+ 'year': 'extract( year from questions_answer.created )',
+ })
+ .values('year', 'month', 'creator')
+ .annotate(count=Count('creator'))
+ .filter(count__gte=10))
d = {}
for a in answerers:
_add_user(d, a['year'], a['month'], a['creator'])
View
35 apps/kpi/tests/test_api.py
@@ -136,6 +136,41 @@ def test_active_contributors(self):
eq_(r['objects'][0]['support_forum'], 1)
eq_(r['objects'][0]['aoa'], 1)
+ def test_asker_replies_arent_a_contribution(self):
+ """Verify that replies posted by the question creator aren't counted.
+
+ If a user has 10 replies to their own question, they aren't counted as
+ a contributor.
+ """
+ # A user with 10 answers to own question.
+ q = question(save=True)
+ u = q.creator
+ for x in range(10):
+ answer(creator=u, question=q, save=True)
+
+ url = reverse('api_dispatch_list',
+ kwargs={'resource_name': 'kpi_active_contributors',
+ 'api_name': 'v1'})
+
+ response = self.client.get(url + '?format=json')
+ eq_(200, response.status_code)
+ r = json.loads(response.content)
+ eq_(len(r['objects']), 0)
+
+ # Change the question creator, now we should have 1 contributor.
+ q.creator = user(save=True)
+ q.save()
+ cache.clear() # We need to clear the cache for new results.
+
+ url = reverse('api_dispatch_list',
+ kwargs={'resource_name': 'kpi_active_contributors',
+ 'api_name': 'v1'})
+
+ response = self.client.get(url + '?format=json')
+ eq_(200, response.status_code)
+ r = json.loads(response.content)
+ eq_(r['objects'][0]['support_forum'], 1)
+
def test_sphinx_clickthrough_get(self):
"""Test Sphinx clickthrough read API."""
click_kind, search_kind = self._make_sphinx_metric_kinds()

0 comments on commit 7636665

Please sign in to comment.
Something went wrong with that request. Please try again.