def on_response(dialog, response_id): if response_id == gtk.RESPONSE_YES: credits_left = settings.save_usage(person, session['credits_cost'], force=True) do_start(credits_left) dialog.destroy()
def start_client(self, computer_no, session_k, person): computer = settings.computers[computer_no] ip = computer[1] name = computer[0] session = self.sessions[session_k] end = datetime.now()+timedelta(minutes=session['duration']) def do_start(credits_left, extension=False): self.log('{person:s} now has {credits:d} credits left today'.format(person=person, credits=credits_left)) self.kill_timeout(ip) self.start_timeout(ip, session['duration'], session['username']) if not extension: self.start_lightdm(ip, session) self.started_clients[computer_no] = { 'end': end, 'username': session['username'], } self.log('{person:s} now has {credits:d} credits left today'.format(person=person, credits=credits_left)) if computer_no in self.started_clients: if self.started_clients[computer_no].get('username', None) == session['username']: if self.started_clients[computer_no].get('end', None) > datetime.now(): extension_lenght = int(( datetime.now() - self.started_clients[computer_no]['end'] ).total_seconds() // 60) + session['duration'] self.log( "'{name:s}' already started - extending {session:s}" " duration until {end:s} for student {person:s}".format( end=str(end), name=name, session=session['name'], person=person, ) ) credits_left = 0 try: credits_left = settings.save_usage(person, extension_lenght) do_start(credits_left, extension=True) except settings.NoMoreCredits: dialog = gtk.MessageDialog(type=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, buttons=gtk.BUTTONS_YES_NO, message_format="{person:s} does not have enough credits - do you want to proceed?".format(person=person)) def on_response(dialog, response_id): if response_id == gtk.RESPONSE_YES: credits_left = settings.save_usage(person, extension_lenght, force=True) do_start(credits_left) dialog.destroy() dialog.connect("response", on_response) dialog.run() return else: self.log( "Session for {name:s} expired, spawning new".format( name=name, ) ) credits_left = 0 try: credits_left = settings.save_usage(person, session['credits_cost']) self.log("Starting new session on {name:s} for person {person:s}, ends in {min:d} minutes".format( name=name, min=session['duration'], person=person, ) ) do_start(credits_left) except settings.NoMoreCredits: dialog = gtk.MessageDialog(type=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, buttons=gtk.BUTTONS_YES_NO, message_format="{person:s} does not have enough credits - do you want to proceed?".format(person=person)) def on_response(dialog, response_id): if response_id == gtk.RESPONSE_YES: credits_left = settings.save_usage(person, session['credits_cost'], force=True) do_start(credits_left) dialog.destroy() dialog.connect("response", on_response) dialog.run()
def start_client(self, computer_no, session_k, person): computer = settings.computers[computer_no] ip = computer[1] name = computer[0] session = self.sessions[session_k] end = datetime.now() + timedelta(minutes=session['duration']) def do_start(credits_left, extension=False): self.log( '{person:s} now has {credits:d} credits left today'.format( person=person, credits=credits_left)) self.kill_timeout(ip) self.start_timeout(ip, session['duration'], session['username']) if not extension: self.start_lightdm(ip, session) self.started_clients[computer_no] = { 'end': end, 'username': session['username'], } self.log( '{person:s} now has {credits:d} credits left today'.format( person=person, credits=credits_left)) if computer_no in self.started_clients: if self.started_clients[computer_no].get( 'username', None) == session['username']: if self.started_clients[computer_no].get( 'end', None) > datetime.now(): extension_lenght = int( (datetime.now() - self.started_clients[computer_no] ['end']).total_seconds() // 60) + session['duration'] self.log( "'{name:s}' already started - extending {session:s}" " duration until {end:s} for student {person:s}". format( end=str(end), name=name, session=session['name'], person=person, )) credits_left = 0 try: credits_left = settings.save_usage( person, extension_lenght) do_start(credits_left, extension=True) except settings.NoMoreCredits: dialog = gtk.MessageDialog( type=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, buttons=gtk.BUTTONS_YES_NO, message_format= "{person:s} does not have enough credits - do you want to proceed?" .format(person=person)) def on_response(dialog, response_id): if response_id == gtk.RESPONSE_YES: credits_left = settings.save_usage( person, extension_lenght, force=True) do_start(credits_left) dialog.destroy() dialog.connect("response", on_response) dialog.run() return else: self.log( "Session for {name:s} expired, spawning new".format( name=name, )) credits_left = 0 try: credits_left = settings.save_usage(person, session['credits_cost']) self.log( "Starting new session on {name:s} for person {person:s}, ends in {min:d} minutes" .format( name=name, min=session['duration'], person=person, )) do_start(credits_left) except settings.NoMoreCredits: dialog = gtk.MessageDialog( type=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, buttons=gtk.BUTTONS_YES_NO, message_format= "{person:s} does not have enough credits - do you want to proceed?" .format(person=person)) def on_response(dialog, response_id): if response_id == gtk.RESPONSE_YES: credits_left = settings.save_usage(person, session['credits_cost'], force=True) do_start(credits_left) dialog.destroy() dialog.connect("response", on_response) dialog.run()