def validate_phone_number(value): try: z = phonenumbers.parse(value, None) except Exception: raise CommonException(code=codes.VALIDATION_ERROR, detail=messages.PHONE_INCORRECT) if not phonenumbers.is_valid_number(z): raise CommonException(code=codes.VALIDATION_ERROR, detail=messages.PHONE_INCORRECT)
def validate_phone_number(value): """ phone number validator """ if len(value) != 0: try: z = phonenumbers.parse(value, None) except Exception: raise CommonException(detail={"phone": messages.PHONE_INCORRECT}) if not phonenumbers.is_valid_number(z): raise CommonException(detail={"phone": messages.PHONE_INCORRECT}) return value
def validate_login(value): if not re.match(constants.REGEX_FOR_FIRST_SYMBOL, value): raise CommonException(code=codes.VALIDATION_ERROR, detail=messages.LOGIN_FIRST_SYMBOL_VALIDATION) if not re.match(constants.REGEX_FOR_LOGIN, value): raise CommonException(code=codes.VALIDATION_ERROR, detail=messages.VALUE_CONTAIN_RULE) if len(value) < constants.LOGIN_MIN_LENGTH: raise CommonException(code=codes.VALIDATION_ERROR, detail={"login": messages.LOGIN_MIN_INVALID}) elif len(value) > constants.LOGIN_MAX_LENGTH: raise CommonException(code=codes.VALIDATION_ERROR, detail={"login": messages.LOGIN_MAX_INVALID}) return value
def validate(self, attrs): """ validate the email if email already exist in database """ if User.objects.filter(email=attrs['email']).exclude( id=self.context['user'].id).count() > 0: raise CommonException(code=codes.ALREADY_EXISTS, detail=messages.EMAIL_ALREADY_EXISTS) return attrs
def validate(self, attrs): """ check if passwords are equal """ password = validate_password(attrs['password']) password_confirm = validate_password(attrs['password']) if password != password_confirm: raise CommonException(code=codes.VALIDATION_ERROR, detail=messages.PASSWORD_NOT_EQUAL) return attrs
def validate_password(value): if len(value) < constants.PASSWORD_MIN_LENGTH: raise CommonException(code=codes.VALIDATION_ERROR, detail=messages.PASSWORD_INVALID) return value
def check_push_is_not_sent(push: object) -> object: """ check if push already sent, even is true, than raise exception """ if push.is_sent is True: raise CommonException( detail=messages.RESTAURANT_PUSH_ALREADY_SENT) return push
def validate_image(image, max_image_size=constants.MAX_IMAGE_SIZE, field="image"): """ validate image size """ if image.size > 1024 * 1024 * max_image_size: raise CommonException(detail={f"{field}": messages.MAX_IMAGE_SIZE})