Skip to content

Commit

Permalink
feat: add DatasetCreateValidator and move some validations from context
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcalvo committed Jun 13, 2024
1 parent 96a0bde commit 04e9e92
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
16 changes: 6 additions & 10 deletions argilla-server/src/argilla_server/contexts/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
)
from argilla_server.models.suggestions import SuggestionCreateWithRecordId
from argilla_server.search_engine import SearchEngine
from argilla_server.validators.datasets import DatasetCreateValidator
from argilla_server.validators.responses import (
ResponseCreateValidator,
ResponseUpdateValidator,
Expand Down Expand Up @@ -120,23 +121,18 @@ async def list_datasets_by_workspace_id(db: AsyncSession, workspace_id: UUID) ->


async def create_dataset(db: AsyncSession, dataset_attrs: dict):
if await Workspace.get(db, dataset_attrs["workspace_id"]) is None:
raise UnprocessableEntityError(f"Workspace with id `{dataset_attrs['workspace_id']}` not found")

if await Dataset.get_by(db, name=dataset_attrs["name"], workspace_id=dataset_attrs["workspace_id"]):
raise NotUniqueError(
f"Dataset with name `{dataset_attrs['name']}` already exists for workspace with id `{dataset_attrs['workspace_id']}`"
)

return await Dataset.create(
db,
dataset = Dataset(
name=dataset_attrs["name"],
guidelines=dataset_attrs["guidelines"],
allow_extra_metadata=dataset_attrs["allow_extra_metadata"],
distribution=dataset_attrs["distribution"],
workspace_id=dataset_attrs["workspace_id"],
)

await DatasetCreateValidator.validate(db, dataset)

return await dataset.save(db)


async def _count_required_fields_by_dataset_id(db: AsyncSession, dataset_id: UUID) -> int:
return (await db.execute(select(func.count(Field.id)).filter_by(dataset_id=dataset_id, required=True))).scalar_one()
Expand Down
37 changes: 37 additions & 0 deletions argilla-server/src/argilla_server/validators/datasets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from uuid import UUID

from sqlalchemy.ext.asyncio import AsyncSession

from argilla_server.errors.future import NotUniqueError, UnprocessableEntityError
from argilla_server.models import Dataset, Workspace


class DatasetCreateValidator:
@classmethod
async def validate(cls, db, dataset: Dataset) -> None:
await cls._validate_workspace_is_present(db, dataset.workspace_id)
await cls._validate_name_is_not_duplicated(db, dataset.name, dataset.workspace_id)

@classmethod
async def _validate_workspace_is_present(cls, db, workspace_id: UUID) -> None:
if await Workspace.get(db, workspace_id) is None:
raise UnprocessableEntityError(f"Workspace with id `{workspace_id}` not found")

@classmethod
async def _validate_name_is_not_duplicated(cls, db, name: str, workspace_id: UUID) -> None:
if await Dataset.get_by(db, name=name, workspace_id=workspace_id):
raise NotUniqueError(f"Dataset with name `{name}` already exists for workspace with id `{workspace_id}`")

0 comments on commit 04e9e92

Please sign in to comment.