This is similar to yours but I am posting it anyway:
db.define_table('robot',
Field('user_id',db.auth_user),
Field('time_request_control','datetime'),
Field('time_start_control','datetime'),
Field('time_stop_control','datetime'),
Field('status',default='pending',
requires=IS_IN_SET(('queued','running','cancelled','done'))))
def has_control(user_id,seconds=300):
import datetime
# if user aready has control return
true
if db(db.robot.user_id==user_id)
(db.robot.status=='running').select().first():
return True
# if you have not done so, request
control:
if not db(db.robot.user_id==user_id)
(db.robot.status=='queued').count():
db.robot.insert(user_id=user_id,time_request_control=request.now,status='queued')
# if somebody lese has control return
false
# but if somebody else control expired take away
control
row=db(db.robot.user_id!=user_id)
(db.robot.status=='running').select().first()
if row:
if row.time_stop_control>=request.now:
return False
row.update_record(status='done')
# if nobody else has control of robot, look for next is
queue
row =
db(db.robot.status=='queued').select(orderby=db.robot.time_request_control).first()
# if user_id is next in queue, give him
control
if row.user_id==user_id:
row.update_record(time_start_control=request.now,
time_stop_control=request.now
+datetime.timedelta(seconds=seconds),
status='running')
return True
# else somebody else is next in
queue
else:
# TODO eventually if somebody requested control but does not
exercise for X secs,
# control should be
revoked.
return False
so just call has_access(auth.user_id) and it returns true if user has
control of the robot.
On May 11, 1:57 pm, Jason Brower <[email protected]> wrote:
> 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)
> ----