Beispiel #1
0
def deck_overview(did):
    username = decrypt_token(request.headers['auth'])['username']
    col = get_collection(username)
    decks = col.decks
    if did:
        # 切换deck
        col.decks.select(did)
    # 查看现在选中的deck (返回的是dict)
    # col.decks.current() 效果和 col.decks.decks[did] 是一样的
    deck = decks.current()
    # 计划
    sched = col.sched
    # 统计数字
    sched.reset()
    # deck首页的几个数字
    newCount, lrnCount, revCount = sched.counts()
    print(newCount, lrnCount, revCount)
    finished = not sum([newCount, lrnCount, revCount])
    col.close()
    return jsonify({
        'code': 20000,
        "deck": deck,
        "newCount": newCount,
        "lrnCount": lrnCount,
        "revCount": revCount,
        "finished": finished,
    })
Beispiel #2
0
def decks_list():
    user_info = decrypt_token(request.headers.get('auth', None))
    username = user_info['username']
    col = get_collection(username)
    decks = col.decks
    deck_list = []
    dueTree = col.sched.deckDueTree()
    for node in dueTree:
        name, did, due, lrn, new, children = node
        if did == 1:
            continue
        sub_deck = []
        for sub_node in children:
            sub_name, sub_did, sub_due, sub_lrn, sub_new, sub_children = sub_node
            sub_deck.append({'name': sub_name, 'did': sub_did, 'new': sub_new})
        deck_list.append({
            'name': name,
            'did': did,
            'new': new,
            'sub_deck': sub_deck
        })

    card_count, learn_time = col.db.first(
        """
    select count(), sum(time)/1000 from revlog
    where id > ?""", (col.sched.dayCutoff - 86400) * 1000)
    col.close()
    return jsonify({
        'code': 20000,
        "deck_list": deck_list,
        "card_count": card_count,
        "learn_time": learn_time
    })
Beispiel #3
0
def decks_list():
    col = get_collection(session['current_user']['username'])
    decks = col.decks
    deck_list = []
    dueTree = col.sched.deckDueTree()
    for node in dueTree:
        name, did, due, lrn, new, children = node
        if did == 1:
            continue
        sub_deck = []
        for sub_node in children:
            sub_name, sub_did, sub_due, sub_lrn, sub_new, sub_children = sub_node
            sub_deck.append({'name': sub_name, 'did': sub_did, 'new': sub_new})
        deck_list.append({
            'name': name,
            'did': did,
            'new': new,
            'sub_deck': sub_deck
        })

    cards, thetime = col.db.first(
        """
    select count(), sum(time)/1000 from revlog
    where id > ?""", (col.sched.dayCutoff - 86400) * 1000)
    col.close()
    html = render_template('decks/decks_list.htm', **locals())
    return html
Beispiel #4
0
def card_study():
    col = get_collection(session['current_user']['username'])
    col.reset()
    card = col.sched.getCard()
    if card is None:
        return redirect(url_for('decks.deck_overview'))
    question = card.q()
    answer = card.a()
    # 把声音文件名拿出来
    question_sound_list = all_sounds(question)
    answer_sound_list = all_sounds(answer)
    # 前端不显示声音信息
    question = re.sub(r"\[sound:[^]]+\]", "", question)
    answer = re.sub(r"\[sound:[^]]+\]", "", answer)
    print(question_sound_list)
    print(answer_sound_list)

    # 处理图片
    # url_for('cards.card_media',filename=img)
    reMedia = re.compile("(?i)(<img[^>]+src=[\"']?)([^\"'>]+[\"']?[^>]*>)")
    media_url_prefix = url_for('cards.card_media', filename="")
    question = reMedia.sub(" \\1" + media_url_prefix + "\\2", question)
    answer = reMedia.sub(" \\1" + media_url_prefix + "\\2", answer)

    cnt = col.sched.answerButtons(card)
    if cnt == 2:
        btn_list = [(1, 'Again'), (2, 'Good')]
    elif cnt == 3:
        btn_list = [(1, 'Again'), (2, 'Good'), (3, 'Easy')]
    else:
        btn_list = [(1, 'Again'), (2, 'Hard'), (3, 'Good'), (4, 'Easy')]

    col.close()
    html = render_template('cards/card_study.htm', **locals())
    return html
