Browse Source

redirect already logged in user

pull/14/head
Di Wu 9 years ago
parent
commit
427a0421c8
3 changed files with 23 additions and 7 deletions
  1. +17
    -6
      hc/accounts/tests/test_check_token.py
  2. +4
    -0
      hc/accounts/views.py
  3. +2
    -1
      hc/front/views.py

+ 17
- 6
hc/accounts/tests/test_check_token.py View File

@ -4,14 +4,25 @@ from django.test import TestCase
class CheckTokenTestCase(TestCase): class CheckTokenTestCase(TestCase):
def test_it_redirects(self):
alice = User(username="alice")
alice.set_password("secret-token")
alice.save()
def setUp(self):
super(CheckTokenTestCase, self).setUp()
self.alice = User(username="alice")
self.alice.set_password("secret-token")
self.alice.save()
def test_it_redirects(self):
r = self.client.get("/accounts/check_token/alice/secret-token/") r = self.client.get("/accounts/check_token/alice/secret-token/")
assert r.status_code == 302 assert r.status_code == 302
# After login, password should be unusable # After login, password should be unusable
alice_again = User.objects.get(username="alice")
assert not alice_again.has_usable_password()
self.alice.refresh_from_db()
assert not self.alice.has_usable_password()
def test_it_redirects_already_logged_in(self):
# Login
self.client.get("/accounts/check_token/alice/secret-token/")
# Login again, when already authenticated
r = self.client.get("/accounts/check_token/alice/secret-token/")
assert r.status_code == 302

+ 4
- 0
hc/accounts/views.py View File

@ -90,6 +90,10 @@ def login_link_sent(request):
def check_token(request, username, token): def check_token(request, username, token):
if request.user.is_authenticated() and request.user.username == username:
# User is already logged in
return redirect("hc-checks")
user = authenticate(username=username, password=token) user = authenticate(username=username, password=token)
if user is not None: if user is not None:
if user.is_active: if user.is_active:


+ 2
- 1
hc/front/views.py View File

@ -162,8 +162,9 @@ def log(request, code):
# Now go through pings, calculate time gaps, and decorate # Now go through pings, calculate time gaps, and decorate
# the pings list for convenient use in template # the pings list for convenient use in template
wrapped = [] wrapped = []
now = timezone.now()
for i, ping in enumerate(pings): for i, ping in enumerate(pings):
prev = timezone.now() if i == 0 else pings[i - 1].created
prev = now if i == 0 else pings[i - 1].created
duration = prev - ping.created duration = prev - ping.created
if duration > check.timeout: if duration > check.timeout:


Loading…
Cancel
Save