#!/usr/bin/env python

import os, sys, datetime, getpass
from couchdbkit import *

path = '/tmp/motion'

# model
class MotionCouchdb(Document):
        author = StringProperty()
        content = StringProperty()
        date = DateTimeProperty()

# server object
server = Server('http://127.0.0.1:5984/')

# create database
db = server.get_or_create_db("motion_couchdb")

# associate MotionCouchdb to the db
MotionCouchdb.set_db(db)

# create a new motion
motion = MotionCouchdb(
    author=getpass.getuser(),
    content="suck video",
    date=datetime.datetime.utcnow()
)

# save it.
motion.save()

# save files
files = os.listdir(path)
file = [f for f in files if f.endswith('.jpg')]
for f in file:
    read_image = open('%s/%s' % (path, f), 'r')
    motion.put_attachment(read_image, f)