attachment.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 .attachment_file import attachment_file
  8. from .exceptions import resources_not_exists
  9. class attachment(model):
  10. """
  11. That model store attachment to the item. Attachment is file, which could
  12. be downloaded by the user, like instruction in PDF, or used in frontend
  13. like images.
  14. Fields
  15. ------
  16. name : str
  17. Name of the attachment visible for user.
  18. description : str
  19. Description of the attachment visible for user.
  20. """
  21. name = field_generator.name()
  22. description = field_generator.description()
  23. resources = field_generator.path()
  24. def _validators(self) -> dict:
  25. """
  26. That function return dict of validators which would be used to
  27. validate text fields of the model.
  28. Returns
  29. -------
  30. dict
  31. Validators which would be used to validate text fields.
  32. """
  33. return {
  34. "name": validators.name,
  35. "description": validators.description,
  36. "resources": validators.path
  37. }
  38. class attachment_proxy(proxy):
  39. """
  40. That class is proxy for the attachment. It could be used to working
  41. witch attachments in cleaner and better way.
  42. Methods
  43. -------
  44. @classmethod create : proxy
  45. That create new attachment from that name.
  46. set_name : None
  47. That change visible name of the attachment.
  48. set_description : None
  49. That change description of the attachment.
  50. """
  51. @classmethod
  52. def create(cls, name: str, resources: attachment_file) -> proxy:
  53. """
  54. That create new proxy with new object, which does not exists in
  55. database inside.
  56. Parameters
  57. ----------
  58. name : str
  59. Name of the new attachment.
  60. resources : str
  61. Path in the resources directory to the attachment file.
  62. Raises
  63. ------
  64. resources_not_exists
  65. When attachment file does not exists on disk.
  66. Returns
  67. -------
  68. proxy
  69. New proxy with new attachment inside it.
  70. """
  71. if not resources.exists():
  72. raise resources_not_exists(resources.path)
  73. return cls(attachment(
  74. name = name,
  75. description = constants.empty_text(),
  76. resources = resources.name
  77. ))
  78. def set_resources(self, target: attachment_file) -> None:
  79. """
  80. That change path to the attachment file in the resources directory.
  81. Parameters
  82. ----------
  83. target : pathlib.Path
  84. Path to new attachment file in the resources directory.
  85. Raises
  86. ------
  87. resources_not_exists
  88. When attachment file does not exists on disk.
  89. """
  90. if not target.exists():
  91. raise resources_not_exists(target.path)
  92. self._target.resources = target.name
  93. def set_name(self, target: str) -> None:
  94. """
  95. That change name of the attachment
  96. Parameters
  97. ----------
  98. target : str
  99. New name of the attachment to set.
  100. """
  101. self._target.name = target
  102. def set_description(self, target: str) -> None:
  103. """
  104. That chanhe description of the attachment.
  105. Parameters
  106. ----------
  107. target : str
  108. New description of the attachment to set.
  109. """
  110. self._target.description = target