def post(self): """ Called when we have a POST request Returns: BaseResponse object with message and code """ data = request.get_json() is_ok, result = helper.validation(schemas.transfer_schema, data) if not is_ok: return result users_exist = usr_ex1 and usr_ex2 user_1_data = data["user1"] user_2_data = data["user2"] amount = data["amount"] username_1 = user_1_data["username"] username_2 = user_2_data["username"] try: helper.balance_validation(username_1, username_2) except ValueError as ex: return jsonify({"message": ex.args[0], "code": ex.args[1]}) helper.update_balance(config.users, user_1_data["username"], amount, balance_acc1, operator.sub) helper.update_balance(config.users, user_2_data["username"], amount, balance_acc2, operator.add) helper.update_tokens(config.users, user_1_data["username"], 1, operator.sub) return jsonify({ "Message": "Transaction completed successfully.", "Code": config.OK })
def post(self) -> BaseResponse: """ Called when we have a POST request. Returns: BaseResponse object with message and code """ admin_pwd_crypted = bcrypt.hashpw(config.admin_pwd.encode("utf8"), bcrypt.gensalt()) config.users.insert({ "Username": config.admin_name, "Password": admin_pwd_crypted }) helper.set_server_data(request.get_json()) validation, result = helper.validation(schemas.refill_schema, token_validation=False) if not validation: return jsonify(result) username, _, tokens_add = result helper.update_tokens(username, tokens_add, operator.add) return jsonify({ "Message": "Tokens updated successfully.", "Code": config.OK })
def post(self) -> BaseResponse: """ Called when we have a POST requiest. Returns: BaseResponse instance with message and status for API """ data = request.get_json() helper.set_server_data(data) is_valid, result = helper.validation(schemas.login_schema) if not is_valid: return jsonify(result) else: username, _, sentence = result # updating Tokens and Sentance helper.update_tokens(1, operator.sub) # update sentance in database config.users.update( {"Username": username}, {"$set": {"Sentance": sentence}} ) return jsonify( {"status": config.OK, "msg": "You succesfully login.", "sentance": sentence} )
def post(self) -> BaseResponse: """ Called when we have a POST request. Returns: BaseResponse object with message and code """ # add admin's password and username to database config.users.insert({ "Username": config.admin_name, "Password": config.admin_pwd_crypted }) data = request.get_json() is_valid, msg, code = helper.user_validation(config.users, data, config.refill_keys_valid, is_register=False) if not is_valid: return jsonify({"Message": msg, "Code": code}) username = data["username"] amount_tokens = data["amount"] # refill tokens helper.update_tokens(config.users, username, add, amount_tokens) return jsonify({ "Message": "Tokens updated successfully.", "Code": helper.OK })
def post(self) -> BaseResponse: """ Called when we have a POST requiest. Returns: BaseResponse instance with message and status for API """ # try to get data from user data = request.get_json() is_valid, result = helper.validation(config.users, schemas.usr_change_schema, data, config.username_change_keys) if not is_valid: return jsonify(result) username, _, new_username = result # removing one token helper.update_tokens(config.users, username, 1, operator.sub) config.users.update({"Username": username}, {"$set": { "Username": new_username }}) return jsonify({ "Message": "Username changed successfully.", "Code": config.OK })
def post(self): """ Called when we have a POST request. Returns: BaseResponse object with message and code """ # taking data from server data = request.get_json() validation, result = helper.validation(config.users, schemas.update_balance_schema, data, config.update_balance_keys) if not validation: return jsonify(result) username, _, code, amount = result # add deposit to current money money_curr = config.users.find({"Username": username})[0]["Balance"] if amount <= 0: return jsonify({ "Message": "Amount must be greather than zero.", "Code": config.INVALID_AMOUNT }) # if code that is sent from user is not # 'D' for deposit or 'W' for withdraw # than we want to sent error to user # otherwise, we want to preform operation (deposit or withdraw) if code == "D": helper.update_balance(config.users, username, amount, money_curr, operator.add) elif code == "W": if amount > money_curr: return jsonify({ "Message": "You don't have enough money", "Code": config.NOT_ENOUGH_MONEY }) helper.update_balance(config.users, username, amount, money_curr, operator.sub) else: return jsonify({ "Message": "For withdraw enter 'W', for deposit enter 'D'", "Code": config.INVALID_CODE }) # remove one token helper.update_tokens(config.users, username, 1, operator.sub) return jsonify({ "Message": "You successfully updated your balance.", "Code": config.OK })
def post(self) -> BaseResponse: """ Called when we have a POST requiest. Returns: BaseResponse instance with message and status for API """ # try to get data from user data = request.get_json() helper.set_server_data(data) is_valid, result = helper.validation(schemas.pass_change_schema) if not is_valid: return jsonify(result) username, _, new_pwd = result new_pwd_hashed = bcrypt.hashpw(new_pwd.encode("utf-8"), bcrypt.gensalt()) if helper.new_old_passwords_equal(): return jsonify({ "Message": "Old password cannot be new password", "Code": config.INVALID_PASSWORD }) # removing one token helper.update_tokens(1, operator.sub) # changing password config.users.update({"Username": username}, {"$set": { "Password": new_pwd_hashed }}) return jsonify({ "status": config.OK, "msg": "Password changed succesfully." })
def post(self): """ Called when we have a POST request. Returns: BaseResponse object with message and code """ data = request.get_json() # if we do, than we can take values that is sent is_valid, msg, code = helper.user_validation( config.users, data, config.weather_keys_valid, is_register=False ) if not is_valid: return jsonify({"Message": msg, "Code": code}) city = data["city"] username = data["username"] format_temp = str(data["format_temperature"]).upper() if helper.count_tokens(config.users, username) <= 0: return jsonify( { "Message": "You are out of tokens, please refill.", "Code": helper.OUT_OF_TOKENS } ) # get data from openweather web app r = requests.get(f"http://api.openweathermap.org/data/2.5/" f"weather?q={city}" f"&appid=5b18652fc700a44670ccb177ade90c69") data_return = dict(r.json()) # validate that city and counrry exist try: all_values = helper.validate_city_and_country( data_return, helper.DATA_NOT_EXIST, config.main_keys_valid ) except KeyError as ex: return jsonify({"Message": ex.args[0], "Code": ex.args[1]}) weather_return = dict(zip(config.keys_weather_return, all_values)) temp_to_convert = [weather_return[key] for key in list(weather_return.keys())[:4]] try: temp_to_convert = helper.validate_temp( format_temp, temp_to_convert, weather_return ) except ValueError: return jsonify( { "Message": ("For temperature in Celsius send 'C', " "for temperature in Fahrenheit send 'F'."), "Code": helper.KEYS_NOT_VALID } ) # don't forget to take one token from user helper.update_tokens(config.users, username, sub, 1) # finaly, return valid respond with OK status return jsonify( { "Message": { f"This is weather for {city}": weather_return }, "Code": helper.OK } )