def projects_read():
    """GET projects/
    Desc: Gets all available projects associated with the current user
    Returns:
        200: all projects in the database
    """
    for project in Projects.objects(creator_id=get_jwt_identity()["_id"]["$oid"]):
        project_hardware = list()
        for hardware in project.hardware:
            price = HardwareSet.objects(id=hardware["_id"]).first()["price"]
            old_datetime = hardware["checkout_time"]
            # price * time diff in hours
            hardware["cost"] = price * \
                ((datetime.now() - old_datetime).seconds / 3600)
            project_hardware.append(hardware)
        project.update(hardware=project_hardware)
        project.reload()
    return Projects.objects(creator_id=get_jwt_identity()["_id"]["$oid"]).to_json(), 200
def test_project_read_id_not_found(client, auth_token):
    user = User.objects.first()
    project_data = [{
        "name": "New Project 1",
        "description": "Project description here",
        "creator_id": user.pk,
        "project_id": "unique-id"
    }, {
        "name": "New Project 2",
        "description": "Project description here",
        "creator_id": user.pk,
        "project_id": "unique-id-2"
    }]
    for project in project_data:
        Projects(**project).save()
    project = Projects.objects.first()
    Projects.objects(id=project.pk).delete()
    res = client.get(f'/projects/{project.project_id}',
                     headers={"Authorization": auth_token})
    assert res.status_code == 404
    assert b"not found" in res.data
def projects_delete(id):
    """DELETE projects/<id>
    Desc: Deletes a specific project from the database
    Returns:
        200: success message
        404: project not found
        422: validation errors
    """
    try:
        if Projects.objects(id=id).delete() > 0:
            return {'msg': 'Projects set successfully deleted'}, 200
        else:
            return {'msg': 'Project not found'}, 404
    except ValidationError as e:
        return parse_error(e), 422
def projects_read_id(id):
    """GET projects/<id>
    Desc: Gets a project by project id
    Returns:
        200: project found in the database and returned
        404: project not found
        422: validation errors
    """
    try:
        curr_project = Projects.objects(project_id=id).first()
        if curr_project:
            return curr_project.to_json(), 200
        else:
            return {'msg': 'Project not found'}, 404
    except ValidationError as e:
        return parse_error(e), 422
def projects_update(id):
    """PUT projects/<id>
    Desc: Updates a specific projects
    Returns:
        200: updated projects object
        404: project not found
        422: validation errors
    """
    req = request.get_json()
    try:
        curr_project = Projects.objects(id=id).first()
        if curr_project:
            curr_project.update(**req)
            curr_project.reload()
            return curr_project.to_json(), 200
        else:
            return {'msg': 'Project not found'}, 404
    except ValidationError as e:
        return parse_error(e), 422