Esempio n. 1
0
def upload_image():
    """
    Upload an image to a list,
    return JSON.
    """
    list_id = request.form.get("list_id")
    base64_str = request.form.get("file")

    if not list_id:
        return error(Error.EMPTY_LIST_ID, 400)
    if not base64_str:
        return error(Error.EMPTY_IMAGE, 400)
    if not List.has_access_to(g.user.netid, list_id):
        return error(Error.NO_ACCESS_TO_LIST, 400)

    decoded = base64.decodestring(base64_str)
    filename = str(uuid.uuid4()) + ".png"
    image_path = app.config['IMAGE_PATH'] + filename
    with open(image_path, "wb") as f:
        f.write(decoded)

    card = Card.add_card(list_id, filename, True)

    image_path = app.config['IMAGE_PATH'] + filename
    thread = Thread(target=async_ocr, args=[app, image_path, card.id])
    thread.start()

    return ok({"created": True, "card": card.to_dict()})
Esempio n. 2
0
def upload_image():
    """
    Upload an image to a list.
    The image should be base64 encoded.
    """
    list_id = request.form.get("list_id")
    base64_str = request.form.get("file")

    if not list_id:
        return error(Error.EMPTY_LIST_ID, 400)
    if not base64_str:
        return error(Error.EMPTY_IMAGE, 400)

    decoded = base64.b64decode(base64_str)
    filename = str(uuid.uuid4()) + ".png"
    image_path = app.config['IMAGE_PATH'] + filename
    with open(image_path, "wb") as f:
        f.write(decoded)

    card = Card.add_card(list_id, filename, True)

    image_path = app.config['IMAGE_PATH'] + filename
    thread = Thread(target=async_ocr, args=[app, image_path, card.id])
    thread.start()

    return ok({"created": True, "card": card.to_dict()})
Esempio n. 3
0
def add_card():
    """
    Adds a card to a list.
    """
    content = request.form.get("content")
    list_id = request.form.get("list_id")

    if not content:
        return error(Error.EMPTY_CONTENT, 400)
    if not List.has_access_to(g.user.netid, list_id):
        return error(Error.NO_ACCESS_TO_BOARD, 400)

    card = Card.add_card(list_id, content)
    return ok({"created": True, "card": card.to_dict()})
Esempio n. 4
0
def add_list():
    """
    Adds a list specified by list name and board id.
    Returns the created list id.
    """
    list_name = request.form.get("name")
    board_id = request.form.get("board_id")

    if not list_name:
        return error(Error.EMPTY_LIST_NAME, 400)
    elif not board_id:
        return error(Error.EMPTY_BOARD_ID, 400)

    if not Board.has_access_to(g.user.netid, board_id):
        return error(Error.NO_ACCESS_TO_BOARD, 400)

    list_ = List.add_list(board_id, list_name)
    return ok({"created": True, "list": list_.to_dict()})
Esempio n. 5
0
def board_lists(board_id):
    """
    Returns all the lists belongs to the board specified by board id.
    """
    if not Board.has_access_to(g.user.netid, board_id):
        return error(Error.NO_ACCESS_TO_BOARD)

    lists = List.get_lists_by_board_id(board_id)
    return ok({"lists": map(lambda x: x.to_dict(), lists)})
Esempio n. 6
0
def add_card():
    """
    Adds a card specified by card content and list id.
    Returns the created card id.
    """
    content = request.form.get("content")
    list_id = request.form.get("list_id")

    if not content:
        return error(Error.EMPTY_CONTENT, 400)
    elif not list_id:
        return error(Error.EMPTY_LIST_ID, 400)

    if not List.has_access_to(g.user.netid, list_id):
        return error(Error.NO_ACCESS_TO_BOARD, 400)

    card = Card.add_card(list_id, content)
    return ok({"created": True, "card": card.to_dict()})
Esempio n. 7
0
def delete_card(card_id):
    """
    Deletes the card specified by card_id.
    """
    if not Card.has_access_to(g.user.netid, card_id):
        return error(Error.NO_ACCESS_TO_CARD)

    Card.delete_card_by_id(card_id)
    return ok({"deleted": True})
