Files
anyway/backend/src/structs/trip.py
Remy Moll 4e07c10969
Some checks failed
Run testing on the backend code / Build (pull_request) Has been cancelled
Run linting on the backend code / Build (pull_request) Has been cancelled
Build and deploy the backend to staging / Deploy to staging (pull_request) Has been cancelled
Build and deploy the backend to staging / Build and push image (pull_request) Has been cancelled
actually use fastapi lifetime manager to setup logging
2024-12-29 14:45:41 +01:00

49 lines
1.6 KiB
Python

"""Definition of the Trip class."""
from uuid import uuid4, UUID
from pydantic import BaseModel, Field
from pymemcache.client.base import Client
from .linked_landmarks import LinkedLandmarks
class Trip(BaseModel):
""""
A Trip represents the final guided tour that can be passed to frontend.
Attributes:
uuid: unique identifier for this particular trip.
total_time: duration of the trip (in minutes).
first_landmark_uuid: unique identifier of the first Landmark to visit.
Methods:
from_linked_landmarks: create a Trip from LinkedLandmarks object.
"""
uuid: UUID = Field(default_factory=uuid4)
total_time: int
first_landmark_uuid: UUID
@classmethod
def from_linked_landmarks(cls, landmarks: LinkedLandmarks, cache_client: Client) -> "Trip":
"""
Initialize a new Trip object and ensure it is stored in the cache.
"""
trip = Trip(
total_time = landmarks.total_time,
first_landmark_uuid = landmarks[0].uuid
)
# Store the trip in the cache
cache_client.set(f"trip_{trip.uuid}", trip)
# Make sure to await the result (noreply=False).
# Otherwise the cache might not be inplace when the trip is actually requested.
cache_client.set_many({f"landmark_{landmark.uuid}": landmark for landmark in landmarks},
expire=3600, noreply=False)
# is equivalent to:
# for landmark in landmarks:
# cache_client.set(f"landmark_{landmark.uuid}", landmark, expire=3600)
return trip