예제 #1
0
    def save_many(
        self,
        query,
        timestamp=None,
        comment=None,
        data=None,
        ip=None,
        author=None,
        action=None,
    ):
        timestamp = timestamp or datetime.datetime.utcnow()
        author = author or self.get_account_manager().get_user()
        ip = ip or web.ctx.get('ip', '127.0.0.1')

        p = writequery.SaveProcessor(self.store, author)

        items = p.process_many(query)
        if not items:
            return []

        changeset = self.store.save_many(
            items, timestamp, comment, data, ip, author and author.key, action=action
        )
        saved_docs = changeset.get('docs')

        result = [
            {"key": doc["key"], "revision": doc['revision']} for doc in saved_docs
        ]
        event_data = dict(
            comment=comment, query=query, result=result, changeset=changeset
        )
        self._fire_event("save_many", timestamp, ip, author and author.key, event_data)

        self._fire_triggers(saved_docs)
        return result
예제 #2
0
    def test_process_value(self):
        def property(expected_type, unique=True, name='foo'):
            return web.storage(expected_type=web.storage(key=expected_type, kind='regular'), unique=unique, name=name)

        p = writequery.SaveProcessor(site.store, None)
        assert p.process_value(1, property('/type/int')) == 1
        assert p.process_value('1', property('/type/int')) == 1
        assert p.process_value(['1', '2'], property('/type/int', unique=False)) == [1, 2]
예제 #3
0
    def save(
        self,
        key,
        doc,
        timestamp=None,
        comment=None,
        data=None,
        ip=None,
        author=None,
        action=None,
    ):
        timestamp = timestamp or datetime.datetime.utcnow()
        author = author or self.get_account_manager().get_user()
        ip = ip or web.ctx.get('ip', '127.0.0.1')

        # @@ why to have key argument at all?
        doc['key'] = key

        p = writequery.SaveProcessor(self.store, author)
        doc = p.process(key, doc)

        if not doc:
            return {}
        else:
            changeset = self.store.save(
                key,
                doc,
                timestamp,
                comment,
                data,
                ip,
                author and author.key,
                action=action,
            )
            saved_docs = changeset.get("docs")
            saved_doc = saved_docs[0]
            result = {
                "key": saved_doc['key'],
                "revision": saved_doc['revision']
            }

            event_data = dict(comment=comment,
                              key=key,
                              query=doc,
                              result=result,
                              changeset=changeset)
            self._fire_event("save", timestamp, ip, author and author.key,
                             event_data)
            self._fire_triggers([saved_doc])
            return result