Now allows username to login instead of API key if they have logged in with an API key at least once.

This commit is contained in:
neviyn 2016-03-15 20:03:01 +00:00
parent 2f5dd712c8
commit ca63d344df
3 changed files with 29 additions and 5 deletions

View File

@ -13,7 +13,7 @@
<form id="submissionform" role="form" class="form-group" method="POST" action="/"> <form id="submissionform" role="form" class="form-group" method="POST" action="/">
<div class="input-group"> <div class="input-group">
{{ form.csrf_token }} {{ form.csrf_token }}
{{ form.api_key(**{'class':'form-control','placeholder':'Insert API Key'}) }} {{ form.api_key(**{'class':'form-control','placeholder':'Insert API Key (or WK Username if you have used this before)'}) }}
<span class="input-group-btn"> <span class="input-group-btn">
<button id="submitbutton" class="btn btn-default" type="submit">Go!</button> <button id="submitbutton" class="btn btn-default" type="submit">Go!</button>
</span> </span>

View File

@ -74,6 +74,19 @@ class TestLogin(BaseTestCase):
response = self.client.get('/user_items') response = self.client.get('/user_items')
self.assert200(response) self.assert200(response)
def test_login_with_username(self):
user = User('test', False, "alternate")
response = self.client.post('/', data={'api_key': user.username})
self.assertRedirects(response, '/quiz')
def test_login_same_username_new_api_key(self):
user = User('test', False, "alternate")
changed_user = User('newapikey', False, "alternate")
self.assertTrue(User.query.get(changed_user.api_key))
self.assertFalse(User.query.get(user.api_key))
response = self.client.post('/', data={'api_key': user.username})
self.assertRedirects(response, '/quiz')
class TestFormats(unittest.TestCase): class TestFormats(unittest.TestCase):

View File

@ -37,14 +37,19 @@ class User(db.Model):
gravatar = db.Column(db.String) gravatar = db.Column(db.String)
last_updated = db.Column(db.DateTime) last_updated = db.Column(db.DateTime)
def __init__(self, api_key, auto_init=True): # auto_init and username should only be changed for testing purposes
def __init__(self, api_key, auto_init=True, username=None):
self.api_key = api_key self.api_key = api_key
self.username = username
self.last_updated = datetime.min self.last_updated = datetime.min
if auto_init: # pragma: no cover if auto_init: # pragma: no cover
self.last_updated = datetime.utcnow() self.last_updated = datetime.utcnow()
self.parse_radicals_and_userdata() self.parse_radicals_and_userdata()
self.parse_kanji() self.parse_kanji()
self.parse_vocabulary() self.parse_vocabulary()
# If a user with this name already exists, delete it as their api key has probably changed to the new input.
if User.query.filter(User.username == self.username).first():
db.session.delete(User.query.filter(User.username == self.username).first())
db.session.add(self) db.session.add(self)
db.session.commit() db.session.commit()
@ -160,18 +165,24 @@ def show_home():
return flask.redirect(flask.url_for('show_quiz')) return flask.redirect(flask.url_for('show_quiz'))
form = LoginForm() form = LoginForm()
if form.validate_on_submit(): if form.validate_on_submit():
user = User.query.get(str(form.api_key.data)) input_data = str(form.api_key.data)
user = User.query.get(input_data)
if not user:
user = User.query.filter(User.username == input_data).first()
if user: if user:
login_user(user, remember=True) login_user(user, remember=True)
return flask.redirect(flask.url_for('show_quiz')) return flask.redirect(flask.url_for('show_quiz'))
else: # pragma: no cover elif len(input_data) == 32: # pragma: no cover
try: try:
new_user = User(str(form.api_key.data)) new_user = User(input_data)
login_user(new_user, remember=True) 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)
return flask.render_template("welcome.html", form=form) return flask.render_template("welcome.html", form=form)
else:
flask.flash("API Key length invalid, or username not already in database.")
return flask.render_template("welcome.html", form=form)
return flask.render_template("welcome.html", form=form) return flask.render_template("welcome.html", form=form)