From 87bb1647cc12ac8dd389ac5aa194a815610f61a9 Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Tue, 16 Dec 2014 13:45:06 +0100 Subject: [PATCH 1/7] Changed request() to create json-compatible strings --- wallaby/backends/couchdb/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wallaby/backends/couchdb/__init__.py b/wallaby/backends/couchdb/__init__.py index 700d199..e4d6ab8 100644 --- a/wallaby/backends/couchdb/__init__.py +++ b/wallaby/backends/couchdb/__init__.py @@ -258,14 +258,13 @@ def _request(self, d, method, path, body, headers, protocol, keepOnTrying=False, else: if isinstance(v, bool): if v: - kv[k] = 'true' + kv[k] = json.dumps('true') else: - kv[k] = 'false' + kv[k] = json.dumps('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( From 5e70652684a09e38f66c55293231bbf07eb9fc86 Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Tue, 4 Aug 2015 11:29:33 +0200 Subject: [PATCH 2/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb4c51a..a094798 100644 --- a/README.md +++ b/README.md @@ -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') From 9d72c30b40e387b58a0810383af20ab2df4bf935 Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Fri, 4 Nov 2016 11:12:20 +0100 Subject: [PATCH 3/7] Added directly getting documents including attachments --- wallaby/backends/couchdb/__init__.py | 35 ++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/wallaby/backends/couchdb/__init__.py b/wallaby/backends/couchdb/__init__.py index e4d6ab8..f54d6d0 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 @@ -285,7 +285,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): @@ -345,6 +345,27 @@ def _get(self, id, d, rev=None): d.callback(response) + 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_with_attachments(self, id, d, rev=None): + urlpath = urllib.quote(id, "") + "?attachments=true" + if rev: + response = yield self.request('GET', path=urlpath, rev=rev) + else: + response = yield self.request('GET', path=urlpath, 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) @@ -481,7 +502,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: From 3d6055642f3e8fee732b38b9890accb6236ac921 Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Mon, 7 Nov 2016 10:46:20 +0100 Subject: [PATCH 4/7] Implemented getting docs with attachment --- wallaby/backends/couchdb/__init__.py | 35 ++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/wallaby/backends/couchdb/__init__.py b/wallaby/backends/couchdb/__init__.py index e4d6ab8..d657899 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 @@ -285,7 +285,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): @@ -332,6 +332,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): @@ -345,6 +353,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) @@ -481,7 +502,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: From a0cb5381684de25bfaaad00da66565810296413a Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Mon, 7 Nov 2016 16:09:10 +0100 Subject: [PATCH 5/7] get_with_attachments added --- wallaby/backends/couchdb/__init__.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/wallaby/backends/couchdb/__init__.py b/wallaby/backends/couchdb/__init__.py index d657899..d442f4f 100644 --- a/wallaby/backends/couchdb/__init__.py +++ b/wallaby/backends/couchdb/__init__.py @@ -253,17 +253,7 @@ 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) - else: - if isinstance(v, bool): - if v: - kv[k] = json.dumps('true') - else: - kv[k] = json.dumps('false') - else: - kv[k] = json.dumps(v) - + kv[k] = json.dumps(v) url += '?'+urllib.urlencode(kv) try: # print "REQUEST", method, str(url), body From be96fbae099afd257db62342a42031883db3e0cf Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Fri, 3 Feb 2017 15:19:34 +0100 Subject: [PATCH 6/7] Bug fix put_attachment --- wallaby/backends/couchdb/__init__.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/wallaby/backends/couchdb/__init__.py b/wallaby/backends/couchdb/__init__.py index d442f4f..5e4a9fc 100644 --- a/wallaby/backends/couchdb/__init__.py +++ b/wallaby/backends/couchdb/__init__.py @@ -253,20 +253,18 @@ def _request(self, d, method, path, body, headers, protocol, keepOnTrying=False, kv = dict() # print ka for k, v in ka.items(): - kv[k] = json.dumps(v) + if k == 'rev': + kv[k] = v + else: + 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: From b5d33d17a8abf99c85fef677dc56a8512a138987 Mon Sep 17 00:00:00 2001 From: Sven Mesecke Date: Fri, 10 Feb 2017 11:31:24 +0100 Subject: [PATCH 7/7] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a094798..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