def dispatch(self): cookie = Cookie.SimpleCookie(self.environ.get("HTTP_COOKIE","")) if 'authToken' in cookie: token = cookie["authToken"].value self.checkAuthByToken(token) if not self.loggedUser: self.checkAuth() path = os.path.join(os.path.dirname(__file__), "..", "templates", "login.html") htmlFile = open(path, "r") content = htmlFile.read() htmlFile.close() r = Response(content, encoder=None) r.setHeaders([ ('Content-type', "text/html") ]) return r
def dispatch(self): #URL mapovani na objekty print "POST", self.post print "GET", self.get dispatcherMethod = None url = re.findall(r'([^/]+)', self.environ['PATH_INFO']) if len(url) == 0 or (len(url) == 1 and url[0] == "logout"): if len(url) == 1 and url[0] == "logout": cookie = Cookie.SimpleCookie( self.environ.get("HTTP_COOKIE", "")) if 'authToken' in cookie: token = cookie["authToken"].value conn = datasources.postgres.SQLTransaction() cursor = conn.cursor() cursor.execute( "DELETE FROM pim.users_tokens WHERE user_id = %s AND token = %s", (self.session['user_id'], token)) conn.commit() conn.close() self.session['user_id'] = None self.session.invalidate() path = os.path.join(os.path.dirname(__file__), "..", "templates", "index.html") htmlFile = open(path, "r") content = htmlFile.read() htmlFile.close() r = Response(content, encoder=None) r.setHeaders([('Content-type', "text/html")]) if 'authToken' in self.session: expiration = datetime.datetime.now() + datetime.timedelta( days=30) cookie = Cookie.SimpleCookie() cookie['authToken'] = self.session['authToken'] cookie['authToken']['expires'] = expiration.strftime( '%a, %d %b %Y %H:%M:%S GMT') r.addHeaders([('Set-Cookie', cookie['authToken'].OutputString())]) return r dispatcherClass = None dispatcherMethod = '__default__' #root node pointer = modules for part in url: if hasattr(pointer, part) and type(getattr( pointer, part)) in [types.ClassType, types.ModuleType]: pointer = getattr(pointer, part) dispatcherClass = pointer continue dispatcherMethod = part #deprecated if not dispatcherClass: dispatcherClass = errors dispatcherMethod = "notFound" dispatcher = dispatcherClass(self.session) calee = getattr(dispatcher, dispatcherMethod) attrs = inspect.getargspec(calee)[0] requestData = {} if 'request' in self.request: print self.request['request'].encode("utf8") requestData = json.loads(self.request['request'].encode("utf8")) print "REQUEST", requestData for parameter in requestData.keys(): if parameter not in attrs: del requestData[parameter] return Response(calee(**requestData), encoder=self.format)