Skip to content

Commit

Permalink
Python 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
fajran committed Apr 23, 2014
1 parent f67f649 commit 096a0bb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
9 changes: 7 additions & 2 deletions loginurl/models.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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()

32 changes: 16 additions & 16 deletions loginurl/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ 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)
self.assertEqual(len(Key.objects.all()), 1)

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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions loginurl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions loginurl/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -55,6 +55,6 @@ def login(request, key):

if data.next is not None:
next = data.next

return HttpResponseRedirect(next)

0 comments on commit 096a0bb

Please sign in to comment.