示例#1
0
async def test_modified_event_gathers_all_index_data(dummy_guillotina):
    container = await create_content("Container", id="guillotina", title="Guillotina")
    container.__name__ = "guillotina"
    task_vars.container.set(container)
    ob = await create_content("Item", id="foobar")
    ob.__uuid__ = "foobar"
    await notify(ObjectModifiedEvent(ob, payload={"title": "", "id": ""}))
    fut = index.get_indexer()

    assert len(fut.update["foobar"]) == 9

    await notify(ObjectModifiedEvent(ob, payload={"creation_date": ""}))
    assert "modification_date" in fut.update["foobar"]
    assert len(fut.update["foobar"]) == 10
示例#2
0
async def test_modified_event_gathers_all_index_data(dummy_guillotina):
    container = await create_content('Container',
                                     id='guillotina',
                                     title='Guillotina')
    container.__name__ = 'guillotina'
    task_vars.container.set(container)
    ob = await create_content('Item', id='foobar')
    ob.__uuid__ = 'foobar'
    await notify(ObjectModifiedEvent(ob, payload={'title': '', 'id': ''}))
    fut = index.get_indexer()

    assert len(fut.update['foobar']) == 8

    await notify(ObjectModifiedEvent(ob, payload={'creation_date': ''}))
    assert len(fut.update['foobar']) == 9
示例#3
0
    async def __call__(self):
        data = await self.get_data()

        behaviors = data.get("@behaviors", None)
        for behavior in behaviors or ():
            try:
                self.context.add_behavior(behavior)
            except (TypeError, ComponentLookupError):
                return HTTPPreconditionFailed(
                    content={"message": f"{behavior} is not a valid behavior", "behavior": behavior}
                )

        deserializer = query_multi_adapter((self.context, self.request), IResourceDeserializeFromJson)
        if deserializer is None:
            raise ErrorResponse(
                "DeserializationError",
                "Cannot deserialize type {}".format(self.context.type_name),
                status=412,
                reason=error_reasons.DESERIALIZATION_FAILED,
            )

        await notify(BeforeObjectModifiedEvent(self.context, payload=data))

        await deserializer(data)

        await notify(ObjectModifiedEvent(self.context, payload=data))

        return Response(status=204)
示例#4
0
async def test_modified_event_gathers_all_index_data(dummy_request):
    request = dummy_request  # noqa
    container = await create_content('Container',
                                     id='guillotina',
                                     title='Guillotina')
    container.__name__ = 'guillotina'
    request.container = container
    ob = await create_content('Item', id='foobar')
    ob._p_oid = 'foobar'
    await notify(ObjectModifiedEvent(ob, payload={'title': '', 'id': ''}))
    fut = index.get_future()

    assert len(fut.update['foobar']) == 5

    await notify(ObjectModifiedEvent(ob, payload={'creation_date': ''}))
    assert len(fut.update['foobar']) == 6
示例#5
0
    async def __call__(self):
        data = await self.get_data()

        if 'id' in data and data['id'] != self.context.id:
            return ErrorResponse('DeserializationError',
                                 'Not allowed to change id of content.',
                                 status=412)

        behaviors = data.get('@behaviors', None)
        for behavior in behaviors or ():
            self.context.add_behavior(behavior)

        deserializer = query_multi_adapter((self.context, self.request),
                                           IResourceDeserializeFromJson)
        if deserializer is None:
            return ErrorResponse('DeserializationError',
                                 'Cannot deserialize type {}'.format(
                                     self.context.type_name),
                                 status=412)

        try:
            await deserializer(data)
        except DeserializationError as e:
            return ErrorResponse('DeserializationError', str(e), status=422)

        await notify(ObjectModifiedEvent(self.context, payload=data))

        return Response(response='', status=204)
示例#6
0
async def deletefavorite(context, request):
    user = get_authenticated_user_id()
    behavior = IFollowing(context)
    await behavior.load(True)
    users_list = behavior.favorites
    if users_list is None:
        behavior.favorites = []
        users_list = behavior.favorites
    if user in users_list:
        users_list.remove(user)
    behavior.data.register()
    await notify(ObjectModifiedEvent(context, payload={"favorites": ""}))
