I have this query et:
plist = UserProfile.objects.filter(q).order_by('-created_at')
The model is:
class UserProfile(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=30, blank=True)
created_at = models.DateTimeField(auto_now_add=True, blank=True)
#some more fields
def last_seen(self):
return cache.get('seen_%s' % self.user.username)
def online(self):
if self.last_seen():
now = datetime.datetime.now()
if now > self.last_seen() + datetime.timedelta(
seconds=settings.USER_ONLINE_TIMEOUT):
return False
else:
return True
What I want to achieve is to filter out further plist
to contain only online users.
I tried different tricks, like:
for p in plist:
if p.online:
print 'profile onlnine \n', p.id
online_plist.append(p)
plist = online_plist
But it does not work. So appreciate your hints.
UPDATE: here is how the online users cache is set:
class ActiveUserMiddleware:
def process_request(self, request):
current_user = request.user
if request.user.is_authenticated():
now = datetime.datetime.now()
cache.set('seen_%s' % (current_user.username), now,
settings.USER_LASTSEEN_TIMEOUT)