Search code examples
pythondjangodjango-rest-frameworkdjango-middlewaredjango-rest-framework-simplejwt

How to track a user time between login and logout activity i.e. to track that how much time he/she spend between login and logout


I am working on a task. To build a middleware using Django rest framework that can track the user time from the moment he/she logins and do some activity and then logout. I have to track the time he/she spends between login and logout using middleware. Please suggest some proper procedures to complete the task. I am new to django and restframework.


Solution

  • I would say to set a variable to time.time() when they log in, and set another variable to time.time() when they log out. Then subtract the values to get the number of seconds.

    def login ():
      login_time = time.time()
    
    def logout ():
      logout_time = time.time()
    
    #To get the number of seconds the user spent on the platform,
    #subtract login_time from logout_time