New attachment handling in couchdb-python module

Tuesday, September 09, 2008

I've been trying out the new methods for the Database class in couchdb-python, in the svn repository.

I tried put_attachment.

Guess you already have a couchdb database called blog. To get a reference to the db you have to...


from couchdb.schema import *


class Post(Document):
author = TextField()
subject = TextField()
content = TextField()
tags = ListField( TextField() )
comments = ListField( DictField(Schema.build(
comment_author = TextField(),
comment = TextField(),
comment_date = DateTimeField()
)))
date = DateTimeField()


from couchdb import Server
from datetime import datetime
import binascii
s = Server("http://127.0.0.1:5984/")
s.create("blog")
blog = s["blog"]
p = Post( author = "Me", subject = "Whatever for the subject ", content = "Any content",date = datetime.now(), tags = ["Python", "Couchdb", "Blog"])

p.store(blog)

f = open("apythonfile.py", "rb")
foo = binascii.b2a_base64(f.read()) # this convert the content of the file to a encode 64 string

#put_attachment only works right now with an encoded string as I see and with a dictionary object for the document ( first parameter ) and not for a Document instance as used here , so...


blog = s["blog"]
adoc = blog[p.id]

and finally call the method...

blog.put_attachment( adoc, "apythonfile.py", foo, "text/python")

#you can't do blog.put_attachment( p, "apythonfile.py", f, "text/python") which I taught by reading the doc inside the method.

1 comments: to “ New attachment handling in couchdb-python module so far...

  •  

    Heya, passing in f works fr me[tm]. I added a test to the suite to verify. Please report bugs to http://code.google.com/p/couchdb-python/issues/list

    Cheers
    Jan
    --