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.

42 lines
1.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import abc
  2. import uuid
  3. from typing import Any, Dict
  4. from pydantic import BaseModel, validator
  5. from examples.models import Example
  6. from projects.models import Project
  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(uuid=uuid.uuid4(), project=project, filename=self.filename, text=self.text, meta=meta)
  27. class FileData(BaseData):
  28. def create(self, project: Project, meta: Dict[Any, Any]) -> Example:
  29. return Example(uuid=uuid.uuid4(), project=project, filename=self.filename, meta=meta)