Esempio n. 8
0
def board_list(board_id):
    """
    GET: get all lists of the specified board
    POST: add a list to the specified board

    Return JSON.
    """
    if request.method == 'GET':
        lists = List.get_lists_by_board_id(board_id)
        return ok({"lists": map(lambda x: x.to_dict(), lists)})
    elif request.method == 'POST':
        name = request.form.get("name")
        if not name:
            return error(Error.EMPTY_LIST_NAME, 400)

        if not Board.has_access_to(g.user.netid, board_id):
            return error(Error.NO_ACCESS_TO_BOARD, 400)

        list_ = List.add_list(board_id, name)
        return ok({"created": True, "list": list_.to_dict()})
Esempio n. 9
0
def list_(list_id):
    """
    Returns all cards in the list specified by list id.
    """
    if not List.has_access_to(g.user.netid, list_id):
        return error(Error.NO_ACCESS_TO_LIST)

    l = List.get_list_by_id(list_id).to_dict()
    cards = List.get_cards_by_list_id(list_id)
    l['cards'] = map(lambda x: x.to_dict(), cards)
    return ok({"list": l})
Esempio n. 10
0
def signup():
    netid = request.form.get('netid', '')
    password = request.form.get('password', '')
    name = request.form.get('name', '')

    if not netid:
        return error(Error.EMPTY_NETID, 400)
    if not password:
        return error(Error.EMPTY_PASSWORD, 400)
    if not name:
        return error(Error.EMPTY_NAME, 400)

    if User.get_user(netid):
        return error(Error.NETID_EXISTED, 400)

    user = User.add_user(netid, password, name)
    ret = user.to_dict()
    ret.pop("password")

    return ok({"user": ret})
Esempio n. 11
0
def card_ocr_text(card_id):
    """
    Returns the OCR text of a card if it is an image card.
    """
    ocr = OCR.get_ocr_by_card_id(card_id)
    if not ocr:
        return error(Error.NO_OCR_TEXT)
    return ok({
        "ocr": {
            "card_id": ocr.card_id,
            "text": ocr.text
        }
    })
Esempio n. 12
0
def signin2():
    """
    Used as REST API.
    """
    netid = request.form['netid'].strip()
    password = request.form['password'].strip()
    remember_me = 'remember_me' in request.form

    registered_user = User.get_user(netid)

    if not registered_user:
        return error(Error.NETID_NOT_EXIST)

    if not registered_user.check_password(password):
        return error(Error.INVALID_PASSWORD)

    login_user(registered_user, remember_me)

    ret = registered_user.to_dict()
    ret.pop("password")

    return ok(ret)
Esempio n. 13
0
def list_(list_id):
    """
    Returns or deletes the list specified by list id.
    """
    if not List.has_access_to(g.user.netid, list_id):
        return error(Error.NO_ACCESS_TO_LIST)

    if request.method == 'GET':
        l = List.get_list_by_id(list_id).to_dict()
        cards = List.get_cards_by_list_id(list_id)
        l['cards'] = map(lambda x: x.to_dict(), cards)
        return ok({"list": l})

    if request.method == 'DELETE':
        List.delete_list_by_id(list_id)
        return ok({"deleted": True})
Esempio n. 14
0
def all_boards():
    """
    Returns all the boards the user has access to
    or adds a board.
    """
    if request.method == 'GET':
        board_ids = Board.get_board_ids_by_netid(g.user.netid)
        boards = map(lambda id_: Board.get_board_by_id(id_).to_dict(), board_ids)
        return ok({'boards': boards})

    elif request.method == 'POST':
        # add a board
        board_name = request.form.get("name")

        if not board_name:
            return error(Error.EMPTY_BOARD_ID, 400)

        b = Board.add_board(board_name)
        Access.add_access(b.id, g.user.netid)
        return ok({"created": True, "board": b.to_dict()})