|  | @@ -4,12 +4,82 @@ from .field_generator import field_generator
 | 
	
		
			
				|  |  |  from .constants import constants
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  class model(tortoise.models.Model):
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    This is base class for all models in the app. It store base fields, like
 | 
	
		
			
				|  |  | +    ID, and have functions, which is necessary to validate object before 
 | 
	
		
			
				|  |  | +    save it in the database.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    Fields
 | 
	
		
			
				|  |  | +    ------
 | 
	
		
			
				|  |  | +    id : field_generator.id
 | 
	
		
			
				|  |  | +        ID of the object.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    Methods
 | 
	
		
			
				|  |  | +    -------
 | 
	
		
			
				|  |  | +    _validators : dict
 | 
	
		
			
				|  |  | +        Return dict of validators, which would be used to validate fields of
 | 
	
		
			
				|  |  | +        the object. For example, when object have field "name", and function 
 | 
	
		
			
				|  |  | +        "val_name(name: str) -> str" would be used to validate it, then that
 | 
	
		
			
				|  |  | +        dict must contain "{ 'name': val_name }".
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    __validate_item : None
 | 
	
		
			
				|  |  | +        That validate sinle item of the object. To pass validation, result
 | 
	
		
			
				|  |  | +        from validating function must be same as before validaion, and that
 | 
	
		
			
				|  |  | +        function must not raise and error.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    __get : any
 | 
	
		
			
				|  |  | +        That return content of given field of object.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    validate : object
 | 
	
		
			
				|  |  | +        That run all validators. When any validators found problem with 
 | 
	
		
			
				|  |  | +        content of object, then raise an ValueError. If all went nice, then
 | 
	
		
			
				|  |  | +        return self.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    Classes
 | 
	
		
			
				|  |  | +    -------
 | 
	
		
			
				|  |  | +    Meta
 | 
	
		
			
				|  |  | +        Fields
 | 
	
		
			
				|  |  | +        ------
 | 
	
		
			
				|  |  | +        app : str
 | 
	
		
			
				|  |  | +            Name of the app.
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      id = field_generator.id()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def _validators(self) -> dict:
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        That return dict of fields, which must be validated, with validators
 | 
	
		
			
				|  |  | +        which must be used. Format of the dict is "{ 'name of the field': 
 | 
	
		
			
				|  |  | +        validation_function }". By default it is empty, and none of the field
 | 
	
		
			
				|  |  | +        is not validating.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Returns
 | 
	
		
			
				|  |  | +        -------
 | 
	
		
			
				|  |  | +        dict
 | 
	
		
			
				|  |  | +            Dictionary with validators for fields.
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          return dict()
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def __validate_item(self, validator: callable, key: str) -> None:
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        It validate single field of the object. When validation is passed, 
 | 
	
		
			
				|  |  | +        it return without errors, in other way it raise ValueError.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Parameters
 | 
	
		
			
				|  |  | +        ----------
 | 
	
		
			
				|  |  | +        validator : callable
 | 
	
		
			
				|  |  | +            Validator which would be used to validate field.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        key : str
 | 
	
		
			
				|  |  | +            Name of the field.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Raises
 | 
	
		
			
				|  |  | +        ------
 | 
	
		
			
				|  |  | +        ValueError
 | 
	
		
			
				|  |  | +            When field contain incorrect data.
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          try:
 | 
	
		
			
				|  |  |              if validator(self.__get(key)) == self.__get(key):
 | 
	
		
			
				|  |  |                  return
 | 
	
	
		
			
				|  | @@ -28,9 +98,40 @@ class model(tortoise.models.Model):
 | 
	
		
			
				|  |  |              raise ValueError(info)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def __get(self, key: str) -> any:
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        That return field with given name.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Parameters
 | 
	
		
			
				|  |  | +        ----------
 | 
	
		
			
				|  |  | +        key : str
 | 
	
		
			
				|  |  | +            Name of the field.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Returns
 | 
	
		
			
				|  |  | +        -------
 | 
	
		
			
				|  |  | +        any
 | 
	
		
			
				|  |  | +            Value of the key.
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          return self.__getattribute__(key)
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      def validate(self) -> object:
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        That function make full validation of the object. It validate all 
 | 
	
		
			
				|  |  | +        fields, which is in the __validators() dict. When validation had 
 | 
	
		
			
				|  |  | +        been passed, it return self. When validation couldn't being passed
 | 
	
		
			
				|  |  | +        it raises ValueError.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Returns
 | 
	
		
			
				|  |  | +        -------
 | 
	
		
			
				|  |  | +        model
 | 
	
		
			
				|  |  | +            Object instance itself.
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Raises
 | 
	
		
			
				|  |  | +        ------
 | 
	
		
			
				|  |  | +        ValueError
 | 
	
		
			
				|  |  | +            When any field contain incorrect data.
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          for key, validator in self._validators().items():
 | 
	
		
			
				|  |  |              self.__validate_item(validator, key)
 | 
	
		
			
				|  |  |          
 |