def put(self, id, data, ins, cb): if ins is False: get = self.get(id, lambda x: "") fullPathId = str(get["_id"] + "?rev=" + get["_rev"]) self.options["path"] = "/%s/%s" % (self.db, fullPathId) self.options["method"] = "PUT" data[self.field_key] = self.field_value return send(self.options, data, cb) elif ins is True: self.options["path"] = "/%s/%s" % (self.db, id) self.options["method"] = "PUT" data[self.field_key] = self.field_value return send(self.options, data, cb) else: raise CouchDBOff("ins must be a boolean not a ", type(ins).__name__, "FAIL")
def design(self, name, options_design, cb): if type(options_design).__name__ != "dict": raise CouchDBOff("Options must be a dictionary not a ", type(self.options).__name__, "WARNING") params = urllib.urlencode({p: json.dumps(options_design["params"][p]) for p in options_design["params"]}) if "type" in options_design: type_ = options_design["type"] else: type_ = "view" self.options["path"] = "/%s/_design/%s/_%s/%s?%s" % (self.db, name, type_, options_design["name"], params) self.options["method"] = "GET" return send(self.options, cb)
def exists_db(self, db): def cb(p): pass cb = cb if len(str(db).strip()) == 0: raise CouchDBOff("The name of the database does not must be empty", "", "WARNING") self.options["path"] = "/_all_dbs" self.options["method"] = "GET" bases = send(self.options, cb) if str(db).strip() in bases: return True else: return False
def create_db(self, name, cb): if len(str(name).strip()) == 0: raise CouchDBOff("The name of the database does not must be empty", "", "WARNING") self.options["method"] = "PUT" self.options["path"] = "/%s" % name return send(self.options, cb)
def delete(self, id, cb): get = self.get(id, lambda x: "") fullPathId = str(get["_id"] + "?rev=" + get["_rev"]) self.options["path"] = "/%s/%s" % (self.db, fullPathId) self.options["method"] = "DELETE" return send(self.options, cb)
def post(self, data, cb): self.options["method"] = "POST" self.options["path"] = "/%s" % (self.db) data[self.field_key] = self.field_value return send(self.options, data, cb)
def get(self, id, cb): self.options["path"] = "/%s/%s" % (self.db, id) self.options["method"] = "GET" return send(self.options, cb)