Work with sessions in django. How to track the number of users on the page for an hour and the number of active users on the page for 24 hours?

Good time of day. The task described in the question. Suppose I have 2 pages and they need to track, initially the idea was to do something like that
request.session['my page'] = True
But all my sessions are stored in the database in cache, therefore I have to decode every entry to get the dictionary, to track the value and count. It's kind of like a lot of code and is inefficient. Even at the level of ideas don't understand how to properly implement it.
July 2nd 19 at 13:07
1 answer
July 2nd 19 at 13:09
Solution
To deal with this issue, maybe someone will be useful. Django by default stores data about sessions(only if you changed something in the session) in the Session model.
I made a table MySession
class MySession(models.Model):
 session_key = models.ForeignKey(Session) # link with the Session table
 name_board = models.CharField(max_length=1024) #Page is for tracking
 expire_date = models.DateTimeField() # Time of life
 active = models.CharField(max_length=1024, blank=True) #monitor activity on the page
 thread = models.TextField(blank=True) #Page is for tracking


Then in the vyuha, describing the session and Mysession.
request.session.set_expiry(3600) #Set my desired lifetime of the session
request.session.save()

 if len(MySession.objects.filter(session_key=request.session.session_key)) != 0: # Check if there is any information about this session, Mysession, is there any information about visit this page. If the information is not, add.
 session = MySession.objects.get(session_key=request.session.session_key)
 if name_board in session.name_board:
pass
else:
 session.name_board += ', ' + name_board
session.save()
 else: MySession.objects.create(session_key=Session.objects.get(pk=request.session.session_key), name_board=name_board, expire_date=Session.objects.get(pk=request.session.session_key).expire_date)
 session = MySession.objects.get(session_key=request.session.session_key)
 session.save()


Then I create a custom django command, to remove all sessions whose lifetime has expired and further counting the number of remaining sessions. Will be considered through the signals
usr_hour = Signal(providing_args=[]) # create a signal

class Command(BaseCommand):

 def handle(self, *args, **options):
 now = timezone.now() # Set the time to check the life sessions
 two_hour = now - datetime.timedelta(hours=2)
 MySession.objects.filter(expire_date__range=(two_hour, now)).delete()
 Session.objects.filter(expire_date__range=(two_hour, now)).delete()

 usr_hour.connect(my_callback) #send the signal
usr_hour.send(sender=self.__class__)
usr_hour.disconnect(my_callback)

 self.stdout.write('Succes')


Signal
def my_callback(sender, **kwargs):
 boards = Board.objects.all()
 for x in Board:
 x.board_in_hour = MySession.objects.filter(name_board__contains=x.board_shortcut).count()
 x.save()


Add the run command db_hour CZK for each hour. Thus it is possible to count the number of users for each hour to ODA. pages

Find more questions by tags Web DevelopmentPythonDjango