Skip to content

Commit

Permalink
Use DB model correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jterry64 committed Mar 29, 2024
1 parent 2122dfd commit 96463de
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
line_length = 88
multi_line_output = 3
include_trailing_comma = True
known_third_party = _pytest,aenum,affine,aiohttp,alembic,async_lru,asyncpg,aws_utils,boto3,botocore,click,docker,errors,fastapi,fiona,gdal_utils,geoalchemy2,geojson,gfw_pixetl,gino,gino_starlette,google,httpx,httpx_auth,logger,logging_utils,moto,numpy,orjson,osgeo,pandas,pendulum,pglast,psutil,psycopg2,pydantic,pyproj,pytest,pytest_asyncio,rasterio,retrying,shapely,sqlalchemy,sqlalchemy_utils,starlette,tileputty,typer
known_third_party = _pytest,aenum,affine,aiohttp,alembic,async_lru,asyncpg,aws_utils,boto3,botocore,click,docker,errors,fastapi,fiona,gdal_utils,geoalchemy2,geojson,gfw_pixetl,gino,gino_starlette,google,httpx,httpx_auth,logger,logging_utils,moto,numpy,orjson,osgeo,pandas,pendulum,pglast,psutil,psycopg2,pydantic,pyproj,pytest,pytest_asyncio,rasterio,shapely,sqlalchemy,sqlalchemy_utils,starlette,tileputty,typer
72 changes: 51 additions & 21 deletions app/crud/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from asyncpg import UniqueViolationError

from ..errors import BadRequestError, RecordAlreadyExistsError, RecordNotFoundError
from ..errors import RecordAlreadyExistsError, RecordNotFoundError
from ..models.orm.asset_metadata import AssetMetadata as ORMAssetMetadata
from ..models.orm.asset_metadata import FieldMetadata as ORMFieldMetadata
from ..models.orm.asset_metadata import RasterBandMetadata as ORMRasterBandMetadata
Expand Down Expand Up @@ -188,13 +188,19 @@ async def update_asset_metadata(asset_id: UUID, **data) -> ORMAssetMetadata:
if data:
await asset_metadata.update(**data).apply()

bands_metadata = []
if bands:
try:
bands_metadata = await update_band_metadata(asset_metadata.id, bands)
except RecordNotFoundError:
bands_metadata = await create_raster_band_metadata(
asset_metadata.id, **bands
)
for band in bands:
try:
pixel_meaning = band.pop("pixel_meaning")
band_metadata = await update_band_metadata(
asset_metadata.id, pixel_meaning, **band
)
except RecordNotFoundError:
bands_metadata = await create_raster_band_metadata(
asset_metadata.id, **bands
)
bands_metadata.append(band_metadata)

asset_metadata.bands = bands_metadata

Expand All @@ -213,6 +219,21 @@ async def update_asset_metadata(asset_id: UUID, **data) -> ORMAssetMetadata:

return asset_metadata

fields_metadata = []
if fields:
for field in fields:
try:
field_metadata = await update_field_metadata(
asset_metadata.id, field["name"], **field
)
except RecordNotFoundError:
field_metadata = await create_field_metadata(asset_metadata.id, **field)
fields_metadata.append(field_metadata)

asset_metadata.fields = fields_metadata

return asset_metadata


async def delete_asset_metadata(asset_id: UUID) -> ORMAssetMetadata:
asset_metadata: ORMAssetMetadata = await get_asset_metadata(asset_id)
Expand Down Expand Up @@ -251,22 +272,16 @@ async def update_field_metadata(
return field_metadata


async def update_band_metadata(metadata_id: UUID, bands) -> ORMFieldMetadata:
bands_metadata: List[
ORMRasterBandMetadata
] = await ORMRasterBandMetadata.query.where(
ORMRasterBandMetadata.asset_metadata_id == metadata_id
).gino.all()

if len(bands) != len(bands_metadata):
raise BadRequestError(
f"Update request must include metadata for {len(bands)} bands."
)
async def update_band_metadata(
metadata_id: UUID, pixel_meaning: str, **data
) -> ORMFieldMetadata:
band_metadata: ORMRasterBandMetadata = await get_asset_raster_band(
metadata_id, pixel_meaning
)

for band, band_metadata in zip(bands, bands_metadata):
await band_metadata.update(**band).apply()
await band_metadata.update(**data).apply()

return bands_metadata
return band_metadata


async def get_asset_fields(asset_metadata_id: UUID) -> List[ORMFieldMetadata]:
Expand Down Expand Up @@ -301,6 +316,21 @@ async def get_asset_field(asset_metadata_id: UUID, field_name: str) -> ORMFieldM
return field_metadata


async def get_asset_raster_band(
asset_metadata_id: UUID, pixel_meaning: str
) -> ORMRasterBandMetadata:
band_metadata: ORMRasterBandMetadata = await ORMRasterBandMetadata.get(
[asset_metadata_id, pixel_meaning]
)

if band_metadata is None:
raise RecordNotFoundError(
f"No band metadata record found for pixel meaning {pixel_meaning}."
)

return band_metadata


def update_metadata(row: Base, parent: Base):
"""Dynamically update metadata with parent metadata.
Expand Down
2 changes: 1 addition & 1 deletion app/models/orm/asset_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class RasterBandMetadata(db.Model):
name="asset_metadata_id_fk",
onupdate="CASCADE",
ondelete="CASCADE",
primary_key=True,
),
primary_key=True,
)
pixel_meaning = db.Column(db.String, primary_key=True)
description = db.Column(db.String)
Expand Down
6 changes: 1 addition & 5 deletions app/routes/assets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ async def update_asset(
request: AssetUpdateIn,
is_authorized: bool = Depends(is_admin),
) -> AssetResponse:
"""Update Asset metadata.
For raster band metadata, raster band updates must include all bands
in the correct order. If no updated is needed for a band, pass `{}`.
"""
"""Update Asset metadata."""

input_data = request.dict(exclude_none=True, by_alias=True)

Expand Down

0 comments on commit 96463de

Please sign in to comment.