I'm trying to know if my user are online or offline.
I got different apps. I'm using user app for all my users.
user/models.py
class UserProfile(models.Model):
...
def last_seen(self):
return cache.get('last_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
else:
return False
then I've created a new file:
user/middleware.py
import datetime
from django.core.cache import cache
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
class ActiveUserMiddleware(MiddlewareMixin):
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)
In my settings.py (monsite folder):
I added this to MIDDLEWARE
'user.middleware.ActiveUserMiddleware'
Also at the bottom of my settings.py file I've added that: (and I don't really with what I've to replace LOCATION value)
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
# Number of seconds of inactivity before a user is marked offline
USER_ONLINE_TIMEOUT = 300
# Number of seconds that we will keep track of inactive users for before
# their last seen is removed from the cache
USER_LASTSEEN_TIMEOUT = 60 * 60 * 24 * 7
I got this issue:
'bool' object is not callable
/home/dulo0814/monProjetDjango/user/middleware.py in process_request, line 10
UPDATE
user/middleware.py
if request.user.is_authenticated:
monsite/settings.py (python manage.py createcachetable)
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'name_table',
}
}
# Number of seconds of inactivity before a user is marked offline
USER_ONLINE_TIMEOUT = 300
# Number of seconds that we will keep track of inactive users for before
# their last seen is removed from the cache
USER_LASTSEEN_TIMEOUT = 60 * 60 * 24 * 7
{{ user.userprofile.online }} = got None