Peter and I are trying to implement a queuing feature in a webpage.
Basically if the user is logged in, he can wait in line for getting to
run a robot from a particular page. I gave it my best shot and couldn't
figure it out.
We need it to be able to have one person access a page at a time and
they can be in and out of that page for 5 minutes before someone else
comes in.
I think we could have a page that has a counter, it refreshes every 30
seconds and redirects the page if their time is up.
Please help as it's a real chance for web2py to shine in this school.
After working with Peter I have come up with the following solution
which I think is pretty close.
----
import time
@auth.requires_login()
def run_robot():
id = auth.user.id
user_in_queue = db(db.queue.users_id == id).select()
if len(user_in_queue):
if session.in_now:
users_in_queue = db().select(db.queue.ALL, orderby =
db.queue.end_time)
last_time = 0
for person in users_in_queue:
last_time = person.end_time
return dict(user_in_queue = user_in_queue)
if user_in_queue[0].end_time < time.time():
response.flash = "you can still be here."
return dict() #redirect(URL(r=request,c='default',f='index'))
else:
#redirect(URL(r=request,c='default',f='index'))
else:
users_in_queue = db().select(db.queue.ALL, orderby =
db.queue.end_time)
last_time = 0
for person in users_in_queue:
last_time = users_in_queue.end_time
response.flash = last_time
session.time_to_wait = int(time.time())+(60*len(users_in_queue))
current_time = time.time()
time_to_wait = (60*len(users_in_queue))
start_time = int(time.time())
end_time = int(session.time_to_wait + 60)
db.queue.insert(users_id = id, end_time = end_time,
start_time=start_time) # we add that active user to the queue and add
time to the current time to get the end time.
return dict(start_time = start_time, end_time = end_time, last_time
= last_time)
----