Added login persistence.

This commit is contained in:
neviyn 2015-12-24 17:03:54 +00:00
parent 441623a947
commit eba6c19f6e

View File

@ -3,7 +3,8 @@ import re
from datetime import datetime, timedelta 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, \
confirm_login
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.wtf import Form from flask.ext.wtf import Form
import requests import requests
@ -118,7 +119,7 @@ def get_items_with_level_restriction(level_range, item_state, item_types):
loads = json.loads loads = json.loads
if 'radical' in item_types: if 'radical' in item_types:
items.extend({'item_type': 'radical', 'answer': item['meaning'], 'question': item['image'] 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['image'] else item['character']} for item in loads(current_user.radicals)
if item['user_specific'] and item['user_specific']['srs'] in item_state if item['user_specific'] and item['user_specific']['srs'] in item_state
and item['level'] in level_range) and item['level'] in level_range)
radical_count = len(items) radical_count = len(items)
@ -161,12 +162,12 @@ def show_home():
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.get(str(form.api_key.data)) user = User.query.get(str(form.api_key.data))
if user: if user:
login_user(user) login_user(user, remember=True)
return flask.redirect(flask.url_for('show_quiz')) return flask.redirect(flask.url_for('show_quiz'))
else: else:
try: try:
new_user = User(str(form.api_key.data)) new_user = User(str(form.api_key.data))
login_user(new_user) login_user(new_user, remember=True)
return flask.redirect(flask.url_for('show_quiz')) return flask.redirect(flask.url_for('show_quiz'))
except ValueError as err: except ValueError as err:
flask.flash(err) flask.flash(err)
@ -179,6 +180,7 @@ def show_home():
def show_quiz(): def show_quiz():
if not login_fresh(): if not login_fresh():
if User.query.get(current_user.api_key): if User.query.get(current_user.api_key):
confirm_login()
return flask.render_template("quiz.html") return flask.render_template("quiz.html")
else: else:
logout_user() logout_user()