From f97fa2cebc85a12f6507f08d9b9266025a1277d7 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Wed, 22 Nov 2017 10:37:53 +0000 Subject: [PATCH] Reused the months array instead of redefining it Re-organised the code to re-use the months array which also was not displaying a month where there was no stats. This now gets the months, enumerates that array updating the templates used when there are stats items so the users sees each month of the financial year (even if there are no stats) when there are stats they are displayed. --- app/main/views/dashboard.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 8717f5312..f04e3245f 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -120,24 +120,20 @@ def template_usage(service_id): months = [ { 'name': yyyy_mm_to_datetime(month).strftime('%B'), - 'templates_used': {}, + 'templates_used': [], } for month in get_months_for_financial_year(year, time_format='%Y-%m') ] - templates_by_month = defaultdict(list) - for stat in stats: - templates_by_month[int(stat['month'])].append({ - 'id': stat['template_id'], - 'name': stat['name'], - 'type': stat['type'], - 'requested_count': stat['count'] - }) - months = [{ - 'name': calendar.month_name[k], - 'templates_used': v - } for k, v in templates_by_month.items() - ] + for i, d in enumerate(months): + for stat in stats: + if d['name'] == calendar.month_name[int(stat['month'])]: + d['templates_used'].append({ + 'id': stat['template_id'], + 'name': stat['name'], + 'type': stat['type'], + 'requested_count': stat['count'] + }) return render_template( 'views/dashboard/all-template-statistics.html',