Beispiel #1
0
def login():
    """
    Log in the user
    """
    errors = None
    try:
        request_data = json.loads(request.data)
        validate_dict_with_schema(request_data, "user/register/request")
        user = User.check_user(
            email=request_data["email"],
            password=request_data["password"]
        )
        if user:
            session["user_id"] = user.id
            return json_response(
                status=200,
                response_data={
                    "success": True, "data": {"user": user.serialize()}
                }
            )
    except (TypeError, ValueError):
        errors = ["Invalid JSON"]
    except ValidationError as e:
        errors = e.message

    if errors:
        return json_response(
            status=400, response_data={"success": False, "errors": errors}
        )

    return json_response(
        status=401,
        response_data={"success": False, "errors": ["Invalid email/password"]}
    )
Beispiel #2
0
def register():
    """
    Register a new user
    """
    try:
        request_data = json.loads(request.data)
        validate_dict_with_schema(request_data, "user/register/request")
        user = User(
            email=request_data["email"],
            password=User.create_hash(plain_password=request_data["password"])
        )
        user.save()
        return json_response(
            status=200,
            response_data={"success": True,  "data": {"user": user.serialize()}}
        )
    except (TypeError, ValueError):
        errors = ["Invalid JSON"]
    except ValidationError as e:
        errors = e.message
    except IntegrityError:
        errors = ["This email address is already registered"]

    if errors:
        return json_response(
            status=400, response_data={"success": False, "errors": errors}
        )
Beispiel #3
0
def me(user):
    """
    Get details of the current user
    """
    return json_response(
        status=200,
        response_data={"success": True, "data": {"user": user.serialize()}}
    )
def test_empty_response():
    """
    When I call`json_response` function without arguments
    Then I get HTTP 200 OK response
    And the response body is empty
    """
    response = json_response()
    assert response.status_code == 200
    assert response.data.decode("utf-8") == u""
Beispiel #5
0
def list_users():
    """
    List all users of the system
    """
    return json_response(
        status=200,
        response_data={
            "success": True,
            "data": {
                "users": [user.serialize() for user in User.all()]
            }
        }
    )
 def handle_405(e):
     return json_response(405,
                          response_data={"error": "Method Not Allowed"})
 def handle_404(e):
     return json_response(404, response_data={"error": "Not Found"})
Beispiel #8
0
def logout():
    """
    Log out the user
    """
    session.clear()
    return json_response(status=200, response_data={"success": True})