From 0d36a0ef1653603baf5712905c6767487965bcb1 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Tue, 21 Nov 2017 11:09:00 +0000 Subject: [PATCH 1/5] Updated link to for browsing to financial years The link which when clicked allows the user to view different financial years was pointing to the template_activity page. Updated to the link to point to the new page. --- app/main/views/dashboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index a027fe4ff..63a0389d9 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -142,7 +142,7 @@ def template_usage(service_id): for month in months ), years=get_tuples_of_financial_years( - partial(url_for, '.template_history', service_id=service_id), + partial(url_for, '.template_usage', service_id=service_id), end=current_financial_year, ), selected_year=year From 5937842ca40d1af6a3810a2950815d02fb7ae4e4 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Tue, 21 Nov 2017 16:48:37 +0000 Subject: [PATCH 2/5] Fixes to the template usage page - When a year contains no data ensure a default set of months is returned so that all months can be seen in the UI - Add the template id so the user can click through to the template --- app/main/views/dashboard.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 63a0389d9..8717f5312 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -117,9 +117,18 @@ def template_usage(service_id): stats = sorted(stats, key=lambda x: (x['count']), reverse=True) + months = [ + { + 'name': yyyy_mm_to_datetime(month).strftime('%B'), + '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'] From f97fa2cebc85a12f6507f08d9b9266025a1277d7 Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Wed, 22 Nov 2017 10:37:53 +0000 Subject: [PATCH 3/5] 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', From dd2bd2f80a912c933e0b9e9ae24663a1957a702f Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Wed, 22 Nov 2017 14:38:47 +0000 Subject: [PATCH 4/5] Removed import An import was not required so removed it. --- app/main/views/dashboard.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index f04e3245f..9ad3586b4 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -1,5 +1,4 @@ import calendar -from collections import defaultdict from datetime import datetime from functools import partial from flask import ( From 9dbb14c9e10efcafbfe256f87fec54aa51cecffa Mon Sep 17 00:00:00 2001 From: Richard Chapman Date: Wed, 22 Nov 2017 15:50:31 +0000 Subject: [PATCH 5/5] Refactored to be more pythonic Removed the nested for loops and refactored to be pythonic so it is more maintainable in future. --- app/main/views/dashboard.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 9ad3586b4..553ad8b3d 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -116,23 +116,25 @@ def template_usage(service_id): stats = sorted(stats, key=lambda x: (x['count']), reverse=True) - months = [ - { - 'name': yyyy_mm_to_datetime(month).strftime('%B'), - 'templates_used': [], - } - for month in get_months_for_financial_year(year, time_format='%Y-%m') - ] - - for i, d in enumerate(months): - for stat in stats: - if d['name'] == calendar.month_name[int(stat['month'])]: - d['templates_used'].append({ + def get_monthly_template_stats(month_name, stats): + return { + 'name': month_name, + 'templates_used': [ + { 'id': stat['template_id'], 'name': stat['name'], 'type': stat['type'], 'requested_count': stat['count'] - }) + } + for stat in stats + if calendar.month_name[int(stat['month'])] == month_name + ], + } + + months = [ + get_monthly_template_stats(month, stats) + for month in get_months_for_financial_year(year, time_format='%B') + ] return render_template( 'views/dashboard/all-template-statistics.html',