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.

52 lines
1.3 KiB

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