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.

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