Switched user_item response generation to use list comprehensions.

This commit is contained in:
neviyn 2015-12-12 00:01:19 +00:00
parent a9e48abd96
commit 35ad032d88

View File

@ -1,4 +1,7 @@
import json, re import json
import re
from datetime import datetime, timedelta
import flask import flask
from flask.ext.login import LoginManager, login_user, login_required, current_user, logout_user, login_fresh from flask.ext.login import LoginManager, login_user, login_required, current_user, logout_user, login_fresh
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
@ -6,7 +9,6 @@ from flask.ext.wtf import Form
import requests import requests
from wtforms import StringField from wtforms import StringField
from wtforms.validators import DataRequired from wtforms.validators import DataRequired
from datetime import datetime, timedelta
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.config.from_pyfile('config.py') app.config.from_pyfile('config.py')
@ -104,38 +106,37 @@ def parse_range(input_range):
return result return result
def combine_onyomi_and_kunyomi(onyomi, kunyomi):
if onyomi and kunyomi:
return onyomi + ',' + kunyomi.replace('.*', '')
return onyomi if onyomi else kunyomi.replace('.*', '')
def get_items_with_level_restriction(level_range, item_state, item_types): def get_items_with_level_restriction(level_range, item_state, item_types):
items = [] items = []
radical_count = 0 radical_count = 0
loads = json.loads
if 'radical' in item_types: if 'radical' in item_types:
for item in json.loads(current_user.radicals): items.extend({'item_type': 'radical', 'answer': item['meaning'], 'question': item['image']
if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range: if item['image'] else item['character']} for item in loads(current_user.radicals)
radical_count += 1 if item['user_specific'] and item['user_specific']['srs'] in item_state
if item['image']: and item['level'] in level_range)
items.append({'item_type': 'radical', 'question': item['image'], 'answer': item['meaning']}) radical_count = len(items)
else:
items.append({'item_type': 'radical', 'question': item['character'], 'answer': item['meaning']})
kanji_count = 0 kanji_count = 0
if 'kanji' in item_types: if 'kanji' in item_types:
for item in json.loads(current_user.kanji): items.extend({'item_type': 'kanji', 'question': item['character'],
made_answer = "" 'answer': combine_onyomi_and_kunyomi(item['onyomi'], item['kunyomi']),
if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range: 'answer_meaning': item['meaning']} for item in loads(current_user.kanji)
kanji_count += 1 if item['user_specific'] and item['user_specific']['srs'] in item_state
if item['onyomi'] and item['kunyomi']: and item['level'] in level_range)
made_answer = item['onyomi'] + ',' + item['kunyomi'].replace('.*', '') kanji_count = len(items) - radical_count
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 vocabulary_count = 0
if 'vocab' in item_types: if 'vocab' in item_types:
for item in json.loads(current_user.vocabulary): items.extend({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'],
if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range: 'answer_meaning': item['meaning']} for item in loads(current_user.vocabulary)
vocabulary_count += 1 if item['user_specific'] and item['user_specific']['srs'] in item_state
items.append({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'], and item['level'] in level_range)
'answer_meaning': item['meaning']}) vocabulary_count = len(items) - (radical_count + kanji_count)
if not items: if not items:
return flask.jsonify(error="No items within these filter parameters") return flask.jsonify(error="No items within these filter parameters")
else: else:
@ -197,10 +198,11 @@ def get_items():
@app.route('/refresh', methods=['POST']) @app.route('/refresh', methods=['POST'])
@login_required
def refresh_api(): def refresh_api():
try: try:
current_user.update_all() current_user.update_all()
return "Refresh Successful", 202 return str(datetime_format(current_user.last_updated)), 202
except ValueError as err: except ValueError as err:
return str(err), 500 return str(err), 500