def save(self, docs, timestamp, comment, ip, author, action, data=None): docs = list(docs) docs = common.format_data(docs) if not docs: return {} dbtx = self.db.transaction() try: records = self._get_records_for_save(docs, timestamp) self._update_thing_table(records) changes = [dict(key=r.key, revision=r.revision) for r in records] bot = bool(author and (self.get_user_details(author) or {}).get('bot', False)) # add transaction changeset = dict( kind=action, author=author and {"key": author}, ip=ip, comment=comment, timestamp=timestamp.isoformat(), bot=bot, changes=changes, data=data or {}, ) tx_id = self._add_transaction(changeset) changeset['id'] = str(tx_id) # add versions versions = [ dict(thing_id=r.id, revision=r.revision, transaction_id=tx_id) for r in records ] self.db.multiple_insert('version', versions, seqname=False) # add data data = [ dict(thing_id=r.id, revision=r.revision, data=simplejson.dumps(r.data)) for r in records ] self.db.multiple_insert('data', data, seqname=False) self._update_index(records) except: dbtx.rollback() raise else: dbtx.commit() changeset['docs'] = [r.data for r in records] changeset['old_docs'] = [r.prev.data for r in records] return changeset
def save(self, docs, timestamp, comment, ip, author, action, data=None): docs = list(docs) docs = common.format_data(docs) if not docs: return {} dbtx = self.db.transaction() try: records = self._get_records_for_save(docs, timestamp) self._update_thing_table(records) changes = [dict(key=r.key, revision=r.revision) for r in records] bot = bool(author and (self.get_user_details(author) or {}).get('bot', False)) # add transaction changeset = dict( kind=action, author=author and {"key": author}, ip=ip, comment=comment, timestamp=timestamp.isoformat(), bot=bot, changes=changes, data=data or {}, ) tx_id = self._add_transaction(changeset) changeset['id'] = str(tx_id) # add versions versions = [dict(thing_id=r.id, revision=r.revision, transaction_id=tx_id) for r in records] self.db.multiple_insert('version', versions, seqname=False) # add data data = [dict(thing_id=r.id, revision=r.revision, data=simplejson.dumps(r.data)) for r in records] self.db.multiple_insert('data', data, seqname=False) self._update_index(records) except: dbtx.rollback() raise else: dbtx.commit() changeset['docs'] = [r.data for r in records] changeset['old_docs'] = [r.prev.data for r in records] return changeset
def save_many(self, docs, timestamp, comment, data, ip, author, action=None): docs = list(docs) action = action or "bulk_update" logger.debug( "saving %d docs - %s", len(docs), dict( timestamp=timestamp, comment=comment, data=data, ip=ip, author=author, action=action, ), ) s = SaveImpl(self.db, self.schema, self.indexer, self.property_manager) # Hack to allow processing of json data before using. Required for OL legacy. s.process_json = process_json docs = common.format_data(docs) changeset = s.save( docs, timestamp=timestamp, comment=comment, ip=ip, author=author, action=action, data=data, ) # update cache. # Use the docs from result as they contain the updated revision and last_modified fields. for doc in changeset.get('docs', []): web.ctx.new_objects[doc['key']] = json.dumps(doc) return changeset
def format_data(self): from infogami.infobase import common return common.format_data(self._get_data())