From 2c49efbe67bbf2b82c0d08e85d047ebc96c65479 Mon Sep 17 00:00:00 2001 From: Sam Dudley Date: Mon, 25 Nov 2024 16:47:39 +0000 Subject: [PATCH] add contextvar magic small js optimisation --- core/utils/generic_helpers.py | 10 ++++++++++ forecast/views/edit_forecast.py | 4 ++-- front_end/src/Util.js | 5 +++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/core/utils/generic_helpers.py b/core/utils/generic_helpers.py index 1c6564d21..0fa165cf9 100644 --- a/core/utils/generic_helpers.py +++ b/core/utils/generic_helpers.py @@ -1,4 +1,5 @@ import datetime +from contextvars import ContextVar from django.contrib.admin.models import CHANGE, LogEntry from django.contrib.contenttypes.models import ContentType @@ -6,7 +7,14 @@ from core.models import FinancialYear +_current_financial_year = ContextVar("current_financial_year", default=None) + + def get_current_financial_year(): + # FIXME: decide if to include this + if var := _current_financial_year.get(): + return var + y = FinancialYear.objects.filter(current=True) if y: current_financial_year = y.last().financial_year @@ -27,6 +35,8 @@ def get_current_financial_year(): # calendar year current_financial_year -= 1 + _current_financial_year.set(current_financial_year) + return current_financial_year diff --git a/forecast/views/edit_forecast.py b/forecast/views/edit_forecast.py index 0a1c8f1cf..4954c5d02 100644 --- a/forecast/views/edit_forecast.py +++ b/forecast/views/edit_forecast.py @@ -471,7 +471,7 @@ class EditForecastView( def class_name(self): return "wide-table" - # FIXME: might be risky + # FIXME: Triple check this is safe @cached_property def cost_centre_details(self): cost_centre = CostCentre.objects.select_related("directorate__group").get( @@ -532,7 +532,7 @@ def get_payroll_forecast_report(self): return data - @property + @cached_property def future_year_display(self): if self._future_year_display is None: current_year = get_current_financial_year() diff --git a/front_end/src/Util.js b/front_end/src/Util.js index ee0510407..7aeb4deda 100644 --- a/front_end/src/Util.js +++ b/front_end/src/Util.js @@ -32,10 +32,11 @@ function getCookie(name) { return cookieValue; } +const NumberFormat = new Intl.NumberFormat('en-GB'); + export const formatValue = (value) => { - let nfObject = new Intl.NumberFormat('en-GB'); let pounds = Math.round(value) - return nfObject.format(pounds); + return NumberFormat.format(pounds); } /**