Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions paisley/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CouchDB(object):
CouchDB client: hold methods for accessing a couchDB.
"""

def __init__(self, host, port=5984, dbName=None, username=None, password=None):
def __init__(self, host, port=5984, dbName=None,
username=None, password=None):
"""
Initialize the client for given host.

Expand All @@ -61,11 +62,17 @@ def __init__(self, host, port=5984, dbName=None, username=None, password=None):
@param dbName: if specified, all calls needing a database name will use
this one by default.
@type dbName: C{str}

@param username: username to use while connecting to couchdb
@type username: C{str}

@param password: password to use while connection to couchdb
@type password: C{str}
"""
self.host = host
self.port = int(port)
self.username = username
self.password =password
self.password = password
self.url_template = "http://%s:%s%%s" % (self.host, self.port)
if dbName is not None:
self.bindToDB(dbName)
Expand All @@ -84,7 +91,7 @@ def bindToDB(self, dbName):
"""
for methname in ["createDB", "deleteDB", "infoDB", "listDoc",
"openDoc", "saveDoc", "deleteDoc", "openView",
"tempView"]:
"tempView", "openList"]:
method = getattr(self, methname)
newMethod = partial(method, dbName)
setattr(self, methname, newMethod)
Expand Down Expand Up @@ -235,11 +242,11 @@ def deleteDoc(self, dbName, docId, revision):

# View operations

def openView(self, dbName, docId, viewId, **kwargs):
def openView(self, dbName, designId, viewId, **kwargs):
"""
Open a view of a document in a given database.
Open a view of a design document in a given database.
"""
uri = "/%s/_design/%s/_view/%s" % (dbName, quote(docId), viewId)
uri = "/%s/_design/%s/_view/%s" % (dbName, quote(designId), viewId)

for arg in kwargs.keys():
kwargs[arg] = json.dumps(kwargs[arg])
Expand All @@ -252,7 +259,7 @@ def openView(self, dbName, docId, viewId, **kwargs):
keys = {'keys': options.pop('keys')}
if options:
uri += "?%s" % (urlencode(options),)
return self.post(uri, body=keys)
return self.post(uri, body=keys).addCallback(self.parseResult)
elif kwargs:
# if not keys we just encode everything in the url
uri += "?%s" % (urlencode(kwargs),)
Expand Down Expand Up @@ -281,6 +288,33 @@ def tempView(self, dbName, view):
d = self.post("/%s/_temp_view" % (dbName,), view)
return d.addCallback(self.parseResult)

# List operations

def openList(self, dbName, designId, listId, viewId, **kwargs):
"""
Open a list for a design document in a given database.
"""

uri = "/%s/_design/%s/_list/%s/%s" % \
(dbName, quote(designId), quote(listId), viewId)

for arg in kwargs.keys():
kwargs[arg] = json.dumps(kwargs[arg])

if not kwargs:
return self.get(uri)
elif 'keys' in kwargs:
# couchdb is crazy and requires that keys be passed in a post body
options = kwargs.copy()
keys = {'keys': options.pop('keys')}
if options:
uri += "?%s" % (urlencode(options),)
return self.post(uri, body=keys)
elif kwargs:
# if not keys we just encode everything in the url
uri += "?%s" % (urlencode(kwargs),)
return self.get(uri)


# Basic http methods

Expand Down