def inner_data_validation(usr_exist: bool, token_validation: bool, schema: dict) -> None: """ Validate inner data from dictionary. Arguments: usr_exist {bool} -- True if user exist in database, False otherwise token_validation {bool} -- True if user wants to validate tokens, False otherwise schema {dict} -- schema for validation Raises: UserException: if user does not exist in database if password is not valid if user that sends money does not have tokens """ if not usr_exist: raise exceptions.UserException(error_usr_exist, config.INVALID_USERNAME) if is_value_dict(): validate_password_dict() else: password_existance() raise_exception_if_password_invalid() # if admin wants to validate that there is enough # tokens for current user if token_validation: validate_tokens(config.OUT_OF_TOKENS)
def validate_username(code: int) -> None: """ Validate that username exist in database. Arguments: code {int} -- code for error Raises: UserException: If username does not exist in database """ if not username_exist(): raise exceptions.UserException("This username does not exist.", code)
def profile(self): """ Return profile informations about this user """ r = self.request("GET", "oauth2/v1/userinfo", params={'alt': 'json'}) profile = r.json() if (profile is None or not "given_name" in profile or not "email" in profile or not "name" in profile): raise exceptions.UserException("Invalid user profile") return profile
def location(self, lid="latest"): """ Return the last known location or a specific location :param lid: location id ("latest" for the last known location) """ r = self.request("GET", "mirror/v1/locations/%s" % (lid)) location = r.json() if (location is None or not "latitude" in location or not "longitude" in location): raise exceptions.UserException("Invalid user location") return location
def update_balance(amount: float, money_curr: float, operation: operator) -> None: """ Update balance for account. Arguments: username {str} -- username amount {float} -- amount for balance update operation {operator} -- add or subtract Raises: UserException: if operation is not subtraction or addition """ if not (operation is operator.sub or operation is operator.add): raise exceptions.UserException( "You can only add or subtract balance from account", config.BAD_REQUEST) config.users.update({"Username": get_username()}, {"$set": { "Balance": operation(money_curr, amount) }})