Spaces:
Sleeping
Sleeping
Download app/database/objectid.py from coder160/backend-directorio-online: direct link, hf CLI and curl.
- Browser
- Download file 737 Bytes
-
https://huggingface.co/spaces/coder160/backend-directorio-online/resolve/main/app/database/objectid.py
- Command line
-
hf download hf://spaces/coder160/backend-directorio-online/app/database/objectid.py
-
curl -L -o objectid.py https://huggingface.co/spaces/coder160/backend-directorio-online/resolve/main/app/database/objectid.py
737 Bytes
| """Utilidades para trabajar con ``ObjectId`` de MongoDB.""" | |
| from __future__ import annotations | |
| from bson import ObjectId | |
| from bson.errors import InvalidId | |
| from app.exceptions import InvalidIdError | |
| def to_object_id(value: str | ObjectId) -> ObjectId: | |
| """Convierte un valor a :class:`bson.ObjectId`. | |
| Args: | |
| value: Identificador como cadena o ``ObjectId``. | |
| Returns: | |
| El ``ObjectId`` correspondiente. | |
| Raises: | |
| InvalidIdError: Si el valor no es un ObjectId válido. | |
| """ | |
| if isinstance(value, ObjectId): | |
| return value | |
| try: | |
| return ObjectId(value) | |
| except (InvalidId, TypeError) as exc: | |
| raise InvalidIdError(f"'{value}' no es un identificador válido.") from exc | |