Example #1
0
    def contribute_to_class(self, cls, name, *args, **kwargs):
        @classproperty
        def river(_self):
            return RiverObject(_self)

        self.field_name = name

        self._add_to_class(
            cls, self.field_name + "_transition_approvals",
            GenericRelation('%s.%s' % (TransitionApproval._meta.app_label,
                                       TransitionApproval._meta.object_name)))
        self._add_to_class(
            cls, self.field_name + "_transitions",
            GenericRelation(
                '%s.%s' %
                (Transition._meta.app_label, Transition._meta.object_name)))

        if id(cls) not in workflow_registry.workflows:
            self._add_to_class(cls, "river", river)

        super(StateField, self).contribute_to_class(cls, name, *args, **kwargs)

        if id(cls) not in workflow_registry.workflows:
            post_save.connect(_on_workflow_object_saved,
                              self.model,
                              False,
                              dispatch_uid='%s_%s_riverstatefield_post' %
                              (self.model, name))
            post_delete.connect(_on_workflow_object_deleted,
                                self.model,
                                False,
                                dispatch_uid='%s_%s_riverstatefield_post' %
                                (self.model, name))

        workflow_registry.add(self.field_name, cls)
Example #2
0
    def test__shouldReturnListOfStateFieldsWhenThereIsModelWithStateField(
            self):
        content_type = ContentType.objects.first()

        from river.core.workflowregistry import workflow_registry
        workflow_registry.add("test-field-1", content_type.model_class())
        workflow_registry.add("test-field-2", content_type.model_class())

        response = self.client.get('/workflow/state-field/list/')
        assert_that(response.status_code, equal_to(HTTP_200_OK))
        assert_that(response.data, has_length(2))

        assert_that(
            response.data,
            has_item(
                all_of(
                    has_entry("field_name", equal_to("test-field-1")),
                    has_entry(
                        "content_type",
                        all_of(
                            has_entry("id", equal_to(content_type.id)),
                            has_entry("app_label",
                                      equal_to(content_type.app_label)),
                            has_entry("model", equal_to(content_type.model)),
                        )))))

        assert_that(
            response.data,
            has_item(
                all_of(
                    has_entry("field_name", equal_to("test-field-2")),
                    has_entry(
                        "content_type",
                        all_of(
                            has_entry("id", equal_to(content_type.id)),
                            has_entry("app_label",
                                      equal_to(content_type.app_label)),
                            has_entry("model", equal_to(content_type.model)),
                        )))))