Exemplo n.º 1
0
def register_user():
    if not request.is_json:
        return error.error_400("Expected JSON")

    try:
        userId = request.json["userId"]
    except KeyError:
        return error.error_400("Missing key in body: 'userId'")

    try:
        roomId = request.json["roomId"]
    except KeyError:
        return error.error_400("Missing key in body: 'roomId'")

    existingRegistration = models.UserRegistration.query.filter_by(
        userid=userId, roomid=roomId).first()
    if existingRegistration is not None:
        return success.success_200({
            "registration": existingRegistration.json(),
            "newRegistration": False
        })

    userRegistration = models.UserRegistration(userid=userId, roomid=roomId)
    session.add(userRegistration)
    session.commit()

    return success.success_200({
        "registration": userRegistration.json(),
        "newRegistration": True
    })
Exemplo n.º 2
0
def get_room():
    if not request.is_json:
        return error.error_400("Expected JSON")

    try:
        roomId = request.json["roomId"]
    except KeyError:
        return error.error_400("Missing field in body: 'roomId'")

    roomQuery = models.Room.query.filter_by(roomid=roomId).first()
    if roomQuery is None:
        return error.error_400("No room found with id " + str(roomId))

    roomJson = roomQuery.json()

    assignmentsQuery = models.Assignment.query.filter_by(roomid=roomId).all()
    if len(assignmentsQuery) > 0:
        roomJson["assignments"] = list(
            map(models.Assignment.json, assignmentsQuery))
    else:
        roomJson["assignments"] = []

    registeredUsersQuery = models.UserRegistration.query.filter_by(
        roomid=roomId).all()
    if len(registeredUsersQuery) > 0:
        roomJson["registeredUsers"] = list(
            map(models.UserRegistration.json, registeredUsersQuery))
    else:
        roomJson["registeredUsers"] = []

    return success.success_200(roomJson)
Exemplo n.º 3
0
def send_nudge():
    if not request.is_json:
        return error.error_400("Expected JSON")

    try:
        assignmentId = request.json["assignmentId"]
    except KeyError:
        return error.error_400("Missing key in body: 'assignmentId'")

    assignment = models.Assignment.query.filter_by(
        assignmentid=assignmentId).first()

    if assignment.completed:
        return success.success_200({"pushStatus": "completed"})

    push.push_client.send_push_nudge(assignment.assigneduser,
                                     assignment.roomid,
                                     assignment.assignmentname)
    return success.success_200({"pushStatus": "nudged"})
Exemplo n.º 4
0
def create_assignments():
    if not request.is_json:
        return error.error_400("Expected JSON")

    try:
        roomId = request.json["roomId"]
    except KeyError:
        return error.error_400("Missing field in body: 'roomId'")

    try:
        createdUser = request.json["createdUser"]
    except KeyError:
        return error.error_400("Missing field in body: 'createdUser'")

    try:
        assignments = request.json["assignments"]
    except KeyError:
        return error.error_400("Missing field in body: 'assignments'")

    created_assignments = list()
    for assignment in assignments:

        try:
            assignedUser = assignment["assignedUser"]
        except KeyError:
            return error.error_400(
                "Missing field in assignment body: 'assignedUser'")

        try:
            assignmentName = assignment["name"]
        except KeyError:
            return error.error_400("Missing field in assignment body: 'name'")

        try:
            assignmentDate = assignment["date"]
        except KeyError:
            return error.error_400("Missing field in assignment body: 'date'")

        newAssignment = models.Assignment(roomid=roomId,
                                          createduser=createdUser,
                                          assigneduser=assignedUser,
                                          assignmentname=assignmentName,
                                          date=assignmentDate,
                                          completed=False)
        created_assignments.append(newAssignment)
        session.add(newAssignment)

    if len(created_assignments) == 0:
        return error.error_400("No assignments created")

    session.commit()
    return success.success_200({
        "createdAssignments":
        list(map(models.Assignment.get_id, created_assignments))
    })
Exemplo n.º 5
0
def create_room():
    if not request.is_json:
        return error.error_400("Expected JSON")

    try:
        newRoom = models.Room(roomname=request.json["roomName"])
        session.add(newRoom)
        session.commit()
    except KeyError:
        return error.error_400("Missing field in body: 'roomName'")

    if newRoom.roomid is None:
        return error.error_400("Error adding room to database")

    return success.success_200({"roomId": newRoom.roomid})
Exemplo n.º 6
0
def delete_assignment():
    if not request.is_json:
        return error.error_400("Expected JSON")

    try:
        assignmentId = request.json["assignmentId"]
    except KeyError:
        return error.error_400("Missing field in body: 'assignmentId'")

    assignment = models.Assignment.query.filter_by(
        assignmentid=assignmentId).first()
    if assignment is None:
        return error.error_400("No assignment found with assignmentId %s" %
                               assignmentId)

    o_session = session.object_session(assignment)
    o_session.delete(assignment)
    o_session.commit()

    return success.success_200({"deletedAssignmentId": assignmentId})