1

I have rather unusual task, so I would like to ask for a piece of advice from experts :)

I need to build small Flask-based web which will have build-in video player. Users will have to log-in to access videos. The problem is that I need to limit user by the amount of time they can spend using the service.

Could someone please recommend a possible way to make it work or help me to find a place to get started?

What I am thinking of... what if i create user's profile variable like "credits_minutes", and i could find a way to decrease credits_minutes every minute by one?

Ilja Leiko
  • 426
  • 2
  • 7
  • 22
  • 1
    Using `Flask-Session` you can set session timeout, Follow this https://stackoverflow.com/questions/11783025/is-there-an-easy-way-to-make-sessions-timeout-in-flask – Rajmani Arya Apr 04 '18 at 08:06
  • Thank you, Rajmani. I am afraid this would not work. This is only to end user's session after 5 minutes. They would still be able to log-in back, if they want. In my case I need ti keep track of how long the users has spend time using web, and not allow to use more, if certain amount of time has passed. – Ilja Leiko Apr 04 '18 at 08:10

2 Answers2

2

The sessions are based on requests from my understanding what you are trying to do is to actually get the amount of time spent on the site? You'll need to do some kind of keep alive from the client.

Such as web sockets, repetitive JavaScript calls or something else to know that they are on the actual site and base you logic on that.

A simple solution would be to write something with jquery that polls an endpoint of you choice where you could do something time based for each poll. Such as saving the oldest call and comparing it to each new that is arriving. and when X minutes has elapsed, redirect the user.

Tobias
  • 101
  • 2
  • Thank you, Tobias, that does sound as something i would need for my system. Unfortunately, i have never worked with JavaScript, do u have any suggesting where to start with jquery to make it work as you described? – Ilja Leiko Apr 04 '18 at 09:02
  • If you have no experience at all i would suggest something on youtube if you prefer that kind of learning. Otherwise https://www.w3schools.com/jquery/default.asp would have alot of basic information required for this. But a combination of an ajax call and setInterval for that function would be sufficent. – Tobias Apr 04 '18 at 09:09
  • Thank you, i will start with that – Ilja Leiko Apr 04 '18 at 09:23
0

From the Flask-Session documentation: https://pythonhosted.org/Flask-Session/

PERMANENT_SESSION_LIFETIME: the lifetime of a permanent session as datetime.timedelta object. Starting with Flask 0.8 this can also be an integer representing seconds.