model.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import tortoise.models
  2. from .field_generator import field_generator
  3. from .constants import constants
  4. class model(tortoise.models.Model):
  5. """
  6. This is base class for all models in the app. It store base fields, like
  7. ID, and have functions, which is necessary to validate object before
  8. save it in the database.
  9. Fields
  10. ------
  11. id : field_generator.id
  12. ID of the object.
  13. Methods
  14. -------
  15. _validators : dict
  16. Return dict of validators, which would be used to validate fields of
  17. the object. For example, when object have field "name", and function
  18. "val_name(name: str) -> str" would be used to validate it, then that
  19. dict must contain "{ 'name': val_name }".
  20. __validate_item : None
  21. That validate sinle item of the object. To pass validation, result
  22. from validating function must be same as before validaion, and that
  23. function must not raise and error.
  24. __get : any
  25. That return content of given field of object.
  26. validate : object
  27. That run all validators. When any validators found problem with
  28. content of object, then raise an ValueError. If all went nice, then
  29. return self.
  30. Classes
  31. -------
  32. Meta
  33. Fields
  34. ------
  35. app : str
  36. Name of the app.
  37. """
  38. id = field_generator.id()
  39. def _validators(self) -> dict:
  40. """
  41. That return dict of fields, which must be validated, with validators
  42. which must be used. Format of the dict is "{ 'name of the field':
  43. validation_function }". By default it is empty, and none of the field
  44. is not validating.
  45. Returns
  46. -------
  47. dict
  48. Dictionary with validators for fields.
  49. """
  50. return dict()
  51. def __validate_item(self, validator: callable, key: str) -> None:
  52. """
  53. It validate single field of the object. When validation is passed,
  54. it return without errors, in other way it raise ValueError.
  55. Parameters
  56. ----------
  57. validator : callable
  58. Validator which would be used to validate field.
  59. key : str
  60. Name of the field.
  61. Raises
  62. ------
  63. ValueError
  64. When field contain incorrect data.
  65. """
  66. try:
  67. if validator(self.__get(key)) == self.__get(key):
  68. return
  69. raise ValueError("Validated value is not same.")
  70. except Exception as error:
  71. value = self.__get(key)
  72. model_name = type(self).__name__
  73. info = "Model \"" + model_name + "\" contain invalid "
  74. info = info + "data in field \"" + key + "\". Content of "
  75. info = info + "field is \"" + value + "\". Found error \""
  76. info = info + str(error) + "\"."
  77. raise ValueError(info)
  78. def __get(self, key: str) -> any:
  79. """
  80. That return field with given name.
  81. Parameters
  82. ----------
  83. key : str
  84. Name of the field.
  85. Returns
  86. -------
  87. any
  88. Value of the key.
  89. """
  90. return self.__getattribute__(key)
  91. def validate(self) -> object:
  92. """
  93. That function make full validation of the object. It validate all
  94. fields, which is in the __validators() dict. When validation had
  95. been passed, it return self. When validation couldn't being passed
  96. it raises ValueError.
  97. Returns
  98. -------
  99. model
  100. Object instance itself.
  101. Raises
  102. ------
  103. ValueError
  104. When any field contain incorrect data.
  105. """
  106. for key, validator in self._validators().items():
  107. self.__validate_item(validator, key)
  108. return self
  109. class Meta:
  110. app = constants.app_name()