Beispiel #5
0
def card_answer():
    answer = request.json['answer']
    col = get_collection(decrypt_token(request.headers['auth'])['username'])
    col.reset()
    card = col.sched.getCard()
    sched = col.sched
    sched.answerCard(card, int(answer))
    col.close()
    return jsonify({"code": 20000})
Beispiel #6
0
def card_answer():
    answer = request.values.get('answer')
    col = get_collection(session['current_user']['username'])
    col.reset()
    card = col.sched.getCard()
    sched = col.sched
    sched.answerCard(card, int(answer))
    col.close()
    return jsonify({"result": True, "next": url_for('cards.card_study')})
Beispiel #7
0
def card_study():
    username = decrypt_token(request.headers['auth'])['username']
    col = get_collection(username)
    col.reset()
    card = col.sched.getCard()
    if card is None:
        return redirect(url_for('api_deck.deck_overview'))
    question = card.question()
    answer = card.answer()

    # 媒体文件域名在设置里配置
    # 媒体文件uri前缀
    media_url_prefix = url_for('api_card.card_media', username=username, filename="")
    # 把声音文件名拿出来
    # question_sound_list = all_sounds(question)
    # answer_sound_list = all_sounds(answer)
    # 接口变了
    question_sound_list = [media_domain + media_url_prefix + x.filename for x in card.question_av_tags()]
    answer_sound_list = [media_domain + media_url_prefix + x.filename for x in card.answer_av_tags()]
    print('question_sound_list', question_sound_list)
    print('answer_sound_list', answer_sound_list)
    # 前端不显示声音信息
    # question = re.sub(r"\[sound:[^]]+\]", "", question)
    # answer = re.sub(r"\[sound:[^]]+\]", "", answer)
    question = re.sub(r"\[anki:play[^]]+\]", "", question)
    answer = re.sub(r"\[anki:play[^]]+\]", "", answer)

    # 处理图片
    # url_for('api_card.card_media',filename=img)
    reMedia = re.compile("(?i)(<img[^>]+src=[\"']?)([^\"'>]+[\"']?[^>]*>)")
    question = reMedia.sub(" \\1" + media_domain + media_url_prefix + "\\2", question)
    answer = reMedia.sub(" \\1" + media_domain + media_url_prefix + "\\2", answer)
    # print('============ question ============')
    # print(question)
    # print('============ answer ============')
    # print(answer)
    # print('==================================')
    cnt = col.sched.answerButtons(card)
    if cnt == 2:
        btn_list = [(1, 'Again'), (2, 'Good')]
    elif cnt == 3:
        btn_list = [(1, 'Again'), (2, 'Good'), (3, 'Easy')]
    else:
        btn_list = [(1, 'Again'), (2, 'Hard'), (3, 'Good'), (4, 'Easy')]

    col.close()
    return jsonify({'code': 20000,
                    'question': question,
                    'answer': answer,
                    'question_sound_list': question_sound_list,
                    'answer_sound_list': answer_sound_list,
                    'btn_list': btn_list,
                    })
Beispiel #8
0
def deck_overview(did):
    col = get_collection(session['current_user']['username'])
    decks = col.decks
    if did:
        # 切换deck
        col.decks.select(did)
    # 查看现在选中的deck (返回的是dict)
    # col.decks.current() 效果和 col.decks.decks[did] 是一样的
    deck = decks.current()
    # 计划
    sched = col.sched
    # 统计数字
    sched.reset()
    # deck首页的几个数字
    newCount, lrnCount, revCount = sched.counts()
    # print(newCount, lrnCount, revCount)
    finished = not sum([newCount, lrnCount, revCount])
    col.close()
    html = render_template('decks/deck_overview.htm', **locals())
    return html