You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

  1. import abc
  2. import uuid
  3. from typing import Any, Dict
  4. from pydantic import BaseModel, validator
  5. from api.models import Project
  6. from examples.models import Example
  7. class BaseData(BaseModel, abc.ABC):
  8. filename: str
  9. @classmethod
  10. def parse(cls, **kwargs):
  11. return cls.parse_obj(kwargs)
  12. def __hash__(self):
  13. return hash(tuple(self.dict()))
  14. @abc.abstractmethod
  15. def create(self, project: Project, meta: Dict[Any, Any]) -> Example:
  16. raise NotImplementedError('Please implement this method in the subclass.')
  17. class TextData(BaseData):
  18. text: str
  19. @validator('text')
  20. def text_is_not_empty(cls, value: str):
  21. if value:
  22. return value
  23. else:
  24. raise ValueError('is not empty.')
  25. def create(self, project: Project, meta: Dict[Any, Any]) -> Example:
  26. return Example(
  27. uuid=uuid.uuid4(),
  28. project=project,
  29. filename=self.filename,
  30. text=self.text,
  31. meta=meta
  32. )
  33. class FileData(BaseData):
  34. def create(self, project: Project, meta: Dict[Any, Any]) -> Example:
  35. return Example(
  36. uuid=uuid.uuid4(),
  37. project=project,
  38. filename=self.filename,
  39. meta=meta
  40. )