Skip to content

Commit

Permalink
Document: Further reduce unnecessary HTTP queries when handling attac…
Browse files Browse the repository at this point in the history
…hments
  • Loading branch information
nailor authored and Jyrki Pulliainen committed Aug 24, 2010
1 parent 72fe5fb commit e944d8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
26 changes: 26 additions & 0 deletions test/test_client.py
Expand Up @@ -600,6 +600,32 @@ def data_callback(data):
s.create('testdb', callback=create_db_callback)
ioloop.start()

@with_ioloop
@with_couchdb
def test_load_inline_attachment_no_fetch(baseurl, ioloop):
def create_db_callback(db):
db.set(
{'testvalue': 'something'},
callback=attach_callback,
doc_id='testid',
attachments={'foobar': (None, 'some textual data')},
)

def attach_callback(doc):
def _broken_fetch(*a, **kw):
assert False, 'Fetch called when not needed!'

doc.db._fetch = _broken_fetch
doc.load_attachment('foobar', callback=data_callback)

def data_callback(data):
eq(data, 'some textual data')
ioloop.stop()

s = trombi.Server(baseurl, io_loop=ioloop)
s.create('testdb', callback=create_db_callback)
ioloop.start()


@with_ioloop
@with_couchdb
Expand Down
13 changes: 8 additions & 5 deletions trombi/client.py
Expand Up @@ -10,7 +10,7 @@
# Asynchronous CouchDB client
import re
import urllib
from base64 import b64encode
from base64 import b64encode, b64decode
from tornado.httpclient import AsyncHTTPClient
try:
import json
Expand Down Expand Up @@ -341,10 +341,13 @@ def load_attachment(self, name, callback):
def _really_callback(response):
callback(response.body)

self.db._fetch(
'%s/%s' % (self.id, name),
_really_callback,
)
if hasattr(self, 'attachments') and name in self.attachments:
callback(b64decode(self.attachments[name]['data']))
else:
self.db._fetch(
'%s/%s' % (self.id, name),
_really_callback,
)

def delete_attachment(self, name, callback):
def _really_callback(response):
Expand Down

0 comments on commit e944d8b

Please sign in to comment.