|
32 | 32 | _format_dataset_url, |
33 | 33 | _format_parquet_url, |
34 | 34 | ) |
| 35 | +from database.exceptions import DuplicatePrimaryKeyError, ForeignKeyConstraintError |
35 | 36 | from database.users import User |
36 | 37 | from routers.dependencies import ( |
37 | 38 | Pagination, |
|
40 | 41 | fetch_user_or_raise, |
41 | 42 | userdb_connection, |
42 | 43 | ) |
43 | | -from routers.types import CasualString128, IntegerRange, SystemString64, integer_range_regex |
| 44 | +from routers.types import ( |
| 45 | + CasualString128, |
| 46 | + Identifier, |
| 47 | + IntegerRange, |
| 48 | + SystemString64, |
| 49 | + integer_range_regex, |
| 50 | +) |
44 | 51 | from schemas.datasets.openml import DatasetMetadata, DatasetStatus, Feature, FeatureType |
45 | 52 |
|
46 | 53 | router = APIRouter(prefix="/datasets", tags=["datasets"]) |
|
50 | 57 | path="/tag", |
51 | 58 | ) |
52 | 59 | async def tag_dataset( |
53 | | - data_id: Annotated[int, Body()], |
| 60 | + data_id: Annotated[Identifier, Body()], |
54 | 61 | tag: Annotated[str, SystemString64], |
55 | 62 | user: Annotated[User, Depends(fetch_user_or_raise)], |
56 | | - expdb_db: Annotated[AsyncConnection, Depends(expdb_connection)] = None, |
| 63 | + expdb_db: Annotated[AsyncConnection, Depends(expdb_connection)], |
57 | 64 | ) -> dict[str, dict[str, Any]]: |
58 | | - assert expdb_db is not None # noqa: S101 |
59 | | - tags = await database.datasets.get_tags_for(data_id, expdb_db) |
60 | | - if tag.casefold() in [t.casefold() for t in tags]: |
| 65 | + try: |
| 66 | + await database.datasets.tag(data_id, tag, user_id=user.user_id, connection=expdb_db) |
| 67 | + except ForeignKeyConstraintError: |
| 68 | + msg = f"Dataset {data_id} not found." |
| 69 | + raise DatasetNotFoundError(msg, code=472) from None |
| 70 | + except DuplicatePrimaryKeyError: |
61 | 71 | msg = f"Dataset {data_id} already tagged with {tag!r}." |
62 | | - raise TagAlreadyExistsError(msg) |
| 72 | + raise TagAlreadyExistsError(msg) from None |
| 73 | + |
| 74 | + logger.info("Dataset {data_id} tagged '{tag}'.", data_id=data_id, tag=tag) |
| 75 | + |
| 76 | + tags = await database.datasets.get_tags_for(data_id, expdb_db) |
63 | 77 |
|
64 | | - await database.datasets.tag(data_id, tag, user_id=user.user_id, connection=expdb_db) |
65 | | - logger.info("Dataset {dataset_id} tagged '{tag}'.", dataset_id=data_id, tag=tag) |
66 | 78 | return { |
67 | | - "data_tag": {"id": str(data_id), "tag": [*tags, tag]}, |
| 79 | + "data_tag": {"id": str(data_id), "tag": tags}, |
68 | 80 | } |
69 | 81 |
|
70 | 82 |
|
|
0 commit comments