| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | import tortoise.modelsfrom .field_generator import field_generatorfrom .constants import constantsclass 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 __repr__(self) -> str:        result = "Dump of " + self.__class__.__name__                if self.id is not None:            result = result + " #" + str(self.id)                result = result + "\n"        for count in dir(self):            if count[0] == "_":                continue            if count == "id":                continue            if count == "pk":                continue            field = self.__getattribute__(count)            if type(field) is str:                result = result + " " + count + ": \"" + field + "\"\n"                continue            if type(field) is int or type(field) is float:                result = result + " " + count + ": " + str(field) + "\n"                continue            if type(field) is bool:                result = result + " " + count + ": " + str(field) + "\n"                continue        return result            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            raise ValueError("Validated value is not same.")        except Exception as error:            value = self.__get(key)            model_name = type(self).__name__                        info = "Model \"" + model_name + "\" contain invalid "            info = info + "data in field \"" + key + "\". Content of "            info = info + "field is \"" + value + "\". Found error \""            info = info + str(error) + "\"."            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)                return self    class Meta:        app = constants.app_name()
 |