From 491badc877a91f9a2e367f75e700d3e77de876e9 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 15 Nov 2019 17:56:14 -0400 Subject: [PATCH] Use timezone aware date for document statistics Signed-off-by: Roberto Rosario --- HISTORY.rst | 1 + mayan/apps/documents/statistics.py | 57 ++++++++++++++++-------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 4c90b6e9cf..c5499da4b8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -139,6 +139,7 @@ These are now a fallback if the new 'DATABASES' setting is not specified. - Refactor the initial setting bootstrap code. +- Use timezone aware date for document statistics 3.2.10 (2019-XX-XX) =================== diff --git a/mayan/apps/documents/statistics.py b/mayan/apps/documents/statistics.py index 2f1059e7d1..7bceb98f86 100644 --- a/mayan/apps/documents/statistics.py +++ b/mayan/apps/documents/statistics.py @@ -1,8 +1,7 @@ from __future__ import absolute_import, unicode_literals -import datetime - from django.apps import apps +from django.utils import timezone from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy as _ @@ -26,14 +25,14 @@ def new_documents_per_month(): qss = qsstats.QuerySetStats(Document.passthrough.all(), 'date_added') - today = datetime.date.today() - this_year = datetime.date(year=today.year, month=1, day=1) + now = timezone.now().date() + start = timezone.datetime(year=now.year, month=1, day=1).date() return { 'series': { 'Documents': map( lambda x: {force_text(MONTH_NAMES[x[0].month]): x[1]}, - qss.time_series(start=this_year, end=today, interval='months') + qss.time_series(start=start, end=now, interval='months') ) } } @@ -48,14 +47,14 @@ def new_document_pages_per_month(): DocumentPage.objects.all(), 'document_version__document__date_added' ) - today = datetime.date.today() - this_year = datetime.date(year=today.year, month=1, day=1) + now = timezone.now().date() + start = timezone.datetime(year=now.year, month=1, day=1).date() return { 'series': { 'Pages': map( lambda x: {force_text(MONTH_NAMES[x[0].month]): x[1]}, - qss.time_series(start=this_year, end=today, interval='months') + qss.time_series(start=start, end=now, interval='months') ) } } @@ -88,14 +87,14 @@ def new_document_versions_per_month(): DocumentVersion.objects.all(), 'document__date_added' ) - today = datetime.date.today() - this_year = datetime.date(year=today.year, month=1, day=1) + now = timezone.now().date() + start = timezone.datetime(year=now.year, month=1, day=1).date() return { 'series': { 'Versions': map( lambda x: {force_text(MONTH_NAMES[x[0].month]): x[1]}, - qss.time_series(start=this_year, end=today, interval='months') + qss.time_series(start=start, end=now, interval='months') ) } } @@ -127,25 +126,27 @@ def total_document_per_month(): Document = apps.get_model(app_label='documents', model_name='Document') qss = qsstats.QuerySetStats(Document.objects.all(), 'date_added') - this_year = datetime.date.today().year + now = timezone.now() result = [] - for month in range(1, datetime.date.today().month + 1): + for month in range(1, now.month + 1): next_month = month + 1 if month == 12: next_month = 1 - year = this_year + 1 + year = now.year + 1 else: next_month = month + 1 - year = this_year + year = now.year result.append( { force_text( MONTH_NAMES[month] - ): qss.until(datetime.date(year, next_month, 1)) + ): qss.until( + timezone.datetime(year, next_month, 1, tzinfo=now.tzinfo) + ) } ) @@ -164,25 +165,27 @@ def total_document_version_per_month(): qss = qsstats.QuerySetStats( DocumentVersion.objects.all(), 'document__date_added' ) - this_year = datetime.date.today().year + now = timezone.now() result = [] - for month in range(1, datetime.date.today().month + 1): + for month in range(1, now.month + 1): next_month = month + 1 if month == 12: next_month = 1 - year = this_year + 1 + year = now.year + 1 else: next_month = month + 1 - year = this_year + year = now.year result.append( { force_text( MONTH_NAMES[month] - ): qss.until(datetime.date(year, next_month, 1)) + ): qss.until( + timezone.datetime(year, next_month, 1, tzinfo=now.tzinfo) + ) } ) @@ -201,25 +204,27 @@ def total_document_page_per_month(): qss = qsstats.QuerySetStats( DocumentPage.objects.all(), 'document_version__document__date_added' ) - this_year = datetime.date.today().year + now = timezone.now() result = [] - for month in range(1, datetime.date.today().month + 1): + for month in range(1, now.month + 1): next_month = month + 1 if month == 12: next_month = 1 - year = this_year + 1 + year = now.year + 1 else: next_month = month + 1 - year = this_year + year = now.year result.append( { force_text( MONTH_NAMES[month] - ): qss.until(datetime.date(year, next_month, 1)) + ): qss.until( + timezone.datetime(year, next_month, 1, tzinfo=now.tzinfo) + ) } )