示例#7
0
async def addfavorite(context, request):
    user = get_authenticated_user_id(request)
    behavior = IFollowing(context)
    await behavior.load(True)
    users_list = behavior.favorites
    if not users_list:
        behavior.favorites = []
        users_list = behavior.favorites
    if user not in users_list:
        users_list.append(user)
    behavior.data._p_register()
    await notify(ObjectModifiedEvent(context, payload={'favorites': ''}))
示例#8
0
    async def __call__(self):
        data = await self.get_data()

        behaviors = data.get('@behaviors', None)
        for behavior in behaviors or ():
            self.context.add_behavior(behavior)

        deserializer = query_multi_adapter((self.context, self.request),
                                           IResourceDeserializeFromJson)
        if deserializer is None:
            raise ErrorResponse('DeserializationError',
                                'Cannot deserialize type {}'.format(
                                    self.context.type_name),
                                status=412,
                                reason=error_reasons.DESERIALIZATION_FAILED)

        await deserializer(data)

        await notify(ObjectModifiedEvent(self.context, payload=data))

        return Response(status=204)
示例#9
0
    async def __call__(self):
        data = await self.get_data()
        behaviors = data.get('@behaviors', None)
        for behavior in behaviors or ():
            self.context.add_behavior(behavior)

        deserializer = queryMultiAdapter((self.context, self.request),
                                         IResourceDeserializeFromJson)
        if deserializer is None:
            return ErrorResponse('DeserializationError',
                                 'Cannot deserialize type {}'.format(
                                     self.context.type_name),
                                 status=501)

        try:
            await deserializer(data)
        except DeserializationError as e:
            return ErrorResponse('DeserializationError', str(e), status=400)

        await notify(ObjectModifiedEvent(self.context, payload=data))

        return Response(response={}, status=204)
示例#10
0
        async def do_action(self, action, comments):
            available_actions = self.actions
            if action not in available_actions:
                raise HTTPPreconditionFailed(content={"reason": "Unavailable action"})

            action_def = available_actions[action]
            policy = get_security_policy()
            if "check_permission" in action_def and not policy.check_permission(
                action_def["check_permission"], self.context
            ):
                raise HTTPUnauthorized()

            # Change permission
            new_state = action_def["to"]

            if "set_permission" in self.states[new_state]:
                await apply_sharing(self.context, self.states[new_state]["set_permission"])

            # Write history
            user = get_authenticated_user_id()
            history = {
                "actor": user,
                "comments": comments,
                "time": datetime.datetime.now(),
                "title": action_def["title"],
                "type": "workflow",
                "data": {"action": action, "review_state": new_state},
            }

            workflow_behavior = IWorkflowBehavior(self.context)
            workflow_behavior.review_state = new_state

            workflow_behavior.history.append(history)
            workflow_behavior.register()

            await notify(WorkflowChangedEvent(self.context, self, action, comments))
            await notify(ObjectModifiedEvent(self.context, payload={"review_state": new_state}))
            return history
示例#11
0
    async def __call__(self, data, validate_all=False):

        modified = False
        errors = []

        factory = get_cached_factory(self.context.portal_type)
        main_schema = factory.schema
        await self.set_schema(main_schema, self.context, data, errors,
                              validate_all, False)

        for behavior_schema in factory.behaviors or ():
            if behavior_schema.__identifier__ in data:
                behavior = behavior_schema(self.context)
                if IAsyncBehavior.implementedBy(behavior.__class__):
                    # providedBy not working here?
                    await behavior.load(create=True)
                await self.set_schema(behavior_schema, behavior, data, errors,
                                      validate_all, True)

        for dynamic_behavior in self.context.__behaviors__ or ():
            dynamic_behavior_obj = BEHAVIOR_CACHE[dynamic_behavior]
            if dynamic_behavior_obj.__identifier__ in data:
                behavior = dynamic_behavior_obj(self.context)
                if IAsyncBehavior.implementedBy(
                        dynamic_behavior_obj.__class__):
                    # providedBy not working here?
                    await behavior.load(create=True)
                await self.set_schema(dynamic_behavior_obj, behavior, data,
                                      errors, validate_all, True)

        if errors:
            raise DeserializationError(errors)

        if modified:
            self.context._p_register()
            await notify(ObjectModifiedEvent(self.context, data))

        return self.context