def lockTimeRemaining(self): """ returns the seconds the lock remains valid """ if self.lockedAt is not None: timeoutPoint = pointOfTimeout() if self.lockedAt > timeoutPoint: #lock is valid timeConsumed = (datetime.utcnow().replace(tzinfo=utc) - self.lockedAt) LOCK_TIMEOUT = getattr(settings, 'LOCK_TIMEOUT', 1800) delta = timedelta(seconds=LOCK_TIMEOUT) remaining = delta - timeConsumed remainingSeconds = remaining.total_seconds() #calculate minutes, hours, seconds hours, remainder = divmod(remainingSeconds, 3600) minutes, seconds = divmod(remainder, 60) if hours == 0 and minutes == 0: timeRemaining = '%s seconds' % ("{0:.0f}".format(seconds)) elif hours == 0: timeRemaining = '%s minutes and %s seconds' % ("{0:.0f}".format(minutes), "{0:.0f}".format(seconds)) else: timeRemaining = '%s hours, %s minutes and %s seconds' % ("{0:.0f}".format(hours), "{0:.0f}".format(minutes), "{0:.0f}".format(seconds)) return timeRemaining else: #lock expired return 0 else: #no lock set return 0
def checkLockStatus(self): """ Returns True if the lock is valid, returns False if the lock has been expired or has not been set. If lock has been expired method unlocks the lock and saves the instance. """ if self.lockedAt is not None: if self.lockedAt > pointOfTimeout(): #lock is still valid return True else: #lock expired. unlock the lock self.lockedAt = None self.lockedBy = None self.lockedFlag = False self.save() return False return False
def get_query_set(self): timeout = pointOfTimeout() return super(LockedManager, self).get_query_set().filter( lockedAt__gt=timeout, lockedAt__isnull=False)
def get_query_set(self): timeout = pointOfTimeout() return super(UnlockedManager, self).get_query_set().filter( Q(lockedAt__lte=timeout) | Q(lockedAt__isnull=True))