From 2d09c7c1a00eb21ef72cfcc7e2981def9ebc232f Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Wed, 26 Jul 2023 11:03:00 +0200 Subject: [PATCH] refactor: move towards a single function to set session variables from request variables Signed-off-by: F.N. Claessen --- flexmeasures/api/dev/sensors.py | 10 +++------- flexmeasures/api/v3_0/assets.py | 5 +++-- flexmeasures/ui/utils/view_utils.py | 9 +++++++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/flexmeasures/api/dev/sensors.py b/flexmeasures/api/dev/sensors.py index 4c00f36a9..19f22300e 100644 --- a/flexmeasures/api/dev/sensors.py +++ b/flexmeasures/api/dev/sensors.py @@ -18,10 +18,7 @@ from flexmeasures.data.models.generic_assets import GenericAsset from flexmeasures.data.models.time_series import Sensor from flexmeasures.data.services.annotations import prepare_annotations_for_chart -from flexmeasures.ui.utils.view_utils import ( - set_chart_type_for_session, - set_time_range_for_session, -) +from flexmeasures.ui.utils.view_utils import set_session_variables class SensorAPI(FlaskView): @@ -71,9 +68,8 @@ def get_chart(self, id: int, sensor: Sensor, **kwargs): - "width" (an integer number of pixels; without it, the chart will be scaled to the full width of the container (hint: use ``
`` to set a div width to 100%) - "height" (an integer number of pixels; without it, FlexMeasures sets a default, currently 300) """ - # Store selected chart type and time range as session variables, for a consistent UX across UI page loads - set_chart_type_for_session() - set_time_range_for_session() + # Store selected time range and chart type as session variables, for a consistent UX across UI page loads + set_session_variables("event_starts_after", "event_ends_before", "chart_type") return json.dumps(sensor.chart(**kwargs)) @route("//chart_data/") diff --git a/flexmeasures/api/v3_0/assets.py b/flexmeasures/api/v3_0/assets.py index db505e34a..1f6fbdde5 100644 --- a/flexmeasures/api/v3_0/assets.py +++ b/flexmeasures/api/v3_0/assets.py @@ -16,7 +16,7 @@ from flexmeasures.api.common.schemas.generic_assets import AssetIdField from flexmeasures.api.common.schemas.users import AccountIdField from flexmeasures.utils.coding_utils import flatten_unique -from flexmeasures.ui.utils.view_utils import set_time_range_for_session +from flexmeasures.ui.utils.view_utils import set_session_variables asset_schema = AssetSchema() @@ -284,7 +284,8 @@ def get_chart(self, id: int, asset: GenericAsset, **kwargs): .. :quickref: Chart; Download a chart with time series """ - set_time_range_for_session() + # Store selected time range as session variables, for a consistent UX across UI page loads + set_session_variables("event_starts_after", "event_ends_before") return json.dumps(asset.chart(**kwargs)) @route("//chart_data/") diff --git a/flexmeasures/ui/utils/view_utils.py b/flexmeasures/ui/utils/view_utils.py index 717d95f7e..40755dc85 100644 --- a/flexmeasures/ui/utils/view_utils.py +++ b/flexmeasures/ui/utils/view_utils.py @@ -110,8 +110,13 @@ def clear_session(): del session[skey] -def set_chart_type_for_session(): - session["chart_type"] = request.values.get("chart_type") +def set_session_variables(*var_names: str): + """Store request values as session variables, for a consistent UX across UI page loads. + + >>> set_session_variables("event_starts_after", "event_ends_before", "chart_type") + """ + for var_name in var_names: + session[var_name] = request.values.get(var_name) def set_time_range_for_session():