def __init__(self, name, tagline): self.gb = Gutenborg(name, tagline) # Some server test things. self.DocTest = Document(self.gb, "Doc1", "") self.DocTwo = Document(self.gb, "Test Document 2", "") self.gb.add_document(self.DocTest) self.gb.add_document(self.DocTwo) self.u = User(self.gb, "Becca", "#008888") self.gb.add_user(self.u) self.DocTest.subscribe_user(self.u) self.DocTest.insert(self.u, 0, "Testing ", 0) self.DocTest.insert(self.u, 8, "Hello World!", 1)
class Root: def __init__(self, name, tagline): self.gb = Gutenborg(name, tagline) # Some server test things. self.DocTest = Document(self.gb, "Doc1", "") self.DocTwo = Document(self.gb, "Test Document 2", "") self.gb.add_document(self.DocTest) self.gb.add_document(self.DocTwo) self.u = User(self.gb, "Becca", "#008888") self.gb.add_user(self.u) self.DocTest.subscribe_user(self.u) self.DocTest.insert(self.u, 0, "Testing ", 0) self.DocTest.insert(self.u, 8, "Hello World!", 1) def quit(self): sys.exit(); quit.exposed = True def is_logged_in(self): """ Private method: Returns true if your session username is in the active user list, false if your session username is not in the active user list.""" if 'user' in cherrypy.session and cherrypy.session['user'] in self.gb.active_users: return True else: return False def login(self, **args): """ Creates a new user and adds them to the gutenborg object if they aren't already. """ cherrypy.session.acquire_lock() assert not self.is_logged_in(), "This session already has a logged in user." assert 'name' in args and 'color' in args, "Bad request. Args: " + repr(args) cherrypy.session['user'] = User(self.gb, args['name'], args['color']) try: self.gb.add_user(cherrypy.session['user']) except NameError: del cherrypy.session['user'] return "User could not be added because this user exists on another computer." return "User added" login.exposed = True def logout(self, **args): """ Logs a user out """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User not logged in, no need to logout." self.gb.disconnect_user(cherrypy.session['user'], "logout") del cherrypy.session['user'] raise cherrypy.HTTPRedirect("/") logout.exposed = True def info(self, **args): # TODO: We must make this an event! """ Returns a JSON object containing lots of server information """ cherrypy.session.acquire_lock() cherrypy.response.headers['Content-Type'] = 'text/json' response = {} response['name'] = self.gb.name response['tag'] = self.gb.tagline response['active_users'] = [] response['dead_users'] = [] response['documents'] = [] for u in self.gb.active_users[:]: response['active_users'].append(u.get_state()) for u in self.gb.dead_users[:]: response['dead_users'].append(u.get_state()) for d in self.gb.documents[:]: response['documents'].append(d.name) # Are we logged in? if self.is_logged_in(): response['logged_in_username'] = cherrypy.session['user'].name return json.write(response) info.exposed = True def wait(self, **args): """ Sends the events of a logged-in user """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User is not logged in" assert 'last' in args, "History required." user = cherrypy.session['user'] cherrypy.session.release_lock() # Update the user's time so they don't timeout user.touch_time() # Timeout all the users self.gb.timeout_users(20) return user.get_events(int(args['last'])) wait.exposed = True def subscribe_document(self, **args): """ Subscribes a user to a document. A subscribed user will begin to receive updates of that document's status. """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User is not logged in" assert "doc_name" in args, "Document name required" user = cherrypy.session['user'] # Get our document object d = self.gb.get_document_by_name(args['doc_name']) d.subscribe_user(user); subscribe_document.exposed = True def unsubscribe_document(self, **args): """ Causes a user to leave the document. """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User is not logged in" assert "doc_name" in args, "Document name required" user = cherrypy.session['user'] # Get our document object d = self.gb.get_document_by_name(args['doc_name']) d.unsubscribe_user(user); unsubscribe_document.exposed = True def resync_doc(self, **args): """ Sends the entire document state as a resync_doc event to the user """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User is not logged in" assert "doc_name" in args, "Document name required" user = cherrypy.session['user'] # Get our document object d = self.gb.get_document_by_name(args['doc_name']) d.resync(user); resync_doc.exposed = True def insert(self, **args): """ Inserts the text into the document """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User is not logged in" assert "doc_name" in args and "pos" in args and "t" in args and "s" in args, "Bad request- please specify doc_name, pos (caret position), t (text), and s (state)" d = self.gb.get_document_by_name(args['doc_name']) assert d.is_subscribed(cherrypy.session['user']), "You must be subscribed to this document to do that." d.insert(cherrypy.session['user'], int(args['pos']), args['t'], int(args['s'])) insert.exposed = True def remove(self, **args): """ Deletes the text from the document """ cherrypy.session.acquire_lock() assert self.is_logged_in(), "User is not logged in" assert "doc_name" in args and "begin" in args and "end" in args and "s" in args, "Bad request- please specify doc_name, pos (caret position), t (text), and s (state)" d = self.gb.get_document_by_name(args['doc_name']) assert d.is_subscribed(cherrypy.session['user']), "You must be subscribed to this document to do that." d.remove(cherrypy.session['user'], int(args['begin']), int(args['end']), int(args['s'])) remove.exposed = True def index(self, **args): raise cherrypy.InternalRedirect("gb.htm") index.exposed = True