attachment.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import pathlib
  2. from .model import model
  3. from .proxy import proxy
  4. from .field_generator import field_generator
  5. from .constants import constants
  6. from .validators import validators
  7. from .exceptions import resources_not_exists
  8. class attachment(model):
  9. """
  10. That model store attachment to the item. Attachment is file, which could
  11. be downloaded by the user, like instruction in PDF, or used in frontend
  12. like images.
  13. Fields
  14. ------
  15. name : str
  16. Name of the attachment visible for user.
  17. description : str
  18. Description of the attachment visible for user.
  19. """
  20. name = field_generator.name()
  21. description = field_generator.description()
  22. resources = field_generator.path()
  23. @property
  24. def resources_path(self) -> pathlib.Path:
  25. return pathlib.Path(self.resources)
  26. def _validators(self) -> dict:
  27. """
  28. That function return dict of validators which would be used to
  29. validate text fields of the model.
  30. Returns
  31. -------
  32. dict
  33. Validators which would be used to validate text fields.
  34. """
  35. return {
  36. "name": validators.name,
  37. "description": validators.description,
  38. "resources": validators.path
  39. }
  40. class attachment_proxy(proxy):
  41. @classmethod
  42. def create(cls, resources: str) -> proxy:
  43. return cls(attachment(
  44. name = cls.__extract_name(resources),
  45. resources = resources,
  46. description = constants.empty_text()
  47. ))
  48. @classmethod
  49. def __extract_name(cls, resources: str) -> str:
  50. splited = resources.split(".")
  51. if len(splited) == 1:
  52. return splited[0]
  53. return str(".").join(splited[:-1])
  54. def set_name(self, target: str) -> object:
  55. self._target.name = target
  56. return self
  57. def set_description(self, target: str) -> object:
  58. self._target.description = target
  59. return self