Skip to content

Commit

Permalink
Change route that gets unique urdf to route that gets latest
Browse files Browse the repository at this point in the history
  • Loading branch information
chennisden committed Jul 30, 2024
1 parent 60b97e5 commit 6941104
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
11 changes: 3 additions & 8 deletions store/app/crud/listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
3 changes: 3 additions & 0 deletions store/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -103,6 +104,7 @@ class Artifact(RobolistBaseModel):
artifact_type: ArtifactType
sizes: list[ArtifactSize] | None = None
description: str | None = None
timestamp: int

@classmethod
def create(
Expand All @@ -120,6 +122,7 @@ def create(
artifact_type=artifact_type,
sizes=sizes,
description=description,
timestamp=int(time.time()),
)


Expand Down
4 changes: 2 additions & 2 deletions store/app/routers/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand Down

0 comments on commit 6941104

Please sign in to comment.