예제 #1
0
    def test_stack_can_produce_snapshot_of_future_revision_of_insert_type(
            self):
        """Test that the stack can create a future state of a new yet to be created document"""

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)

        fixture = self.test_fixture

        yield stack.push(self.test_fixture, self.three_min_past_now)

        fixture["baz"] = "bop"
        fixture["new_persistent"] = True
        yield stack.push(fixture, self.two_min_past_now)

        del fixture["new_persistent"]
        fixture["baz"] = "bit"
        id = yield stack.push(fixture, self.one_min_past_now)

        response = yield stack.preview(id)
        snapshot = response.get("snapshot")

        self.assertIsInstance(snapshot, dict)
        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("baz"), "bit")
예제 #2
0
    def test_stack_can_produce_snapshot_of_future_revision_of_insert_type(self):
        """Test that the stack can create a future state of a new yet to be created document"""

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings)

        fixture = self.test_fixture

        yield stack.push(self.test_fixture, self.three_min_past_now)

        fixture["baz"] = "bop"
        fixture["new_persistent"] = True
        yield stack.push(fixture, self.two_min_past_now)

        del fixture["new_persistent"]
        fixture["baz"] = "bit"
        id = yield stack.push(fixture, self.one_min_past_now)


        response = yield stack.preview(id)
        snapshot = response.get("snapshot")

        self.assertIsInstance(snapshot, dict)
        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("baz"), "bit")
예제 #3
0
    def test_stack_can_produce_snapshot_of_future_revision_of_update_type(
            self):
        """Test that the stack can create a future state of a document"""
        master_id = yield self.collection.insert(self.test_fixture)

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture",
                                                      settings,
                                                      master_id=master_id)

        update = {test_attr: test_val}

        yield stack.push(update, self.three_min_past_now)

        update["new_persistent"] = True
        yield stack.push(update, self.two_min_past_now)

        update["foo"] = "baz"
        id = yield stack.push(update, self.one_min_from_now)

        response = yield stack.preview(id)
        snapshot = response.get("snapshot")
        self.assertIsInstance(snapshot, dict)

        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("foo"), "baz")
예제 #4
0
    def test_stack_can_produce_snapshot_of_future_revision_of_update_type(self):
        """Test that the stack can create a future state of a document"""
        master_id = yield self.collection.insert(self.test_fixture)

        stack = AsyncSchedulableDocumentRevisionStack("test_fixture", settings, master_id=master_id)

        update = {
            test_attr: test_val
        }

        yield stack.push(update, self.three_min_past_now)

        update["new_persistent"] = True
        yield stack.push(update, self.two_min_past_now)

        update["foo"] = "baz"
        id = yield stack.push(update, self.one_min_from_now)

        response = yield stack.preview(id)
        snapshot = response.get("snapshot")
        self.assertIsInstance(snapshot, dict)

        self.assertEqual(snapshot.get("bool_val"), True)
        self.assertEqual(snapshot.get("new_persistent"), True)
        self.assertEqual(snapshot.get("foo"), "baz")
예제 #5
0
    def post(self, id=None):
        """
        Create a new object resource

        :json: Object to create
        :returns: json string representation
        :rtype: JSON

        """
        try:

            try:
                base_object = json_util.loads(self.request.body)
            except TypeError:
                base_object = json_util.loads(self.request.body.decode())

            #assert not hasattr(base_object, "_id")

            toa = self.request.headers.get("Caesium-TOA", None)

            if toa:
                # Async create flow
                stack = AsyncSchedulableDocumentRevisionStack(
                    self.client.collection_name, self.settings)

                revision_id = yield stack.push(base_object,
                                               toa=int(toa),
                                               meta=self._get_meta_data())
                resource = yield stack.preview(revision_id)

                if isinstance(revision_id, str):
                    self.set_header("Caesium-TOA", toa)
                    self.return_resource(resource.get("snapshot"))
                else:
                    self.raise_error(
                        404, "Revision not scheduled for object: %s" % id)

            else:

                id = yield self.client.insert(base_object)
                base_object = yield self.client.find_one_by_id(id)

                self.return_resource(base_object)

        except ValidationError as vex:
            self.logger.error("%s validation error" % self.object_name, vex)
            self.raise_error(
                400,
                "Your %s cannot be created because it is missing required fields, see docs"
                % self.object_name)
        except ValueError as ex:
            self.raise_error(400,
                             "Invalid JSON Body, check formatting. %s" % ex[0])
        except Exception as ex:
            self.logger.error(ex)
            self.raise_error()
