diff --git a/loginurl/models.py b/loginurl/models.py index bd39d9a..2bba597 100644 --- a/loginurl/models.py +++ b/loginurl/models.py @@ -1,9 +1,13 @@ +from __future__ import unicode_literals + from django.db import models from django.contrib.auth.models import User from django.utils import timezone +from django.utils.encoding import python_2_unicode_compatible from loginurl.utils import create_key +@python_2_unicode_compatible class Key(models.Model): """ A simple key store. @@ -15,8 +19,8 @@ class Key(models.Model): expires = models.DateTimeField(null=True, blank=True) next = models.CharField(null=True, blank=True, max_length=200) - def __unicode__(self): - return '%s (%s)' % (self.key, self.user.username) + def __str__(self): + return '{} ({})'.format(self.key, self.user.username) def save(self, *args, **kwargs): if not self.key: @@ -47,3 +51,4 @@ def update_usage(self): if self.usage_left is not None and self.usage_left > 0: self.usage_left -= 1 self.save() + diff --git a/loginurl/tests.py b/loginurl/tests.py index 7bcf337..5be4da8 100644 --- a/loginurl/tests.py +++ b/loginurl/tests.py @@ -77,7 +77,7 @@ def testValid(self): utils.cleanup() self.assertEqual(len(Key.objects.all()), 1) - + def testExpired(self): oneweekago = timezone.now() - timedelta(days=7) data = utils.create(self.user, usage_left=1, expires=oneweekago) @@ -85,7 +85,7 @@ def testExpired(self): utils.cleanup() self.assertEqual(len(Key.objects.all()), 0) - + def testAlwaysValid(self): data = utils.create(self.user, usage_left=None, expires=None) self.assertEqual(len(Key.objects.all()), 1) @@ -113,21 +113,21 @@ def testValid(self): oneweek = timezone.now() + timedelta(days=7) data = Key.objects.create(user=self.user, usage_left=1, expires=oneweek) self.assertTrue(data.is_valid()) - + def testExpired(self): oneweekago = timezone.now() - timedelta(days=7) data = Key.objects.create(user=self.user, usage_left=1, expires=oneweekago) self.assertFalse(data.is_valid()) - + def testAlwaysValid(self): data = Key.objects.create(user=self.user, usage_left=None, expires=None) self.assertTrue(data.is_valid()) - + def testBothInvalid(self): oneweekago = timezone.now() - timedelta(days=7) data = Key.objects.create(user=self.user, usage_left=-1, expires=oneweekago) self.assertFalse(data.is_valid()) - + class ModelUpdateUsageTestCase(BaseTestCase): def testDefault(self): data = Key.objects.create(user=self.user) @@ -137,7 +137,7 @@ def testDefault(self): datadb = Key.objects.get(key=data.key) self.assertEqual(datadb.usage_left, 0) - + def testNone(self): data = Key.objects.create(user=self.user, usage_left=None) self.assertEqual(data.usage_left, None) @@ -187,7 +187,7 @@ def testValid(self): def testInvalidKey(self): data = utils.create(self.user) - invalid_key = '%s-invalid' % data.key + invalid_key = '{}-invalid'.format(data.key) res = self.backend.authenticate(invalid_key) self.assertEqual(res, None) @@ -237,7 +237,7 @@ def test(request, key): self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], settings.LOGIN_REDIRECT_URL) - + datadb = Key.objects.get(key=data.key) self.assertEqual(datadb.usage_left, 0) @@ -254,8 +254,8 @@ def test(request): res = test(req) - next = '%s?next=%s' % (settings.LOGIN_URL, - settings.LOGIN_REDIRECT_URL) + next = '{}?next={}'.format(settings.LOGIN_URL, + settings.LOGIN_REDIRECT_URL) self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], next) @@ -277,7 +277,7 @@ def test(request, key): self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], '/next/page/') - + datadb = Key.objects.get(key=data.key) self.assertEqual(datadb.usage_left, 0) @@ -299,7 +299,7 @@ def test(request, key): self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], '/next/page/') - + datadb = Key.objects.get(key=data.key) self.assertEqual(datadb.usage_left, 0) @@ -321,7 +321,7 @@ def test(request, key): self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], '/next/database/') - + datadb = Key.objects.get(key=data.key) self.assertEqual(datadb.usage_left, 0) @@ -338,7 +338,7 @@ def test(request): res = test(req) - next = '%s?next=%s' % (settings.LOGIN_URL, '/next/query-string/') + next = '{}?next={}'.format(settings.LOGIN_URL, '/next/query-string/') self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], next) @@ -347,7 +347,7 @@ def testCall(self): from loginurl.management.commands import loginurl_cleanup mock = Mock() - + @patch.object(utils, 'cleanup', mock) def test(): management.call_command('loginurl_cleanup') diff --git a/loginurl/utils.py b/loginurl/utils.py index 2a1c6c8..320590f 100644 --- a/loginurl/utils.py +++ b/loginurl/utils.py @@ -13,15 +13,15 @@ def _create_token(user): The token is created from the user id and a unique id generated from UUIDv4. Then both are hashed using MD5 digest algorithm. """ - id = '%d-%s' % (user.id, str(uuid.uuid4())) - hash = hashlib.md5(id) + id = '{}-{}'.format(user.id, str(uuid.uuid4())) + hash = hashlib.md5(id.encode('ascii')) hash.digest() return hash.hexdigest() def create_key(user): token = _create_token(user) b36_uid = int_to_base36(user.id) - key = '%s-%s' % (b36_uid, token) + key = '{}-{}'.format(b36_uid, token) return key @@ -85,7 +85,7 @@ def cleanup(): """ from loginurl.models import Key - data = Key.objects.filter(Q(usage_left__lte=0) | + data = Key.objects.filter(Q(usage_left__lte=0) | Q(expires__lt=timezone.now())) if data is not None: data.delete() diff --git a/loginurl/views.py b/loginurl/views.py index 01751f5..ad90ad2 100644 --- a/loginurl/views.py +++ b/loginurl/views.py @@ -43,7 +43,7 @@ def login(request, key): if user is None: url = settings.LOGIN_URL if next is not None: - url = '%s?next=%s' % (url, next) + url = '{}?next={}'.format(url, next) return HttpResponseRedirect(url) # The key is valid, then now log the user in. @@ -55,6 +55,6 @@ def login(request, key): if data.next is not None: next = data.next - + return HttpResponseRedirect(next)