diff --git a/locking/models.py b/locking/models.py index 159b76b..193e968 100644 --- a/locking/models.py +++ b/locking/models.py @@ -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: @@ -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 @@ -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): @@ -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")