diff --git a/templates/welcome.html b/templates/welcome.html index 36757f2..d6e7dfc 100644 --- a/templates/welcome.html +++ b/templates/welcome.html @@ -13,7 +13,7 @@
{{ 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)'}) }} diff --git a/tests.py b/tests.py index 7aa81f0..b778f9f 100644 --- a/tests.py +++ b/tests.py @@ -74,6 +74,19 @@ class TestLogin(BaseTestCase): response = self.client.get('/user_items') 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): diff --git a/wanikaniburned.py b/wanikaniburned.py index 99270ce..edef4e7 100644 --- a/wanikaniburned.py +++ b/wanikaniburned.py @@ -37,14 +37,19 @@ class User(db.Model): gravatar = db.Column(db.String) 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.username = username self.last_updated = datetime.min if auto_init: # pragma: no cover self.last_updated = datetime.utcnow() self.parse_radicals_and_userdata() self.parse_kanji() 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.commit() @@ -160,18 +165,24 @@ def show_home(): return flask.redirect(flask.url_for('show_quiz')) form = LoginForm() 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: login_user(user, remember=True) return flask.redirect(flask.url_for('show_quiz')) - else: # pragma: no cover + elif len(input_data) == 32: # pragma: no cover try: - new_user = User(str(form.api_key.data)) + new_user = User(input_data) login_user(new_user, remember=True) return flask.redirect(flask.url_for('show_quiz')) except ValueError as err: flask.flash(err) 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)