Skip to content
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

use timezone.now() instead of datetime.today() to determine time #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions locking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from datetime import datetime, timedelta

from django import VERSION
if VERSION[1] > 3:
from django.utils.timezone import now
else:
now = datetime.now

from django.db import models
from django.conf import settings
try:
Expand Down Expand Up @@ -85,7 +91,7 @@ def is_locked(self):
if isinstance(self.locked_at, datetime):
# tue -> time delta until expiration
_tue = timedelta(seconds=settings.LOCKING['time_until_expiration'])
if (datetime.today() - self.locked_at) < _tue:
if (now() - self.locked_at) < _tue:
return True
else:
return False
Expand All @@ -105,7 +111,7 @@ def lock_seconds_remaining(self):
a new lock using the ``lock_for`` method.
"""
_tue = timedelta(settings.LOCKING['time_until_expiration'])
diff = _tue - (datetime.today() - self.locked_at)
diff = _tue - (now() - self.locked_at)
return (diff.days * 24 * 60 * 60) + diff.seconds

def lock_for(self, user, hard_lock=True):
Expand Down Expand Up @@ -136,7 +142,7 @@ def lock_for(self, user, hard_lock=True):
raise ObjectLockedError("This object is already locked by another"
" user. May not override, except through the `unlock` method.")
else:
self._locked_at = datetime.today()
self._locked_at = now()
self._locked_by = user
self._hard_lock = self.__init_hard_lock = hard_lock
date = self.locked_at.strftime("%H:%M:%S")
Expand Down