def put(self): # /admin/rechnungslauf data = request.get_json() settings = SystemSetting.get_settings() connection = pika.BlockingConnection( pika.ConnectionParameters(host="localhost")) channel = connection.channel() channel.queue_declare(queue="email") for each in data: settings = SystemSetting.get_settings() logging.warning(f"Using: {each}") logging.warning( f"username: {each['username']}, coffee_count: {each['coffee_count']}" ) obj = { "username": "", "email": "", "coffee_count": "", "betrag": "" } logging.warning("Rechne new_coffee_count aus") new_coffee_count = RechnungsApi.count(each['coffee_count']) if new_coffee_count is None: new_coffee_count = int(0) logging.warning(f"New coffee count: {new_coffee_count}") logging.warning("Getting user") user = User.find_by_username(each['username']) logging.warning(f"Found user: {user.username}") logging.warning(f"User coffee count: {user.coffee_count}") temp_var = user.coffee_count - new_coffee_count logging.warning(f"Temporary coffee_count: {temp_var}") user.coffee_count = temp_var user.save() obj["username"] = user.username obj["email"] = user.email obj["coffee_count"] = new_coffee_count obj["betrag"] = new_coffee_count * settings.kaffee_preis logging.warning(f"obj: {obj}") user = User.find_by_username(each['username']) logging.warning(f"Neuer User coffee counter: {user.coffee_count}") channel.basic_publish(exchange="", routing_key="email", body=json.dumps(obj)) connection.close() return { "message": "Die Daten wurden gespeichert und Emails werden versendet." }, 201
def post(self): response = {} user = User.find_by_id(get_jwt_identity()) if not user.admin: response['status'] = "ERROR" response['msg'] = "No admin rights" return response, 403 else: schema = UserSchema() result = schema.load(request.json) if not result.errors: if User.find_by_username(username=result.data['username']): response["status"] = "ERROR" response["msg"] = "Username existiert bereits" return response, 400 else: data = request.get_json() user = User(**data) user.save() response['status'] = "OK" response['msg'] = "User wurde angelegt" return response, 201 else: response['status'] = "ERROR" response['msg'] = result.errors return response, 300
def post(self): user = User.find_by_username(data.get('username')) if user and user.check_password(data.get('password'), user._password): logger.info("Creating access_token") access_token = create_access_token(identity=str(user.id), fresh=True) return { "access_token": access_token, "username": user.username }, 200 return {"msg": "Invalid credentials"}, 401
def post(self): response = {} data = request.get_json() if User.find_by_username(data['username']): response['status'] = "ERROR" response['message'] = "Username ist bereits vergeben." return response, 500 user = User(**data) user.save() response['status'] = "OK" response['message'] = "User wurde erfolgreich angelegt." return response, 201
def test_create_user(): schema = UserSchema() result = schema.load(user) if result.errors == '{}': user = User(username, password, email) user.save() saved_user = User.find_by_username(username=user['username']) assert user['username'] == saved_user.username assert user['email'] == saved_user.email
def post(self): response = {} data = data.get_json() user = User.find_by_username(username=data['username']) if user and user.check_pw(user._password, data['password']): response['token'] = create_access_token(identity=str(user.id), fresh=True) response['status'] = "OK" response['msg'] = "Du wurdest erfolgreich eingeloggt." return response, 200 else: response['status'] = "ERROR" response['msg'] = "Username und/oder Passwort falsch." return response, 401
def post(self): data = request.get_json() logger.info(data) if User.find_by_username(data.get('username')): logger.info("User not found.") return {"msg": "Dieser Username ist bereits vergeben."}, 500 user = User(**data) try: logger.info("Saving user data {}".format(data)) user.save() return {"msg": "User wurde erfolgreich angelegt."} except: logger.info("ERROR while saving user data.") return { "msg": "Der User konnte nicht angelegt werden. Ein Fehler ist auftreten." }
def post(self): response = {} data = request.get_json() user = User.find_by_username(data['username']) if user and user.check_pw(password=data['password'], hashed_pw=user._password): token = create_access_token( identity=str(user.id), fresh=True, expires_delta=datetime.timedelta(minutes=60)) response['token'] = token response['username'] = user.username response['status'] = "OK" response['message'] = "User wurde angemeldet" return response, 200 else: response['status'] = "ERROR" response['message'] = "Username und/oder Passwort sind falsch" return response, 403
def post(self): response = {} admin = User.find_by_id(get_jwt_identity()) if not admin.is_admin: response['status'] = "ERROR" response[ 'message'] = "Sie haben nicht die nötigen Rechte. Bitte wenden sie sich an den Admin." return response, 403 data = request.get_json() if User.find_by_username(data['username']): response['status'] = "ERROR" response['message'] = "Username vergeben" return response, 500 user = User(**data) user.save() response['status'] = "OK" response['message'] = "User wurde angelegt" return response, 201
def test_User_find_by_username(): user = User.find_by_username(username=user['username']) assert user['username'] == saved_user.username assert user['email'] == saved_user.email