def init_db(uri, dbname, main_db=True): """Returns a db object and syncs the design documents on demand. If main_db is set to true then all models will use that one by default. """ server = Server(uri) db = server.get_or_create_db(dbname) if main_db: Document.set_db(db) return db
def launch(app): def _fake_render(handler, template_name, **kwargs): handler.write(json.dumps(kwargs)) handler.finish() RequestHandler.render = _fake_render thread = DebugServerThread(app) thread.start() if app.settings['db_user']: Document.get_db().flush() thread.loaded.acquire()
def __init__(self, entity=None): self.context = Context() if entity: self.entity = entity else: self.entity = Document() self.entity.set_db(self.context.db)
class Repository(object): """ Classe que implementa métodos básicos a todos os repositórios """ def __init__(self, entity=None): self.context = Context() if entity: self.entity = entity else: self.entity = Document() self.entity.set_db(self.context.db) def create(self): self.entity.save() return self.entity.id def update(self): doc = self.entity.get(self.entity.id, db=self.context.db) for g in self.entity.items(): doc[g[0]] = g[1] doc.save() def remove(self, _id=None): if _id: self.entity = self.get_by_id(_id, Post) self.context.db.delete_doc(self.entity) #TODO: verificar se self.entity retorna o nome da entidade def list(self, amount): # colocar paginação aqui return self.context.db.view('main/entities', self.entity) def get_by_id(self, _id, entity): assert entity.__class__ == Document.__class__ if type(_id) != unicode: raise NameError('O valor passado deve ser um unicode - Valor passado: ' + str(type(_id))) return entity.get(_id, db = self.context.db)
def __init__(self, application, request, **kwargs): if application.settings['db_user']: Document.set_db(application.couchdb) RequestHandler.__init__(self, application, request) self.output = kwargs.get('output','page') self.set_header("Content-Type", self.mime_type[self.output])