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