예제 #6
0
파일: handler.py 프로젝트: urbn/Caesium
    def post(self, id=None):
        """
        Create a new object resource

        :json: Object to create
        :returns: json string representation
        :rtype: JSON

        """
        try:

            try:
                base_object = json_util.loads(self.request.body)
            except TypeError:
                base_object = json_util.loads(self.request.body.decode())

            #assert not hasattr(base_object, "_id")

            toa = self.request.headers.get("Caesium-TOA", None)

            if toa:
                # Async create flow
                stack = AsyncSchedulableDocumentRevisionStack(self.client.collection_name, self.settings)

                revision_id = yield stack.push(base_object, toa=int(toa), meta=self._get_meta_data())
                resource = yield stack.preview(revision_id)

                if isinstance(revision_id, str):
                    self.set_header("Caesium-TOA", toa)
                    self.return_resource(resource.get("snapshot"))
                else:
                    self.raise_error(404, "Revision not scheduled for object: %s" % id)

            else:

                id = yield self.client.insert(base_object)
                base_object = yield self.client.find_one_by_id(id)

                self.return_resource(base_object)

        except ValidationError as vex:
            self.logger.error("%s validation error" % self.object_name, vex)
            self.raise_error(400, "Your %s cannot be created because it is missing required fields, see docs" % self.object_name)
        except ValueError as ex:
            self.raise_error(400, "Invalid JSON Body, check formatting. %s" % ex[0])
        except Exception as ex:
            self.logger.error(ex)
            self.raise_error()
예제 #7
0
class RevisionHandler(BaseRestfulMotorHandler):
    def initialize(self):
        """Initializer for the Search Handler"""
        super(self.__class__, self).initialize()
        self.client = None

    @coroutine
    def put(self, id):
        """
        Update a revision by ID

        :param id: BSON id
        :return:
        """

        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).put(id)

    @coroutine
    def delete(self, id):
        """
        Delete a revision by ID

        :param id: BSON id
        :return:
        """

        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).delete(id)

    @coroutine
    def post(self, id=None):
        """
        Create a revision manually without the stack

        :param id: BSON id
        :return: JSON
        """
        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).post(id)

    @coroutine
    def get(self, id):
        """
        Get revision based on the stack preview algorithm

        :param id: BSON id
        :return: JSON
        """
        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name for stack")

        self.stack = AsyncSchedulableDocumentRevisionStack(
            collection_name, self.settings)

        revision = yield self.stack.preview(id)
        self.write(revision)
예제 #8
0
파일: handler.py 프로젝트: urbn/Caesium
class RevisionHandler(BaseRestfulMotorHandler):

    def initialize(self):
        """Initializer for the Search Handler"""
        super(self.__class__, self).initialize()
        self.client = None

    @coroutine
    def put(self, id):
        """
        Update a revision by ID

        :param id: BSON id
        :return:
        """

        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).put(id)

    @coroutine
    def delete(self, id):
        """
        Delete a revision by ID

        :param id: BSON id
        :return:
        """

        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).delete(id)

    @coroutine
    def post(self, id=None):
        """
        Create a revision manually without the stack

        :param id: BSON id
        :return: JSON
        """
        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name header")

        self.client = BaseAsyncMotorDocument("%s_revisions" % collection_name)

        super(self.__class__, self).post(id)


    @coroutine
    def get(self, id):
        """
        Get revision based on the stack preview algorithm

        :param id: BSON id
        :return: JSON
        """
        collection_name = self.request.headers.get("collection")

        if not collection_name:
            self.raise_error(400, "Missing a collection name for stack")

        self.stack = AsyncSchedulableDocumentRevisionStack(collection_name, self.settings)

        revision = yield self.stack.preview(id)
        self.write(revision)