diff --git a/README.md b/README.md index eb4c51a..ca74a9f 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ For more information on wallaby visit http://wlby.freshx.de Installation ============ -You can install the couchdb backend with pip +You can install this bug-fixed couchdb backend with pip using ```bash -pip install wallaby-backend-couchdb +pip install git+https://github.com/sveme/wallaby-backend-couchdb.git ``` How to use @@ -123,7 +123,7 @@ Attachment handling content = open('test.png').read() # Now we can attach this file to an existing document - res = yield db.put_attachment(doc, 'newimage.png', content, content-type='image/png') + res = yield db.put_attachment(doc, 'newimage.png', content, contentType='image/png') # And load from database again content = yield db.get_attachment(doc, 'newimage.png') diff --git a/wallaby/backends/couchdb/__init__.py b/wallaby/backends/couchdb/__init__.py index 700d199..5e4a9fc 100644 --- a/wallaby/backends/couchdb/__init__.py +++ b/wallaby/backends/couchdb/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) by it's authors. +# Copyright (c) by it's authors. # Some rights reserved. See LICENSE, AUTHORS. from twisted.web.client import Agent @@ -43,7 +43,7 @@ def parseData(self): try: obj = json.loads(msg) - if 'error' in obj: + if 'error' in obj: # Reconnect self.transport.stopProducing() return @@ -187,7 +187,7 @@ def name(self): def proto(self): if not self._url: return None - + m = re.match(r'(^.*?)://(.*?):(.*?)', self._url) if m == None: return None @@ -195,7 +195,7 @@ def proto(self): def port(self): if not self._url: return None - + m = re.match(r'(^.*?)://(.*?):(.*?)', self._url) if m == None: return None @@ -203,7 +203,7 @@ def port(self): def host(self): if not self._url: return None - + m = re.match(r'(^.*?)://(.*?):(.*?)', self._url) if m == None: return None @@ -253,31 +253,18 @@ def _request(self, d, method, path, body, headers, protocol, keepOnTrying=False, kv = dict() # print ka for k, v in ka.items(): - if not (isinstance(v, str) or isinstance(v, unicode) or isinstance(v, int) or isinstance(v, bool)): - kv[k] = json.dumps(v) + if k == 'rev': + kv[k] = v else: - if isinstance(v, bool): - if v: - kv[k] = 'true' - else: - kv[k] = 'false' - else: - kv[k] = v - + kv[k] = json.dumps(v) url += '?'+urllib.urlencode(kv) - try: - # print "REQUEST", method, str(url), body - response = yield self._agent.request( - method, - str(url), - Headers(headers), - body) + #print "REQUEST", method, str(url), body, headers, Headers(headers) + response = yield self._agent.request(method, str(url), headers=Headers(headers), bodyProducer=body) responseDeferred = defer.Deferred() response.deliverBody(protocol(responseDeferred, response.length)) responseData = yield responseDeferred - # print responseData d.callback(responseData) except (Exception,Failure) as e: @@ -286,7 +273,7 @@ def _request(self, d, method, path, body, headers, protocol, keepOnTrying=False, reactor.callLater(1, self._request, d, method, path, body, headers, protocol, **ka) elif returnOnError: d.errback(e) - else: + else: self._failedRequests.append((d, method, path, body, headers, protocol, ka)) def connectionEstablished(self): @@ -333,6 +320,14 @@ def get(self, id, rev=None): return d + def get_with_attachments(self, id, rev=None): + d = defer.Deferred() + + from twisted.internet import reactor + reactor.callLater(0, self._get_with_attachments, id, d, rev=rev) + + return d + @defer.inlineCallbacks def _get(self, id, d, rev=None): @@ -346,6 +341,19 @@ def _get(self, id, d, rev=None): d.callback(response) + @defer.inlineCallbacks + def _get_with_attachments(self, id, d, rev=None): + + if rev: + response = yield self.request('GET', path=urllib.quote(id, ""), attachments=True, rev=rev) + else: + response = yield self.request('GET', path=urllib.quote(id, ""), attachments=True, conflicts=True) + + if '_id' not in response or 'error' in response: + response = None + + d.callback(response) + def info(self, **ka): return self.request('GET', **ka) @@ -482,7 +490,7 @@ def removeCallbacks(self, __id, close=True): cb(None, viewID=__id) del self._changesCBs[__id] - del self._changesRunning[__id] + del self._changesRunning[__id] del self._lastSeq[__id] if self._changesProtocols[__id] is not None and close: