Added and setup frontend for filtering by srs state.

This commit is contained in:
neviyn 2015-11-21 03:00:17 +00:00
parent a907736895
commit c5fb8db98c
4 changed files with 84 additions and 96 deletions

View File

@ -37,39 +37,26 @@ updateQuizItemStats = () ->
$('#vocab-num').text(sessionStorage.getItem('vocabulary_count'));
$('#total-num').text(parseInt(sessionStorage.getItem('radical_count')) + parseInt(sessionStorage.getItem('kanji_count'))+ parseInt(sessionStorage.getItem('vocabulary_count')))
getSrsTypeFilters = () ->
srs_levels = ['burnedcheck','enlightencheck','mastercheck','gurucheck','apprenticecheck']
return (document.getElementById(s).value for s in srs_levels when document.getElementById(s).checked).toString()
filterQuestions = () ->
input_data = $('#filter-input').val()
if(input_data)
sessionStorage.removeItem('user_items')
sessionStorage.removeItem('radical_count')
sessionStorage.removeItem('kanji_count')
sessionStorage.removeItem('vocabulary_count')
target_url = "/user_items?level_range=" + input_data
$.ajax
url: target_url,
dataType: 'json',
success: (e) ->
if(e.hasOwnProperty('error'))
document.getElementById('filter-input').value = ''
$('#error-text').text(e['error'])
$('#error-area').show();
filterQuestions()
else
sessionStorage.setItem('user_items', JSON.stringify(e['item_list']))
sessionStorage.setItem('radical_count', e['radical_count'])
sessionStorage.setItem('kanji_count', e['kanji_count'])
sessionStorage.setItem('vocabulary_count', e['vocabulary_count'])
updateQuizItemStats()
refreshQuestion()
else
sessionStorage.removeItem('user_items')
sessionStorage.removeItem('radical_count')
sessionStorage.removeItem('kanji_count')
sessionStorage.removeItem('vocabulary_count')
$.ajax
url: "/user_items",
dataType: 'json',
success: (e) ->
sessionStorage.removeItem('user_items')
sessionStorage.removeItem('radical_count')
sessionStorage.removeItem('kanji_count')
sessionStorage.removeItem('vocabulary_count')
target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters()
$.ajax
url: target_url,
dataType: 'json',
success: (e) ->
if(e.hasOwnProperty('error'))
document.getElementById('filter-input').value = ''
$('#error-text').text(e['error'])
$('#error-area').show();
filterQuestions()
else
sessionStorage.setItem('user_items', JSON.stringify(e['item_list']))
sessionStorage.setItem('radical_count', e['radical_count'])
sessionStorage.setItem('kanji_count', e['kanji_count'])
@ -80,7 +67,6 @@ filterQuestions = () ->
$('#filter-form').submit( (e) ->
e.preventDefault()
filterQuestions()
refreshQuestion()
$('#modal-filter').modal('hide')
)

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.10.0
(function() {
var filterQuestions, refreshQuestion, updateQuizItemStats;
var filterQuestions, getSrsTypeFilters, refreshQuestion, updateQuizItemStats;
refreshQuestion = function() {
var input_element, items, selection;
@ -49,43 +49,39 @@
return $('#total-num').text(parseInt(sessionStorage.getItem('radical_count')) + parseInt(sessionStorage.getItem('kanji_count')) + parseInt(sessionStorage.getItem('vocabulary_count')));
};
filterQuestions = function() {
var input_data, target_url;
input_data = $('#filter-input').val();
if (input_data) {
sessionStorage.removeItem('user_items');
sessionStorage.removeItem('radical_count');
sessionStorage.removeItem('kanji_count');
sessionStorage.removeItem('vocabulary_count');
target_url = "/user_items?level_range=" + input_data;
return $.ajax({
url: target_url,
dataType: 'json',
success: function(e) {
if (e.hasOwnProperty('error')) {
document.getElementById('filter-input').value = '';
$('#error-text').text(e['error']);
$('#error-area').show();
return filterQuestions();
} else {
sessionStorage.setItem('user_items', JSON.stringify(e['item_list']));
sessionStorage.setItem('radical_count', e['radical_count']);
sessionStorage.setItem('kanji_count', e['kanji_count']);
sessionStorage.setItem('vocabulary_count', e['vocabulary_count']);
updateQuizItemStats();
return refreshQuestion();
}
getSrsTypeFilters = function() {
var s, srs_levels;
srs_levels = ['burnedcheck', 'enlightencheck', 'mastercheck', 'gurucheck', 'apprenticecheck'];
return ((function() {
var i, len, results;
results = [];
for (i = 0, len = srs_levels.length; i < len; i++) {
s = srs_levels[i];
if (document.getElementById(s).checked) {
results.push(document.getElementById(s).value);
}
});
} else {
sessionStorage.removeItem('user_items');
sessionStorage.removeItem('radical_count');
sessionStorage.removeItem('kanji_count');
sessionStorage.removeItem('vocabulary_count');
return $.ajax({
url: "/user_items",
dataType: 'json',
success: function(e) {
}
return results;
})()).toString();
};
filterQuestions = function() {
var target_url;
sessionStorage.removeItem('user_items');
sessionStorage.removeItem('radical_count');
sessionStorage.removeItem('kanji_count');
sessionStorage.removeItem('vocabulary_count');
target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters();
return $.ajax({
url: target_url,
dataType: 'json',
success: function(e) {
if (e.hasOwnProperty('error')) {
document.getElementById('filter-input').value = '';
$('#error-text').text(e['error']);
$('#error-area').show();
return filterQuestions();
} else {
sessionStorage.setItem('user_items', JSON.stringify(e['item_list']));
sessionStorage.setItem('radical_count', e['radical_count']);
sessionStorage.setItem('kanji_count', e['kanji_count']);
@ -93,14 +89,13 @@
updateQuizItemStats();
return refreshQuestion();
}
});
}
}
});
};
$('#filter-form').submit(function(e) {
e.preventDefault();
filterQuestions();
refreshQuestion();
return $('#modal-filter').modal('hide');
});

View File

@ -1,5 +1,7 @@
{% extends "layout.html" %}
{% block content %}
{% set srs_levels = [ ('burned', 'Burned'), ('enlighten', 'Enlightened'), ('master', 'Master'),
('guru', 'Guru'), ('apprentice', 'Apprentice') ] -%}
<div class="modal fade" id="modal-filter" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm">
<div class="modal-content">
@ -10,6 +12,11 @@
<form id="filter-form">
<div class="modal-body">
<input type="text" pattern="\d[\d-,]*\d*" class="form-control" id="filter-input" placeholder="e.g. 1-3,5,8">
<br />
{% for srs_tag, srs_text in srs_levels %}
<input type="checkbox" id="{{ srs_tag }}check" name="{{ srs_tag }}" value="{{ srs_tag }}" {% if srs_tag == "burned" %}checked{% endif %}><label for="{{ srs_tag }}check">{{ srs_text }}</label>
<br />
{% endfor %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

View File

@ -1,5 +1,5 @@
import json, re
from flask import Flask, render_template, redirect, url_for, jsonify, flash, request
import flask
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.wtf import Form
@ -8,7 +8,7 @@ from wtforms import StringField
from wtforms.validators import DataRequired
from datetime import datetime, timedelta
app = Flask(__name__)
app = flask.Flask(__name__)
app.config.from_pyfile('config.py')
login_manager = LoginManager()
login_manager.init_app(app)
@ -100,7 +100,7 @@ def parse_range(input_range):
result.append(int(item))
else:
range_ends = item.split('-')
result.extend(list(range(range_ends[0], range_ends[1])))
result.extend(list(range(int(range_ends[0]), int(range_ends[1]) + 1)))
return result
@ -134,10 +134,10 @@ def get_items_with_level_restriction(level_range, item_state):
items.append({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'],
'answer_meaning': item['meaning']})
if not items:
return jsonify(error="No items within these filter parameters")
return flask.jsonify(error="No items within these filter parameters")
else:
return jsonify(radical_count=radical_count, kanji_count=kanji_count, vocabulary_count=vocabulary_count,
item_list=items)
return flask.jsonify(radical_count=radical_count, kanji_count=kanji_count, vocabulary_count=vocabulary_count,
item_list=items)
@login_manager.user_loader
@ -148,22 +148,22 @@ def load_user(api_key):
@app.route('/', methods=('GET', 'POST'))
def show_home():
if current_user.is_authenticated:
return redirect(url_for('show_quiz'))
return flask.redirect(flask.url_for('show_quiz'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.get(str(form.api_key.data))
if user:
login_user(user)
return redirect(url_for('show_quiz'))
return flask.redirect(flask.url_for('show_quiz'))
else:
try:
new_user = User(str(form.api_key.data))
login_user(new_user)
return redirect(url_for('show_quiz'))
return flask.redirect(flask.url_for('show_quiz'))
except ValueError as err:
flash(err)
return render_template("welcome.html", form=form)
return render_template("welcome.html", form=form)
flask.flash(err)
return flask.render_template("welcome.html", form=form)
return flask.render_template("welcome.html", form=form)
@login_required
@ -171,22 +171,22 @@ def show_home():
def show_quiz():
if not login_fresh():
if User.query.get(current_user.api_key):
return render_template("quiz.html")
return flask.render_template("quiz.html")
else:
logout_user()
return redirect(url_for('show_home'))
return render_template("quiz.html")
return flask.redirect(flask.url_for('show_home'))
return flask.render_template("quiz.html")
@app.route('/user_items')
@login_required
def get_items():
level_range = list(range(0, 61))
if request.args.get('level_range'):
level_range = parse_range(request.args.get('level_range'))
if flask.request.args.get('level_range') is not '':
level_range = parse_range(flask.request.args.get('level_range'))
item_state = ['burned']
if request.args.get('item_state'):
item_state = request.args.get('item_state').split(',')
if flask.request.args.get('item_state') is not '':
item_state = flask.request.args.get('item_state').split(',')
return get_items_with_level_restriction(level_range, item_state)
@ -202,16 +202,16 @@ def refresh_api():
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('show_home'))
return flask.redirect(flask.url_for('show_home'))
@login_manager.unauthorized_handler
def unauthorized():
return redirect(url_for('show_home'))
return flask.redirect(flask.url_for('show_home'))
def datetime_format(input):
return input.strftime("%d %B %Y %I:%M%p")
def datetime_format(input_data):
return input_data.strftime("%d %B %Y %I:%M%p")
if __name__ == '__main__':