def validate(cls, document): for key in list(document.keys()): if key not in cls.schema or document[key] is None: del document[key] try: if cls.schema[key]['type'] == 'datetime': document[key] = parser.parse(document[key]) elif cls.schema[key]['type'] == 'object_id': document[key] = ObjectId(document[key]) except KeyError: pass validator = Validator( schema=cls.schema ) if not validator.validate(document): (document, errors) = cls._remove_unknown(document, validator.errors) if len(errors): from core.helpers.raise_error import raise_error raise_error('validation', 'fields_invalid', code=400, fields=errors) return document
def exponentiation(number, exponent): if Validator.number_control([number, exponent])[0]: number = Validator.number_control([number, exponent])[1][0] exponent = Validator.number_control([number, exponent])[1][1] return number**exponent else: return False
def summation_list(number_list): total = 0 if Validator.number_control(number_list)[0]: number_list = Validator.number_control(number_list)[1] for number in number_list: total += number return total, type(total)
def percent(number, ratio): if Validator.number_control([number, ratio])[0]: number = Validator.number_control([number, ratio])[1][0] ratio = Validator.number_control(([number, ratio]))[1][1] return (number * ratio) / 100 else: return False
def remainder(first_number, second_number): if Validator.number_control([first_number, second_number])[0]: first_number = Validator.number_control( [first_number, second_number])[1][0] second_number = Validator.number_control( [first_number, second_number])[1][1] return first_number % second_number else: return False
def summation(first_number, second_number): if Validator.number_control([first_number, second_number])[0]: first_number = Validator.number_control( [first_number, second_number])[1][0] second_number = Validator.number_control( [first_number, second_number])[1][1] return first_number + second_number else: return False
def multiplication_list(multiplier_list): multiply = 1 if Validator.number_control(multiplier_list)[0]: multiplier_list = Validator.number_control(multiplier_list)[1] for number in multiplier_list: multiply *= number return multiply else: return False
def multiplication(multiplicand, multiplier): if Validator.number_control([multiplicand, multiplier])[0]: multiplicand = Validator.number_control([multiplicand, multiplier])[1][0] multiplier = Validator.number_control([multiplicand, multiplier])[1][1] return multiplicand * multiplier else: return False
def rea_abs(number): if Validator.number_control([number])[0]: number = Validator.number_control([number])[1][0] if number > 0: return number else: return -number else: return False
def rea_sqrt(number, expo, boolean): if Validator.number_control([number, expo])[0]: number = Validator.number_control([number])[1][0] expo = Validator.number_control([number, expo])[1][1] if number > 0 and expo >= 0: sqrt_number: Union[float, Any] = number**(1 / expo) return ReaMath.call_sqrt(sqrt_number, boolean) else: return False else: return False
def extraction(first_number, second_number): if Validator.number_control([first_number, second_number])[0]: first_number = Validator.number_control( [first_number, second_number])[1][0] second_number = Validator.number_control( [first_number, second_number])[1][1] if first_number < second_number: return second_number - first_number else: return first_number - second_number else: return False
def division_floor(divided, divisor): if Validator.number_control([divided, divisor])[0]: divided = Validator.number_control([divided, divisor])[1][0] divisor = Validator.number_control([divided, divisor])[1][1] if divided != 0 and divisor != 0: if divided > divisor: return divided // divisor else: return divisor // divided else: return False else: return False
def different_percent(first_number, second_number, boolean): if Validator.number_control([first_number, second_number])[0]: first_number = Validator.number_control( [first_number, second_number])[1][0] second_number = Validator.number_control( [first_number, second_number])[1][1] if boolean: if first_number < second_number: return (first_number / second_number) * 100 else: return (second_number / first_number) * 100 else: if first_number > second_number: return (first_number / second_number) * 100 else: return (second_number / first_number) * 100 else: return False
def validate(cls, document): for key in list(document.keys()): if key not in cls.schema: del document[key] try: if cls.schema[key]['type'] == 'datetime': document[key] = parser.parse(document[key]) except KeyError: pass validator = Validator( schema=cls.schema ) if not validator.validate(document): print(validator.errors) abort(400) return document
def validator(): return Validator()
def average(number_list): if Validator.number_control(number_list)[0]: number_list = Validator.number_control(number_list)[1] return ReaMath.summation_list(number_list) / len(number_list) else: return False