diff --git a/arpav_ppcv/webapp/api_v2/routers/coverages.py b/arpav_ppcv/webapp/api_v2/routers/coverages.py index db437ec6..2039ab3b 100644 --- a/arpav_ppcv/webapp/api_v2/routers/coverages.py +++ b/arpav_ppcv/webapp/api_v2/routers/coverages.py @@ -184,29 +184,11 @@ def list_coverage_identifiers( _, unfiltered_total = db.list_coverage_identifiers( db_session, limit=1, offset=0, include_total=True ) - records = [] - for cov_internal in cov_internals: - thredds_url_fragment = cov_internal.configuration.get_thredds_url_fragment( - cov_internal.identifier - ) - wms_base_url = "/".join( - ( - settings.thredds_server.base_url, - settings.thredds_server.wms_service_url_fragment, - thredds_url_fragment, - ) - ) - records.append( - coverage_schemas.CoverageIdentifierReadListItem( - identifier=cov_internal.identifier, - wms_base_url=wms_base_url, - wms_main_layer_name=cov_internal.configuration.wms_main_layer_name, - ) - ) return coverage_schemas.CoverageIdentifierList.from_items( - records, - request=request, + cov_internals, + request, + settings=settings, limit=list_params.limit, offset=list_params.offset, filtered_total=filtered_total, diff --git a/arpav_ppcv/webapp/api_v2/schemas/coverages.py b/arpav_ppcv/webapp/api_v2/schemas/coverages.py index cca316d8..db621ffd 100644 --- a/arpav_ppcv/webapp/api_v2/schemas/coverages.py +++ b/arpav_ppcv/webapp/api_v2/schemas/coverages.py @@ -4,8 +4,9 @@ import pydantic from fastapi import Request -from .base import WebResourceList +from ....config import ArpavPpcvSettings from ....schemas import coverages as app_models +from .base import WebResourceList class ConfigurationParameterValueEmbeddedInConfigurationParameter(pydantic.BaseModel): @@ -110,9 +111,40 @@ class CoverageConfigurationList(WebResourceList): class CoverageIdentifierReadListItem(pydantic.BaseModel): identifier: str + related_coverage_configuration_url: str wms_base_url: str wms_main_layer_name: str | None = None + @classmethod + def from_db_instance( + cls, + instance: app_models.CoverageInternal, + settings: ArpavPpcvSettings, + request: Request, + ) -> "CoverageIdentifierReadListItem": + thredds_url_fragment = instance.configuration.get_thredds_url_fragment( + instance.identifier + ) + wms_base_url = "/".join( + ( + settings.thredds_server.base_url, + settings.thredds_server.wms_service_url_fragment, + thredds_url_fragment, + ) + ) + + return cls( + identifier=instance.identifier, + wms_base_url=wms_base_url, + wms_main_layer_name=instance.configuration.wms_main_layer_name, + related_coverage_configuration_url=str( + request.url_for( + "get_coverage_configuration", + coverage_configuration_id=instance.configuration.id, + ) + ), + ) + class CoverageIdentifierList(WebResourceList): items: list[CoverageIdentifierReadListItem] @@ -121,9 +153,10 @@ class CoverageIdentifierList(WebResourceList): @classmethod def from_items( cls, - items: typing.Sequence[str], + items: typing.Sequence[app_models.CoverageInternal], request: Request, *, + settings: ArpavPpcvSettings, limit: int, offset: int, filtered_total: int, @@ -134,7 +167,10 @@ def from_items( links=cls._get_list_links( request, limit, offset, filtered_total, len(items) ), - items=[i for i in items], + items=[ + CoverageIdentifierReadListItem.from_db_instance(i, settings, request) + for i in items + ], )