# Path: couch/models/document.py
# class Document(db.Model):
#     id = db.StringProperty()
#     rev = db.StringProperty()
#     dbname = db.StringProperty()
#     docstring = db.TextProperty()
#     deleted = db.BooleanProperty(default = False)
# 
#     def to_dict(self):
#         return json.loads(self.docstring)
# 
# class DocumentRoot(db.Model):
#     ''' Controls document '''
#     revno = db.IntegerProperty(default = 0)
#     revsuffix = db.StringProperty()
#     deleted = db.BooleanProperty(default = False)
# 
#     def rev(self):
#         return '%s-%s' % (self.revno, self.revsuffix)


import time
import hashlib
from datetime import datetime
from google.appengine.ext import db
from django.utils import simplejson as json
from couch.models.document import Document, DocumentRoot
from couch.models.util import *
        return self.key().name()

    def _save(self, document):
        if document.has_key('_id'):
            id = document.get('_id')
            del document['_id']
        else:
            id = gen_uuid()[0]
        
        if document.has_key('_rev'):
            rev = document.get('_rev')
            del document['_rev']
        else:
            rev = None

        if document.has_key('_deleted'):
            deleted = document.get('_deleted') == True
            del document['_deleted']
        else:
            deleted = False

        docstring = json.dumps(document)
        return db.run_in_transaction(self._save_doc_transaction, id, rev, deleted, document, docstring)
    
    
    def _save_doc_transaction(self, id, rev, deleted, document, docstring):
        dbname = self.dbname
        # get current document
        docroot = DocumentRoot.get_by_key_name("%s/%s" % (dbname, id))
        if not docroot:

# Path: couch/models/document.py
# class Document(db.Model):
#     id = db.StringProperty()
#     rev = db.StringProperty()
#     dbname = db.StringProperty()
#     docstring = db.TextProperty()
#     deleted = db.BooleanProperty(default = False)
# 
#     def to_dict(self):
#         return json.loads(self.docstring)
# 
# class DocumentRoot(db.Model):
#     ''' Controls document '''
#     revno = db.IntegerProperty(default = 0)
#     revsuffix = db.StringProperty()
#     deleted = db.BooleanProperty(default = False)
# 
#     def rev(self):
#         return '%s-%s' % (self.revno, self.revsuffix)


# Path: couch/models/database.py
import time
import hashlib
from datetime import datetime
from google.appengine.ext import db
from django.utils import simplejson as json
from couch.models.document import Document, DocumentRoot
from couch.models.util import *

        return self.key().name()

    def _save(self, document):
        if document.has_key('_id'):
            id = document.get('_id')
            del document['_id']
        else:
            id = gen_uuid()[0]
        
        if document.has_key('_rev'):
            rev = document.get('_rev')
            del document['_rev']
        else:
            rev = None

        if document.has_key('_deleted'):
            deleted = document.get('_deleted') == True
            del document['_deleted']
        else:
            deleted = False

        docstring = json.dumps(document)
        return db.run_in_transaction(self._save_doc_transaction, id, rev, deleted, document, docstring)
    
    
    def _save_doc_transaction(self, id, rev, deleted, document, docstring):
        dbname = self.dbname
        # get current document
        docroot = DocumentRoot.get_by_key_name("%s/%s" % (dbname, id))
        if not docroot:

            docroot = DocumentRoot(key_name = "%s/%s" % (dbname, id))