示例#1
0
def add_cardbox():
    if not request.is_json:
        abort(404)

    # already returns dictionary
    payload = request.get_json()

    req = ('username', 'password', 'tags', 'content', 'name')
    if not payload or not all(r in payload for r in req):
        abort(404)

    if User.exists(db, payload['username']):
        user = User.fetch(db, payload['username'])
        if not user.check_password(payload['password']):
            abort(404)

        new_box = CardBox(CardBox.gen_card_id(),
                          name=payload['name'],
                          owner=user._id,
                          rating=0,
                          tags=payload['tags'],
                          content=payload['content'])
        new_box.store(db)

        user.cardboxs.append(new_box._id)

        user.store(db)

    return 'OK'
示例#2
0
def add_cardbox():
    if not request.is_json:
        print('not json')
        abort(404)

    # already returns dictionary
    payload = request.get_json()

    # <-- validate payload -->
    req = ('username', 'password', 'tags', 'content', 'name', 'info')
    if not payload or not all(r in payload for r in req):
        print('missing key in payload')
        abort(404)

    # None check
    if any(payload[key] is None for key in req):
        print('key is None')
        abort(404)

    # type check
    if not all([
            isinstance(payload['tags'], list),
            isinstance(payload['name'], str),
            isinstance(payload['info'], str)
    ]):
        print('key wrong type')
        abort(404)

    if any(' ' in tag for tag in payload['tags']):
        print('whitespace in tag')
        abort(404)

    # <-- validate content -->
    if not isinstance(payload['content'], list):
        print('content not list')
        abort(404)

    attrs = ('question', 'answers', 'correct_answer', 'explanation')
    if not all(a in _dict for a in attrs for _dict in payload['content']):
        print('missing key in card')
        abort(404)

    number_of_answers = 3

    for card in payload['content']:

        q, a, ca, e = (card['question'], card['answers'],
                       card['correct_answer'], card['explanation'])

        if not isinstance(q, str) or not isinstance(e, str):
            abort(404)

        if not (isinstance(a, list) and len(a) == number_of_answers):
            abort(404)

        if not (isinstance(ca, int) and ca in range(number_of_answers)):
            abort(404)

    # check authorization
    if User.exists(db, payload['username']):
        user = User.fetch(db, payload['username'])
        if not user.check_password(payload['password']):
            print('unauthorized')
            abort(404)

        cardbox_id = CardBox.gen_card_id()

        # 'Update'-Function
        boxes = CardBox.fetch_multiple(db, user.cardboxs)

        for box in boxes:
            if box.name == payload['name']:
                cardbox_id = box._id
                break
        else:
            user.cardboxs.append(cardbox_id)

        # store content in separate redis table
        Card.save_content(db, cardbox_id, payload['content'])

        # create CardBox object for metadata
        new_box = CardBox(cardbox_id,
                          name=payload['name'],
                          owner=user._id,
                          rating=0,
                          info=payload['info'],
                          tags=payload['tags'])

        new_box.store(db)
        user.store(db)
        User.update_score(db, user._id)

    return 'OK'