Skip to content

Commit

Permalink
Merge pull request #95 from ricardogsilva/92-add-admin-section-for-ma…
Browse files Browse the repository at this point in the history
…naging-observation-variables-

Add admin views for observation-related models
  • Loading branch information
francbartoli authored May 28, 2024
2 parents 1e84361 + 2ce2c1e commit e4b7e62
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 21 deletions.
11 changes: 8 additions & 3 deletions arpav_ppcv/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ def create_station(
"""Create a new station."""
geom = shapely.io.from_geojson(station_create.geom.model_dump_json())
wkbelement = from_shape(geom)
db_station = observations.Station(geom=wkbelement, code=station_create.code)
db_station = observations.Station(
**station_create.model_dump(exclude={"geom"}),
geom=wkbelement,
)
session.add(db_station)
try:
session.commit()
Expand Down Expand Up @@ -205,8 +208,10 @@ def update_station(
station_update: observations.StationUpdate,
) -> observations.Station:
"""Update a station."""
data_ = station_update.model_dump(exclude_unset=True)
for key, value in data_.items():
geom = from_shape(shapely.io.from_geojson(station_update.geom.model_dump_json()))
other_data = station_update.model_dump(exclude={"geom"}, exclude_unset=True)
data = {**other_data, "geom": geom}
for key, value in data.items():
setattr(db_station, key, value)
session.add(db_station)
session.commit()
Expand Down
44 changes: 37 additions & 7 deletions arpav_ppcv/webapp/admin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
from starlette.middleware.sessions import SessionMiddleware
from starlette.exceptions import HTTPException
from starlette_admin.contrib.sqlmodel import Admin
from starlette_admin.views import Link
from starlette_admin.views import (
DropDown,
Link,
)

from ... import (
config,
database,
)
from ...schemas import coverages
from . import (
auth,
views,
from ...schemas import (
coverages,
observations,
)
from . import auth
from .middlewares import SqlModelDbSessionMiddleware
from .views import (
coverages as coverage_views,
observations as observations_views,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,8 +58,31 @@ def create_admin(settings: config.ArpavPpcvSettings) -> ArpavPpcvAdmin:
Middleware(SqlModelDbSessionMiddleware, engine=engine),
],
)
admin.add_view(views.ConfigurationParameterView(coverages.ConfigurationParameter))
admin.add_view(views.CoverageConfigurationView(coverages.CoverageConfiguration))
admin.add_view(
coverage_views.ConfigurationParameterView(coverages.ConfigurationParameter)
)
admin.add_view(
coverage_views.CoverageConfigurationView(coverages.CoverageConfiguration)
)
admin.add_view(observations_views.VariableView(observations.Variable))
admin.add_view(observations_views.StationView(observations.Station))
admin.add_view(
DropDown(
"Measurements",
icon="fa-solid fa-vials",
views=[
observations_views.MonthlyMeasurementView(
observations.MonthlyMeasurement
),
observations_views.SeasonalMeasurementView(
observations.SeasonalMeasurement
),
observations_views.YearlyMeasurementView(
observations.YearlyMeasurement
),
],
)
)
admin.add_view(
Link(
"V2 API docs",
Expand Down
47 changes: 46 additions & 1 deletion arpav_ppcv/webapp/admin/schemas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import datetime as dt
from typing import Optional
import uuid

import sqlmodel

from ...schemas.base import ObservationAggregationType
from ...schemas.base import (
ObservationAggregationType,
Season,
)


class ConfigurationParameterValueRead(sqlmodel.SQLModel):
Expand Down Expand Up @@ -59,3 +63,44 @@ class ObservationVariableRead(sqlmodel.SQLModel):
class CoverageConfigurationReadListItem(sqlmodel.SQLModel):
id: uuid.UUID
name: str


class VariableRead(sqlmodel.SQLModel):
id: uuid.UUID
name: str
description: str
unit: Optional[str]


class StationRead(sqlmodel.SQLModel):
id: uuid.UUID
name: str
code: str
type: str
longitude: float
latitude: float
active_since: Optional[dt.date]
active_until: Optional[dt.date]
altitude_m: Optional[float]


class MonthlyMeasurementRead(sqlmodel.SQLModel):
station: str
variable: str
date: dt.date
value: float


class SeasonalMeasurementRead(sqlmodel.SQLModel):
station: str
variable: str
year: int
season: Season
value: float


class YearlyMeasurementRead(sqlmodel.SQLModel):
station: str
variable: str
year: int
value: float
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Views for the admin app.
"""Views for the admin app's coverages.
The classes contained in this module are derived from
starlette_admin.contrib.sqlmodel.ModelView. This is done mostly for two reasons:
Expand All @@ -23,12 +23,12 @@
from starlette.requests import Request
from starlette_admin.contrib.sqlmodel import ModelView

from ... import database
from ...schemas import (
from .... import database
from ....schemas import (
coverages,
base,
)
from . import (
from .. import (
fields,
schemas as read_schemas,
)
Expand Down Expand Up @@ -116,6 +116,10 @@ class ConfigurationParameterView(ModelView):
),
)

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.icon = "fa-solid fa-quote-left"

async def get_pk_value(self, request: Request, obj: Any) -> Any:
# note: we need to cast the value, which is a uuid.UUID, to a string
# because starlette_admin just assumes that the value of a model's
Expand Down Expand Up @@ -313,6 +317,10 @@ class CoverageConfigurationView(ModelView):
)
exclude_fields_from_edit = ("coverage_id_pattern",)

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.icon = "fa-solid fa-map"

async def get_pk_value(self, request: Request, obj: Any) -> Any:
# note: we need to cast the value, which is a uuid.UUID, to a string
# because starlette_admin just assumes that the value of a model's
Expand Down
Loading

0 comments on commit e4b7e62

Please sign in to comment.