Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def log():
    try:
        data = request.get_json()
        print(data)

        return response_with(resp.SUCCESS_200, value={"status": "logged"})
    except Exception as e:
        logging.error(e)
        return response_with(resp.INVALID_INPUT_422)
Exemplo n.º 3
0
def create_user():
    try:
        data = request.get_json()
        user_schema = UserSchema()
        user, error = user_schema.load(data)
        result = user_schema.dump(user.create()).data
        return response_with(resp.SUCCESS_200, value={"user": result})
    except Exception as e:
        logging.error(e)
        return response_with(resp.INVALID_INPUT_422)
Exemplo n.º 4
0
def create_access_point():
    try:
        data = request.get_json()
        access_point_schema = AccessPointSchema()
        ap, error = access_point_schema.load(data)
        result = access_point_schema.dump(ap.create()).data
        return response_with(resp.SUCCESS_200, value={"accessPoint": result})
    except Exception as e:
        logging.error(e)
        return response_with(resp.INVALID_INPUT_422)
Exemplo n.º 5
0
def validate():
    try:
        uid = JWT.details["user_id"]
        return _get_user(uid)
    except Exception as e:
        logging.error(e)
        return response_with(resp.SERVER_ERROR_500)
Exemplo n.º 6
0
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})
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
 def server_error(e):
     logging.error(e)
     return response_with(resp.SERVER_ERROR_500)
Exemplo n.º 11
0
def healthcheck():
    try:
        return response_with(resp.SUCCESS_200, value={"message": "i'm alive"})
    except Exception as e:
        logging.error(e)
        return response_with(resp.SERVER_ERROR_500)
Exemplo n.º 12
0
 def not_found(e):
     logging.error(e)
     return response_with(resp.NOT_FOUND_HANDLER_404)
Exemplo n.º 13
0
def get_user_details(uid):
    try:
        return _get_user(uid)
    except Exception as e:
        logging.error(e)
        return response_with(resp.SERVER_ERROR_500)
Exemplo n.º 14
0
 def auth_required(e):
     logging.error(e)
     return response_with(resp.UNAUTHORIZED_403, error=e.error)
Exemplo n.º 15
0
 def bad_request(e):
     logging.error(e)
     return response_with(resp.BAD_REQUEST_400)
Exemplo n.º 16
0
 def decode_error(e):
     logging.error(e)
     return response_with(resp.UNAUTHORIZED_403, error=e.error)
Exemplo n.º 17
0
 def base_jwt_error(e):
     logging.error(e)
     return response_with(resp.UNAUTHORIZED_403, error=e.error)
Exemplo n.º 18
0
 def expired_error(e):
     logging.error(e)
     return response_with(resp.UNAUTHORIZED_403, error=e.error)