diff --git a/store/app/crud/listings.py b/store/app/crud/listings.py index ad8e8f19..7ed477aa 100644 --- a/store/app/crud/listings.py +++ b/store/app/crud/listings.py @@ -3,7 +3,7 @@ import asyncio import logging -from store.app.crud.base import BaseCrud, InternalError, ItemNotFoundError +from store.app.crud.base import BaseCrud, ItemNotFoundError from store.app.model import Artifact, Listing, ListingTag logger = logging.getLogger(__name__) @@ -54,7 +54,7 @@ async def delete_tag(self, listing_id: str, tag: str) -> None: listing_tag = ListingTag.create(listing_id=listing_id, tag=tag) return await self._delete_item(listing_tag) - async def get_urdf_id( + async def get_latest_urdf_id( self, listing_id: str, ) -> str | None: @@ -63,9 +63,4 @@ async def get_urdf_id( urdfs = [artifact for artifact in artifacts if artifact.artifact_type == "urdf"] if len(urdfs) == 0: return None - if len(urdfs) > 1: - raise InternalError( - f"""More than one URDF found for listing {listing_id}. - This is due to incorrect data in the database, likely caused by a bug.""" - ) - return urdfs[0].id + return max(urdfs, key=lambda urdf: urdf.timestamp) diff --git a/store/app/model.py b/store/app/model.py index 828132ef..8ac6a070 100644 --- a/store/app/model.py +++ b/store/app/model.py @@ -5,6 +5,7 @@ expects (for example, converting a UUID into a string). """ +import time from datetime import datetime, timedelta from typing import Literal, Self @@ -103,6 +104,7 @@ class Artifact(RobolistBaseModel): artifact_type: ArtifactType sizes: list[ArtifactSize] | None = None description: str | None = None + timestamp: int @classmethod def create( @@ -120,6 +122,7 @@ def create( artifact_type=artifact_type, sizes=sizes, description=description, + timestamp=int(time.time()), ) diff --git a/store/app/routers/urdf.py b/store/app/routers/urdf.py index 42669ee8..73869804 100644 --- a/store/app/routers/urdf.py +++ b/store/app/routers/urdf.py @@ -54,12 +54,12 @@ async def upload_urdf( return UserInfoResponse(urdf_id=artifact.id) -@urdf_router.get("/{listing_id}") +@urdf_router.get("/latest/{listing_id}") async def listing_urdf( listing_id: str, crud: Annotated[Crud, Depends(Crud.get)], ) -> RedirectResponse: - urdf_id = await crud.get_urdf_id(listing_id) + urdf_id = await crud.get_latest_urdf_id(listing_id) if urdf_id is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) urdf_url = f"{settings.site.artifact_base_url}/{get_urdf_name(urdf_id)}"