def __init__(self, ext, renderer, parser, model): Collection.__init__(self) self._ext = ext self._renderer = renderer # converts dicts to representations self._model = model self._parser = parser # converts representations to dicts self._repr = {} # request representation as a dict()
def __call__(self, environ, start_response): response = Collection.__call__(self, environ, start_response) if response == None: if self._id: response = {'id': self._id} else: response = {} if isinstance(response, dict): view = environ['wsgiorg.routing_args'][1].get('view', '.') template_file = os.path.join(view, self._function_name + "." + self._ext) return self._renderer(environ, start_response, template_file, response) else: return response
def __call__(self, environ, start_response): size = environ.get('CONTENT_LENGTH', '') if size and self._parser: size = int(size) self._repr = self._parser(environ['wsgi.input'].read(size)) if environ['REQUEST_METHOD'] == "POST" and '_method' in self._repr and self._repr['_method'] in ['PUT', 'DELETE']: environ['REQUEST_METHOD'] = self._repr['_method'] response = Collection.__call__(self, environ, start_response) if response == None: primary = self._model.primary_key.columns.keys()[0] view = environ['wsgiorg.routing_args'][1].get('view', '.') template_file = os.path.join(view, self._function_name + "." + self._ext) method = environ.get('REQUEST_METHOD', 'GET') if self._id: if method == "POST" and "_method" in self._repr and self._repr["_method"] in ["PUT", "DELETE"]: method = self._repr["_method"] del self._repr["_method"] if method == 'GET': result = self._model.select(self._model.c[primary]==self._id).execute() row = result.fetchone() if None == row: return http404(environ, start_response) data = dict(zip(result.keys, row)) return self._renderer(environ, start_response, template_file, {"row": data, "primary": primary}) elif method == 'PUT': self._model.update(self._model.c[primary]==self._id).execute(self._repr) return http303(environ, start_response, self._id) elif method == 'DELETE': self._model.delete(self._model.c[primary]==self._id).execute() return http303(environ, start_response, "./") else: print method return http405(environ, start_response) else: if method == 'GET': result = self._model.select().execute() meta = self._model.columns.keys() data = [dict(zip(result.keys, row)) for row in result.fetchall()] return self._renderer(environ, start_response, template_file, {"data": data, "primary": primary, "meta": meta}) elif method == 'POST': results = self._model.insert(self._repr).execute() return http303(environ, start_response, str(results.last_inserted_ids()[0])) else: return response
def __call__(self, environ, start_response): size = environ.get('CONTENT_LENGTH', '') if size and self._parser: size = int(size) self._repr = self._parser(environ['wsgi.input'].read(size)) if environ[ 'REQUEST_METHOD'] == "POST" and '_method' in self._repr and self._repr[ '_method'] in ['PUT', 'DELETE']: environ['REQUEST_METHOD'] = self._repr['_method'] response = Collection.__call__(self, environ, start_response) if response == None: primary = self._model.primary_key.columns.keys()[0] view = environ['wsgiorg.routing_args'][1].get('view', '.') template_file = os.path.join(view, self._function_name + "." + self._ext) method = environ.get('REQUEST_METHOD', 'GET') if self._id: if method == "POST" and "_method" in self._repr and self._repr[ "_method"] in ["PUT", "DELETE"]: method = self._repr["_method"] del self._repr["_method"] if method == 'GET': result = self._model.select( self._model.c[primary] == self._id).execute() row = result.fetchone() if None == row: return http404(environ, start_response) data = dict(zip(result.keys, row)) return self._renderer(environ, start_response, template_file, { "row": data, "primary": primary }) elif method == 'PUT': self._model.update( self._model.c[primary] == self._id).execute(self._repr) return http303(environ, start_response, self._id) elif method == 'DELETE': self._model.delete( self._model.c[primary] == self._id).execute() return http303(environ, start_response, "./") else: print method return http405(environ, start_response) else: if method == 'GET': result = self._model.select().execute() meta = self._model.columns.keys() data = [ dict(zip(result.keys, row)) for row in result.fetchall() ] return self._renderer(environ, start_response, template_file, { "data": data, "primary": primary, "meta": meta }) elif method == 'POST': results = self._model.insert(self._repr).execute() return http303(environ, start_response, str(results.last_inserted_ids()[0])) else: return response
def __init__(self, ext, renderer): Collection.__init__(self) self._ext = ext self._renderer = renderer