From b3efe16e3cd0a54ff5eabc333e0dbd658049ac71 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sun, 18 Oct 2015 00:12:12 +0100 Subject: [PATCH] Added route for level filtered quiz item list. Removed unused code from quiz_item.coffee. --- static/quiz_item.coffee | 2 - static/quiz_item.js | 4 +- wanikaniburned.py | 83 ++++++++++++++++++++++++++--------------- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/static/quiz_item.coffee b/static/quiz_item.coffee index 4fe30e2..6de90f3 100644 --- a/static/quiz_item.coffee +++ b/static/quiz_item.coffee @@ -51,8 +51,6 @@ $(document).keypress (e) -> $("#submit-answer").click (e) -> e.preventDefault(); - answers = $('#answer').text().split(/[,\.]/g); - answers = $.map(answers, (x) -> ) input_answer = document.getElementById('kana').value; if($("#kana").hasClass("correct")) refreshQuestion(); diff --git a/static/quiz_item.js b/static/quiz_item.js index 4bcd35a..a0f3590 100644 --- a/static/quiz_item.js +++ b/static/quiz_item.js @@ -67,10 +67,8 @@ }); $("#submit-answer").click(function(e) { - var answers, input_answer; + var input_answer; e.preventDefault(); - answers = $('#answer').text().split(/[,\.]/g); - answers = $.map(answers, function(x) {}); input_answer = document.getElementById('kana').value; if ($("#kana").hasClass("correct")) { return refreshQuestion(); diff --git a/wanikaniburned.py b/wanikaniburned.py index a433aef..21fc8b6 100644 --- a/wanikaniburned.py +++ b/wanikaniburned.py @@ -90,6 +90,52 @@ class User(db.Model): raise ValueError('Cannot refresh now, try again later.') +def parse_range(input_range): + if input_range.match('\d+([,-]\d+)*'): + result = [] + components = input_range.split(',') + for item in components: + if item.isdigit(): + result.append(item) + else: + range_ends = item.split('-') + result.extend(list(range(range_ends[0], range_ends[1]))) + return result + + +def get_items_with_level_restriction(level_range): + items = [] + radical_count = 0 + for item in json.loads(current_user.radicals): + if item['user_specific'] and item['user_specific']['burned'] and item['level'] in level_range: + radical_count += 1 + if item['image']: + items.append({'item_type': 'radical', 'question': item['image'], 'answer': item['meaning']}) + else: + items.append({'item_type': 'radical', 'question': item['character'], 'answer': item['meaning']}) + kanji_count = 0 + for item in json.loads(current_user.kanji): + kanji_count += 1 + made_answer = "" + if item['user_specific'] and item['user_specific']['burned'] and item['level'] in level_range: + if item['onyomi'] and item['kunyomi']: + made_answer = item['onyomi'] + ',' + item['kunyomi'].replace('.*', '') + elif item['onyomi']: + made_answer = item['onyomi'] + elif item['kunyomi']: + made_answer = item['kunyomi'].replace('.*', '') + items.append({'item_type': 'kanji', 'question': item['character'], 'answer': made_answer, + 'answer_meaning': item['meaning']}) + vocabulary_count = 0 + for item in json.loads(current_user.vocabulary): + if item['user_specific'] and item['user_specific']['burned'] and item['level'] in level_range: + vocabulary_count += 1 + items.append({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'], + 'answer_meaning': item['meaning']}) + return jsonify(radical_count=radical_count, kanji_count=kanji_count, vocabulary_count=vocabulary_count, + item_list=items) + + @login_manager.user_loader def load_user(api_key): return User.query.get(str(api_key)) @@ -131,36 +177,13 @@ def show_quiz(): @app.route('/user_items') @login_required def get_items(): - items = [] - radical_count = 0 - for item in json.loads(current_user.radicals): - if item['user_specific'] and item['user_specific']['burned']: - radical_count += 1 - if item['image']: - items.append({'item_type': 'radical', 'question': item['image'], 'answer': item['meaning']}) - else: - items.append({'item_type': 'radical', 'question': item['character'], 'answer': item['meaning']}) - kanji_count = 0 - for item in json.loads(current_user.kanji): - kanji_count += 1 - made_answer = "" - if item['user_specific'] and item['user_specific']['burned']: - if item['onyomi'] and item['kunyomi']: - made_answer = item['onyomi'] + ',' + item['kunyomi'].replace('.*', '') - elif item['onyomi']: - made_answer = item['onyomi'] - elif item['kunyomi']: - made_answer = item['kunyomi'].replace('.*', '') - items.append({'item_type': 'kanji', 'question': item['character'], 'answer': made_answer, - 'answer_meaning': item['meaning']}) - vocabulary_count = 0 - for item in json.loads(current_user.vocabulary): - if item['user_specific'] and item['user_specific']['burned']: - vocabulary_count += 1 - items.append({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'], - 'answer_meaning': item['meaning']}) - return jsonify(radical_count=radical_count, kanji_count=kanji_count, vocabulary_count=vocabulary_count, - item_list=items) + return get_items_with_level_restriction(list(range(0, 60))) + + +@app.route('/user_items/') +@login_required +def get_items_with_range(level_range): + return get_items_with_level_restriction(parse_range(level_range)) @app.route('/refresh', methods=['POST'])