示例#1
0
 def test_kana(self):
     PronunciationOfKanamoji.insert_pronunciations(self.db)
     Kana.insert_kanas(self.db)
     with self.db.session as session:
         kana = Kana.query(session).filter_by(romaji='a').first()
         self.assertTrue(kana.hiragana == 'あ')
         character = PronunciationOfKanamoji.query(session) \
             .filter_by(id=kana.pronunciation_id) \
             .first().character
         self.assertTrue(character == 'Seion')
示例#2
0
文件: kana.py 项目: AuTa/JPL
def ajax_test():
    session_id = request.cookies.get('session_id')
    kana_state = session.get('kana_state')
    data = request.get_json()
    kanamoji = data['kanamoji']
    next_kana_count = ''
    prev_romaji = session.get('romaji')

    if kanamoji == 'start':
        cur_kana = random.choice(kana_state)
        next_kana = random.choice(kana_state)
        prev_kana = session.get('cur_kana')
        session['cur_kana'] = cur_kana
        session['next_kana'] = next_kana

        with db.session as db_session:
            kana_model = Kana.query(db_session).filter(
                    or_(Kana.hiragana == cur_kana, Kana.katakana == cur_kana)).first()
            session['romaji'] = kana_model.romaji
    else:
        render_time = data['render_time']
        submit_time = data['submit_time']
        render_time = datetime.utcfromtimestamp(float(render_time) / 1000)
        submit_time = datetime.utcfromtimestamp(float(submit_time) / 1000)
        use_time = submit_time - render_time
        cur_kana = session.get('cur_kana')

        if kanamoji == prev_romaji:
            if session.get('result')[1] == True:
                session['result'][0] += 2
                session['result'][3] += 2
                session['result'][2] = '+2'
            else:
                session['result'][0] += 1
                session['result'][3] += 1
                session['result'][2] = '+1'
                session['result'][1] = True
        else:
            if session.get('result')[1] == False:
                session['result'][0] -= 2
                session['result'][3] -= 2
                session['result'][2] = '-2'
            else:
                session['result'][0] -= 1
                session['result'][3] -= 1
                session['result'][2] = '-1'
                session['result'][1] = False

        with db.session as db_session:
            user = User.query(db_session).filter_by(session_id=session_id).first()
            kana_model = Kana.query(db_session).filter(
                    or_(Kana.hiragana == cur_kana, Kana.katakana == cur_kana)).first()
            user.score = session.get('result')[0]
            kanatest = KanaTest(kanamoji=cur_kana)
            kanatest.use_time = use_time
            kanatest.submit_time = submit_time
            kanatest.enter_str = kanamoji
            kanatest.kana = kana_model
            kanatest.user = user
            kanatest.result = session.get('result')[1]
            db_session.add(user, kanatest)
            db_session.commit()

        cur_kana = session['next_kana']
        next_kana = random.choice(kana_state)
        prev_kana = session.get('cur_kana')
        session['cur_kana'] = cur_kana
        session['next_kana'] = next_kana
        with db.session as db_session:
            kana_model = Kana.query(db_session).filter(
                    or_(Kana.hiragana == cur_kana, Kana.katakana == cur_kana)).first()
            session['romaji'] = kana_model.romaji

            count_0 = db_session.query(KanaTest).join(KanaTest.user)\
                .filter(and_(User.session_id == session_id,
                             KanaTest.kanamoji == next_kana,
                             KanaTest.result == True)).count()
            count_1 = db_session.query(KanaTest).join(KanaTest.user)\
                .filter(and_(User.session_id == session_id,
                             KanaTest.kanamoji == next_kana,
                             KanaTest.result == False)).count()
            count_2 = db_session.query(KanaTest).join(KanaTest.user)\
                .filter(and_(User.session_id == session_id,
                             KanaTest.kanamoji == next_kana)).count()
            next_kana_count = '{0}/{1}/{2}'.format(count_0, count_1, count_2)

    return jsonify(display_kana=[prev_kana,
                                 session.get('cur_kana'),
                                 session.get('next_kana'), ],
                   result=session.get('result'),
                   romaji=prev_romaji,
                   next_kana_count=next_kana_count)