Skip to content

Commit

Permalink
refactor: move towards a single function to set session variables fro…
Browse files Browse the repository at this point in the history
…m request variables

Signed-off-by: F.N. Claessen <[email protected]>
  • Loading branch information
Flix6x committed Jul 26, 2023
1 parent b2a71a1 commit 2d09c7c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
10 changes: 3 additions & 7 deletions flexmeasures/api/dev/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 ``<div style="width: 100%;">`` 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("/<id>/chart_data/")
Expand Down
5 changes: 3 additions & 2 deletions flexmeasures/api/v3_0/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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("/<id>/chart_data/")
Expand Down
9 changes: 7 additions & 2 deletions flexmeasures/ui/utils/view_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 2d09c7c

Please sign in to comment.