Skip to content

Fix lock expiration and seconds remaining when lock passes 24 hours old #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions locking/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

from datetime import datetime
from datetime import datetime, timedelta

from django.db import models
from django.conf import settings
Expand Down Expand Up @@ -79,7 +79,7 @@ def is_locked(self):
Works by calculating if the last lock (self.locked_at) has timed out or not.
"""
if isinstance(self.locked_at, datetime):
if (datetime.today() - self.locked_at).seconds < settings.LOCKING['time_until_expiration']:
if (datetime.today() - self.locked_at) < timedelta(seconds=settings.LOCKING['time_until_expiration']):
return True
else:
return False
Expand All @@ -97,7 +97,8 @@ def lock_seconds_remaining(self):
If you want to extend a lock beyond its current expiry date, initiate a new
lock using the ``lock_for`` method.
"""
return settings.LOCKING['time_until_expiration'] - (datetime.today() - self.locked_at).seconds
diff = timedelta(settings.LOCKING['time_until_expiration']) - (datetime.today() - self.locked_at)
return (diff.days * 24 * 60 * 60) + diff.seconds

def lock_for(self, user, hard_lock=True):
"""
Expand Down
34 changes: 33 additions & 1 deletion locking/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,39 @@ def test_lock_expiration(self):
self.assertTrue(self.story.is_locked)
self.story._locked_at = datetime.today() - timedelta(minutes=LOCK_TIMEOUT+1)
self.assertFalse(self.story.is_locked)


def test_lock_expiration_day(self):
self.story.lock_for(self.user)
self.assertTrue(self.story.is_locked)
self.story._locked_at = datetime.today() - timedelta(days=1, seconds=1)
self.assertFalse(self.story.is_locked)

def test_lock_seconds_remaining(self):
self.story.lock_for(self.user)
expected = LOCK_TIMEOUT
self.assertTrue(self.story.lock_seconds_remaining <= expected + 1 and
self.story.lock_seconds_remaining >= expected - 1,
"%d not close to %d" % (
self.story.lock_seconds_remaining, expected))

def test_lock_seconds_remaining_half_timeout(self):
self.story.lock_for(self.user)
self.story._locked_at -= timedelta(seconds=(LOCK_TIMEOUT / 2))
expected = LOCK_TIMEOUT / 2
self.assertTrue(self.story.lock_seconds_remaining <= expected + 1 and
self.story.lock_seconds_remaining >= expected - 1,
"%d not close to %d" % (
self.story.lock_seconds_remaining, expected))

def test_lock_seconds_remaining_day(self):
self.story.lock_for(self.user)
self.story._locked_at -= timedelta(days=1)
expected = LOCK_TIMEOUT - (24 * 60 * 60)
self.assertTrue(self.story.lock_seconds_remaining <= expected + 1 and
self.story.lock_seconds_remaining >= expected - 1,
"%d not close to %d" % (
self.story.lock_seconds_remaining, expected))

def test_lock_applies_to(self):
self.story.lock_for(self.alt_user)
applies = self.story.lock_applies_to(self.user)
Expand Down