def get_changelog(self, when=None, db=None): """Return a iterable of the form: (time, author, key, oldvalue, newvalue) """ db = db or self.env.get_db_cnx() cursor = db.cursor() when_ts = when and to_timestamp(when) or 0 if when_ts: cursor.execute('SELECT time, author, key, oldvalue, newvalue ' 'FROM boxdb_changes ' 'WHERE document=%s AND time=%s' 'ORDER BY time', (self.name, when_ts)) else: cursor.execute('SELECT time, author, key, oldvalue, newvalue ' 'FROM boxdb_changes ' 'WHERE document=%s' 'ORDER BY time', (self.name,)) for t, author, key, oldvalue, newvalue in cursor: t = datetime.fromtimestamp(int(t), utc) if oldvalue: oldvalue = simplejson.loads(oldvalue) else: oldvalue = None if newvalue: newvalue = simplejson.loads(newvalue) else: newvalue = None yield t, author, key, oldvalue, newvalue
def _fetch(self, name, db=None): """Retrieve the raw data for a given document.""" db = db or self.env.get_db_cnx() cursor = db.cursor() type = self._get_db(cursor, name, '__type__') if type: # Fetch and decode collection type = simplejson.loads(type) if type == self.name: raise ValueError('Document cannot be its own type') self.type = type # Process inheritance inherit = self._get_db(cursor, name, '__inherit__') if inherit: inherit = simplejson.loads(inherit) self._fetch(inherit, db) cursor.execute('SELECT key, value FROM boxdb WHERE name=%s', (name,)) for key, value in cursor: self[key] = value
def __init__(self, env, name, db=None): super(Document, self).__init__() self.env = env self.name = name self._old_data = {} if self.name == 'schema': self._bootstrap() else: self._fetch(self.name, db) if not self: self.type = None # Document doesn't exist, return a blank return self._old_data.update(self) # Decode the document self.type = Document(self.env, self.type, db) for key, value in self.iteritems(): self[key] = simplejson.loads(value)