|
16 | 16 | from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, Iterable |
17 | 17 | from uuid import UUID, uuid4 |
18 | 18 |
|
| 19 | +from argilla._exceptions import ArgillaError |
19 | 20 | from argilla._models import ( |
20 | 21 | MetadataModel, |
21 | 22 | RecordModel, |
@@ -324,6 +325,9 @@ def __iter__(self): |
324 | 325 | def __getitem__(self, name: str): |
325 | 326 | return self.__responses_by_question_name[name] |
326 | 327 |
|
| 328 | + def __len__(self): |
| 329 | + return len(self.__responses) |
| 330 | + |
327 | 331 | def __repr__(self) -> str: |
328 | 332 | return {k: [{"value": v["value"]} for v in values] for k, values in self.to_dict().items()}.__repr__() |
329 | 333 |
|
@@ -354,10 +358,21 @@ def add(self, response: Response) -> None: |
354 | 358 | Args: |
355 | 359 | response: The response to add. |
356 | 360 | """ |
| 361 | + self._check_response_already_exists(response) |
| 362 | + |
357 | 363 | response.record = self.record |
358 | 364 | self.__responses.append(response) |
359 | 365 | self.__responses_by_question_name[response.question_name].append(response) |
360 | 366 |
|
| 367 | + def _check_response_already_exists(self, response: Response) -> None: |
| 368 | + """Checks if a response for the same question name and user id already exists""" |
| 369 | + for response in self.__responses_by_question_name[response.question_name]: |
| 370 | + if response.user_id == response.user_id: |
| 371 | + raise ArgillaError( |
| 372 | + f"Response for question with name {response.question_name!r} and user id {response.user_id!r} " |
| 373 | + f"already found. The responses for the same question name do not support more than one user" |
| 374 | + ) |
| 375 | + |
361 | 376 |
|
362 | 377 | class RecordSuggestions(Iterable[Suggestion]): |
363 | 378 | """This is a container class for the suggestions of a Record. |
|
0 commit comments