author.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from .model import model
  2. from .proxy import proxy
  3. from .field_generator import field_generator
  4. from .constants import constants
  5. from .validators import validators
  6. class author(model):
  7. """
  8. This model store author or manufacturer who create item. It has name
  9. and surname, because items created by person exists more than created
  10. by companies. Name and surname fields would be displayed different way
  11. when is_person is set to False.
  12. Fields
  13. ------
  14. name : str
  15. Name of the person, or name of the company.
  16. surname : str
  17. Surname of the person, or second part of the name of the company.
  18. description : str
  19. Description of the author or company.
  20. is_person : bool
  21. Binary flag, which is True when author is the person, or False
  22. when author is the company.
  23. is_company : bool
  24. Binary flag, which is True when author is the company, or False
  25. when author is the person.
  26. Methods
  27. -------
  28. _validators() : dict
  29. Return dict of validator for text fields.
  30. """
  31. name = field_generator.name()
  32. surname = field_generator.surname()
  33. description = field_generator.description()
  34. is_person = field_generator.boolean()
  35. @property
  36. def is_company(self) -> bool:
  37. """ Reverse flag for is_person. """
  38. return not self.is_person
  39. def _validators(self) -> dict:
  40. """
  41. It return dict of validators for text fields.
  42. Returns
  43. -------
  44. dict
  45. Validators for text fields.
  46. """
  47. return {
  48. "name": validators.name,
  49. "surname": validators.surname,
  50. "description": validators.description
  51. }
  52. class author_proxy(proxy):
  53. """
  54. This class is proxy for author. It could be used to make work with
  55. author objects easier and cleaner.
  56. Methods
  57. -------
  58. @classmethod create : proxy
  59. That method is used to creating new objects.
  60. set_name : None
  61. That set new name and surname of the author.
  62. set_description : None
  63. That set new description of the author.
  64. """
  65. @classmethod
  66. def create(cls, name: str, surname: str) -> proxy:
  67. """
  68. That create new instance of author with given name and surnme, other
  69. fields is set to defaults, that mean description is empty, and author
  70. is the person, not a company.
  71. Parameters
  72. ----------
  73. name : str
  74. Name of the author.
  75. surname : str
  76. Surname of the author.
  77. Returns
  78. -------
  79. proxy
  80. New proxy with new instance of author inside it.
  81. """
  82. return cls(author(
  83. name = name,
  84. surname = surname,
  85. is_person = True,
  86. description = constants.empty_text()
  87. ))
  88. def set_name(self, name: str, surname: str) -> proxy:
  89. """
  90. It set new name and surname of the author.
  91. Parameters
  92. ----------
  93. name : str
  94. New name of the author.
  95. surname : str
  96. Surname of the author.
  97. """
  98. self._target.name = name
  99. self._target.surname = surname
  100. return self
  101. def set_description(self, target: str) -> proxy:
  102. """
  103. It set new description of the author.
  104. Parameters
  105. ----------
  106. target : str
  107. Description of the author.
  108. """
  109. self._target.description = target
  110. return self
  111. def make_person(self) -> proxy:
  112. """
  113. It make author the person.
  114. """
  115. self._target.is_person = True
  116. def make_company(self) -> proxy:
  117. """
  118. It make author the company.
  119. """
  120. self._target.is_person = False
  121. return self