def login(): try: data = request.get_json() fetched = User.query.filter_by(login=data["login"]).first() if not fetched: e = notFound.format("User") return response_with(resp.INVALID_INPUT_422, error=e) valid_password = User.validate_password(data["password"], fetched.password) if not valid_password: e = invalid.format("Password") return response_with(resp.INVALID_INPUT_422, error=e) user_schema = UserSchema() user, error = user_schema.dump(fetched) token = generate_jwt(user) return response_with(resp.SUCCESS_200, value={ "user": user, "token": token }) except Exception as e: logging.error(e) return response_with(resp.SERVER_ERROR_500)
def update_user(uid): try: data = request.get_json() name = data.get("firstName") if not name: e = required.format("Name") return response_with(resp.MISSING_PARAMETERS_422, error=e) surname = data.get("lastName") if not surname: e = required.format("Surname") return response_with(resp.MISSING_PARAMETERS_422, error=e) email = data.get("email") if not email: e = required.format("Email") return response_with(resp.MISSING_PARAMETERS_422, error=e) password = data.get("password") # validate user exists user = User.query.filter_by(id=uid).first() if not user: e = notFound.format("User") return response_with(resp.NOT_FOUND_HANDLER_404, error=e) # validate access to user access = User.query.filter_by(id=JWT.details["user_id"]).first() if not access: e = permission return response_with(resp.NOT_FOUND_HANDLER_404, error=e) # update user user.update(name, surname, email, password=password) # response details return _get_user(uid) except IntegrityError: e = exists.format("Name") return response_with(resp.INVALID_INPUT_422, error=e) except Exception as e: logging.error(e) return response_with(resp.SERVER_ERROR_500)
def _get_user(uid): user = User.query.filter_by(id=uid).first() if not user: error = notFound.format("User") return response_with(resp.NOT_FOUND_HANDLER_404, error=error) user_schema = UserSchema() user_data, error = user_schema.dump(user) if error: return response_with(resp.SERVER_ERROR_500, error=error) val = { "id": user_data["id"], "firstName": user_data["name"], "lastName": user_data["surname"], "email": user_data["email"], "login": user_data["login"], "created": user_data["created"], "updated": user_data["updated"], } return response_with(resp.SUCCESS_200, value={"user": val})
def update_access_point(aid): try: data = request.get_json() name = data.get("name") if not name: e = required.format("Name") return response_with(resp.MISSING_PARAMETERS_422, error=e) # validate access point exists ap = AccessPoint.query.filter_by(id=aid).first() if not ap: e = notFound.format("Access Point") return response_with(resp.NOT_FOUND_HANDLER_404, error=e) # update access point ap.update(name) # response details return response_with(resp.SUCCESS_200) except Exception as e: logging.error(e) return response_with(resp.SERVER_ERROR_500)
def get_access_point_details(aid): try: ap = AccessPoint.query.filter_by(id=aid).first() if not ap: error = notFound.format("Access Point") return response_with(resp.NOT_FOUND_HANDLER_404, error=error) access_point_schema = AccessPointSchema() ap_data, error = access_point_schema.dump(ap) if error: return response_with(resp.SERVER_ERROR_500, error=error) val = { "id": ap_data["id"], "name": ap_data["name"], "created": ap_data["created"], "updated": ap_data["updated"], } return response_with(resp.SUCCESS_200, value={"accessPoint": val}) except Exception as e: logging.error(e) return response_with(resp.SERVER_ERROR_500)