Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sorting information to configuration parameter values #272

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions arpav_ppcv/bootstrapper/configurationparameters.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions arpav_ppcv/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ def create_configuration_parameter(
display_name_italian=allowed.display_name_italian,
description_english=allowed.description_english,
description_italian=allowed.description_italian,
sort_order=allowed.sort_order,
)
db_configuration_parameter.allowed_values.append(db_conf_param_value)
to_refresh.append(db_conf_param_value)
Expand Down Expand Up @@ -874,6 +875,7 @@ def update_configuration_parameter(
display_name_italian=av.display_name_italian,
description_english=av.description_english,
description_italian=av.description_italian,
sort_order=av.sort_order or 0,
)
db_configuration_parameter.allowed_values.append(db_allowed_value)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""added conf param value sort_order

Revision ID: c6f618a7f88f
Revises: 400c6e807767
Create Date: 2024-10-11 10:54:49.432153

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


# revision identifiers, used by Alembic.
revision: str = 'c6f618a7f88f'
down_revision: Union[str, None] = '400c6e807767'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('configurationparametervalue', sa.Column('sort_order', sa.Integer(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('configurationparametervalue', 'sort_order')
# ### end Alembic commands ###
6 changes: 5 additions & 1 deletion arpav_ppcv/schemas/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ConfigurationParameterValue(sqlmodel.SQLModel, table=True):
display_name_italian: Optional[str] = None
description_english: Optional[str] = None
description_italian: Optional[str] = None
sort_order: Optional[int] = 0
configuration_parameter_id: uuid.UUID

configuration_parameter: "ConfigurationParameter" = sqlmodel.Relationship(
Expand Down Expand Up @@ -76,6 +77,7 @@ class ConfigurationParameterValueCreate(sqlmodel.SQLModel):
display_name_italian: Optional[str] = None
description_english: Optional[str] = None
description_italian: Optional[str] = None
sort_order: Optional[int] = 0


class ConfigurationParameter(sqlmodel.SQLModel, table=True):
Expand All @@ -91,7 +93,7 @@ class ConfigurationParameter(sqlmodel.SQLModel, table=True):
sa_relationship_kwargs={
"cascade": "all, delete, delete-orphan",
"passive_deletes": True,
"order_by": "ConfigurationParameterValue.name",
"order_by": "ConfigurationParameterValue.sort_order",
},
)

Expand All @@ -114,6 +116,7 @@ class ConfigurationParameterValueCreateEmbeddedInConfigurationParameter(
display_name_italian: Optional[str] = None
description_english: Optional[str] = None
description_italian: Optional[str] = None
sort_order: int


class ConfigurationParameterCreate(sqlmodel.SQLModel):
Expand Down Expand Up @@ -147,6 +150,7 @@ class ConfigurationParameterValueUpdateEmbeddedInConfigurationParameterEdit(
display_name_italian: Optional[str] = None
description_english: Optional[str] = None
description_italian: Optional[str] = None
sort_order: Optional[int] = None


class ConfigurationParameterUpdate(sqlmodel.SQLModel):
Expand Down
1 change: 1 addition & 0 deletions arpav_ppcv/webapp/admin/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ConfigurationParameterValueRead(sqlmodel.SQLModel):
display_name_italian: Optional[str]
description_english: Optional[str]
description_italian: Optional[str]
sort_order: int


class ConfigurationParameterRead(sqlmodel.SQLModel):
Expand Down
25 changes: 17 additions & 8 deletions arpav_ppcv/webapp/admin/views/coverages.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class ConfigurationParameterView(ModelView):
starlette_admin.StringField("display_name_italian", required=True),
starlette_admin.StringField("description_english"),
starlette_admin.StringField("description_italian"),
starlette_admin.StringField("sort_order"),
),
)
),
Expand Down Expand Up @@ -158,6 +159,7 @@ async def create(self, request: Request, data: Dict[str, Any]) -> Any:
display_name_italian=av["display_name_italian"],
description_english=av.get("description_english"),
description_italian=av.get("description_italian"),
sort_order=av.get("sort_order", 0),
)
for av in data["allowed_values"]
],
Expand Down Expand Up @@ -222,6 +224,7 @@ async def edit(self, request: Request, pk: Any, data: Dict[str, Any]) -> Any:
display_name_italian=av["display_name_italian"],
description_english=av.get("description_english"),
description_italian=av.get("description_italian"),
sort_order=av.get("sort_order"),
)
for av in data["allowed_values"]
],
Expand Down Expand Up @@ -253,12 +256,14 @@ async def find_by_pk(
db_conf_param = await anyio.to_thread.run_sync(
database.get_configuration_parameter, request.state.session, pk
)
allowed = []
for av in db_conf_param.allowed_values:
cpv_kwargs = av.model_dump()
cpv_kwargs["sort_order"] = cpv_kwargs["sort_order"] or 0
allowed.append(read_schemas.ConfigurationParameterValueRead(**cpv_kwargs))
return read_schemas.ConfigurationParameterRead(
**db_conf_param.model_dump(),
allowed_values=[
read_schemas.ConfigurationParameterValueRead(**av.model_dump())
for av in db_conf_param.allowed_values
],
allowed_values=allowed,
)

async def find_all(
Expand All @@ -281,13 +286,17 @@ async def find_all(
)
result = []
for db_conf_param in db_conf_params:
allowed = []
for av in db_conf_param.allowed_values:
cpv_kwargs = av.model_dump()
cpv_kwargs["sort_order"] = cpv_kwargs["sort_order"] or 0
allowed.append(
read_schemas.ConfigurationParameterValueRead(**cpv_kwargs)
)
result.append(
read_schemas.ConfigurationParameterRead(
**db_conf_param.model_dump(),
allowed_values=[
read_schemas.ConfigurationParameterValueRead(**av.model_dump())
for av in db_conf_param.allowed_values
],
allowed_values=allowed,
)
)
return result
Expand Down
37 changes: 33 additions & 4 deletions arpav_ppcv/webapp/api_v2/routers/coverages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import itertools
import logging
import urllib.parse
from operator import itemgetter
from xml.etree import ElementTree as et
from typing import (
Annotated,
Expand Down Expand Up @@ -735,10 +736,26 @@ def get_forecast_variable_combinations(
):
variable_combinations = operations.get_forecast_variable_parameters(db_session)
var_combinations = []
for var_name, var_menu in variable_combinations.items():
for var_menu in variable_combinations.values():
variable_sort_order = (
var_menu[CoreConfParamName.CLIMATOLOGICAL_VARIABLE.value].sort_order or 0
)
aggregation_period_sort_order = (
var_menu[CoreConfParamName.AGGREGATION_PERIOD.value].sort_order or 0
)
measure_sort_order = (
var_menu[CoreConfParamName.MEASURE.value].sort_order or 0,
)
var_combinations.append(
coverage_schemas.ForecastVariableCombinations.from_items(var_menu)
(
variable_sort_order,
aggregation_period_sort_order,
measure_sort_order,
coverage_schemas.ForecastVariableCombinations.from_items(var_menu),
)
)
var_combinations.sort(key=itemgetter(0, 1, 2, 3))
var_combinations = [vc[3] for vc in var_combinations]
return coverage_schemas.ForecastVariableCombinationsList(
combinations=var_combinations,
translations=coverage_schemas.ForecastMenuTranslations.from_items(
Expand All @@ -756,10 +773,22 @@ def get_historical_variable_combinations(
):
variable_combinations = operations.get_historical_variable_parameters(db_session)
var_combinations = []
for var_name, var_menu in variable_combinations.items():
for var_menu in variable_combinations.values():
variable_sort_order = (
var_menu[CoreConfParamName.HISTORICAL_VARIABLE.value].sort_order or 0
)
aggregation_period_sort_order = (
var_menu[CoreConfParamName.AGGREGATION_PERIOD.value].sort_order or 0
)
var_combinations.append(
coverage_schemas.HistoricalVariableCombinations.from_items(var_menu)
(
variable_sort_order,
aggregation_period_sort_order,
coverage_schemas.HistoricalVariableCombinations.from_items(var_menu),
)
)
var_combinations.sort(key=itemgetter(0, 1, 2))
var_combinations = [vc[2] for vc in var_combinations]
return coverage_schemas.HistoricalVariableCombinationsList(
combinations=var_combinations,
translations=coverage_schemas.HistoricalMenuTranslations.from_items(
Expand Down
12 changes: 11 additions & 1 deletion arpav_ppcv/webapp/api_v2/schemas/coverages.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid
import typing
from operator import itemgetter

import pydantic
from fastapi import Request
Expand Down Expand Up @@ -28,6 +29,7 @@ class ConfigurationParameterValueEmbeddedInConfigurationParameter(pydantic.BaseM
display_name_italian: str
description_english: str | None
description_italian: str | None
sort_order: int


class ConfigurationParameterReadListItem(pydantic.BaseModel):
Expand Down Expand Up @@ -59,10 +61,12 @@ def from_db_instance(
exclude={
"display_name_english",
"display_name_italian",
"sort_order",
}
),
display_name_english=pv.display_name_english or pv.name,
display_name_italian=pv.display_name_italian or pv.name,
sort_order=pv.sort_order or 0,
)
for pv in instance.allowed_values
],
Expand Down Expand Up @@ -472,7 +476,13 @@ def from_items(cls, menu_tree: app_models.ForecastVariableMenuTree):
for param_name, param_combinations in menu_tree["combinations"].items():
combinations[param_name] = []
for valid_value in param_combinations["values"]:
combinations[param_name].append(valid_value.name)
combinations[param_name].append(
(valid_value.name, valid_value.sort_order or 0)
)

for param_name, param_combinations in combinations.items():
param_combinations.sort(key=itemgetter(1))
combinations[param_name] = [name for name, sort_order in param_combinations]

return cls(
variable=menu_tree[CoreConfParamName.CLIMATOLOGICAL_VARIABLE.value].name,
Expand Down