From 5b3d8b0047254dfc9ddbbaa3064ed625c994dede Mon Sep 17 00:00:00 2001
From: Martin Burchell <martinb@aptivate.org>
Date: Wed, 29 Jul 2015 11:05:31 +0100
Subject: [PATCH] Modified transport itemcounts to handle 0 counts

The transport layer term_itemcount when called with a date range
now returns terms that fall outside the range with count=0.
---
 django/website/transport/taxonomies.py        | 32 ++++++++++++++++---
 .../tests/taxonomy_term_itemcount_tests.py    | 28 ++++++++++++++++
 2 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/django/website/transport/taxonomies.py b/django/website/transport/taxonomies.py
index 7f1111b7..f5b98dcd 100644
--- a/django/website/transport/taxonomies.py
+++ b/django/website/transport/taxonomies.py
@@ -5,7 +5,7 @@ from rest_framework.test import APIRequestFactory
 from rest_framework import status
 
 from .exceptions import TransportException
-
+from .terms import list as terms_list
 
 request_factory = APIRequestFactory()
 
@@ -34,14 +34,38 @@ def list(**kwargs):
     return view(request).data
 
 
+def _add_zero_counts_for_missing_terms(slug, itemcounts):
+    # We want to return zero counts for terms that fall outside the date range,
+    # which the API doesn't do for us. So we need to get all the terms for the
+    # taxonomy and set any that aren't in the date range results to count: 0
+    all_terms = terms_list(taxonomy=slug)
+
+    itemcounts_by_name = {t['name']: t for t in itemcounts}
+
+    new_itemcounts = []
+
+    for term in all_terms:
+        if term['name'] not in itemcounts_by_name:
+            term['count'] = 0
+        else:
+            term = itemcounts_by_name[term['name']]
+
+        new_itemcounts.append(term)
+
+    return new_itemcounts
+
+
 def term_itemcount(slug, **kwargs):
     view = get_view(actions={'get': 'itemcount'})
     request = request_factory.get(itemcount_url(slug),
                                   kwargs)
     response = view(request, slug=slug)
 
-    if status.is_success(response.status_code):
+    if not status.is_success(response.status_code):
+        response.data['status_code'] = response.status_code
+        raise TransportException(response.data)
+
+    if 'start_time' not in kwargs or 'end_time' not in kwargs:
         return response.data
 
-    response.data['status_code'] = response.status_code
-    raise TransportException(response.data)
+    return _add_zero_counts_for_missing_terms(slug, response.data)
diff --git a/django/website/transport/tests/taxonomy_term_itemcount_tests.py b/django/website/transport/tests/taxonomy_term_itemcount_tests.py
index 20f232c8..dceecfcb 100644
--- a/django/website/transport/tests/taxonomy_term_itemcount_tests.py
+++ b/django/website/transport/tests/taxonomy_term_itemcount_tests.py
@@ -92,3 +92,31 @@ def test_term_itemcount_returns_terms_and_counts_for_range(
         end_time=end_time)
 
     assert term['count'] == 7
+
+
+@pytest.mark.django_db
+def test_term_itemcount_returns_zero_term_counts_for_range(
+        questions_category,
+        questions_term):
+
+    now = time_now()
+    one_day_ago = now - timedelta(days=1)
+    one_week_ago = now - timedelta(weeks=1)
+    eight_days_ago = now - timedelta(days=8)
+
+    item_too_recent = item_data(timestamp=now)
+    item_too_old = item_data(timestamp=eight_days_ago)
+
+    transport.items.add_term(item_too_recent['id'],
+                             questions_category.slug,
+                             questions_term.name)
+    transport.items.add_term(item_too_old['id'],
+                             questions_category.slug,
+                             questions_term.name)
+
+    [term] = transport.taxonomies.term_itemcount(
+        slug=questions_category.slug,
+        start_time=one_week_ago,
+        end_time=one_day_ago)
+
+    assert term['count'] == 0
-- 
GitLab