Exemple #1
0
    def __init__(self, *args, **kwargs):
        # ensure 'title' prevails over 'name'
        if 'title' in kwargs and 'name' in kwargs:
            title = kwargs.get('title')
            name = kwargs.get('name')

            if title is None:
                del kwargs['title']
            elif name != title:
                kwargs['name'] = title

        Entity.__init__(self, *args, **kwargs)
Exemple #2
0
    def __init__(self, *args, **kwargs):
        # ensure 'title' prevails over 'name'
        if "title" in kwargs and "name" in kwargs:
            title = kwargs.get("title")
            name = kwargs.get("name")

            if title is None:
                del kwargs["title"]
            elif name != title:
                kwargs["name"] = title

        Entity.__init__(self, *args, **kwargs)
Exemple #3
0
    def __init__(self, **kw):
        self.has_documents = True
        self.membership_count = 0
        self.document_count = 0
        self.members_can_send_by_email = False
        Entity.__init__(self, **kw)
        if self.has_documents and not self.folder:
            #FIXME: this should be done in documents by using signals
            name = self.name
            if not name:
                # during creation, we may have to provide a temporary name for
                # subfolder, we don't want empty names on folders since they must be
                # unique among siblings
                name = u'{}_{}-{}'.format(self.__class__.__name__,
                                          str(self.id), time.asctime())
            self.folder = repository.root_folder.create_subfolder(name)
            #if not self.group:
            #  self.group = Group(name=self.name)

        if not self.image:
            fn = join(dirname(__file__), "data", "community.png")
            self.image = Blob(open(fn, 'rb').read())
Exemple #4
0
    def reindex_class(self, cls: Entity) -> None:
        current_object_type = cls._object_type()

        if not self.clear and current_object_type not in self.cleared:
            self.strategy.send(current_object_type)
            self.cleared.add(current_object_type)

        adapter = self.adapted.get(current_object_type)

        if not adapter or not adapter.indexable:
            return

        name = cls.__name__

        with self.session.begin():
            query = self.session.query(cls).options(sa.orm.lazyload("*"))
            try:
                count = query.count()
            except Exception as e:
                current_app.logger.error(
                    f"Indexing error on class {name}: {repr(e)}")
                return

            print("*" * 79)
            print(f"{name}")
            if count == 0:
                print("*" * 79)
                print(f"{name}")
                return

            print("*" * 79)
            print(f"{name}")

            with tqdm(total=count) as bar:
                self.reindex_batch(query, current_object_type, adapter, bar)

            if not self.batch_size:
                self.strategy.send(COMMIT)

        self.strategy.send(COMMIT)
Exemple #5
0
 def __init__(self, *args, **kw):
     Entity.__init__(self, *args, **kw)
     self._display_value_called = False
Exemple #6
0
 def __init__(self, *args, **kw):
     Entity.__init__(self, *args, **kw)
     self._display_value_called = False
Exemple #7
0
 def __init__(self, title="", body_src="", message="", *args, **kwargs):
     Entity.__init__(self, *args, **kwargs)
     self.title = title
     self.create_revision(body_src, message)
Exemple #8
0
def test_form_permissions_controller():
    security_mock = mock.Mock()
    has_role = security_mock.has_role = mock.Mock()
    has_role.return_value = True
    current_app_mock = mock.Mock()
    current_app_mock.services = dict(security=security_mock)
    MarkRole = Role('tests:mark-role')
    _MARK = object()
    _ENTITY_MARK = Entity()

    with mock.patch('abilian.web.forms.current_app', current_app_mock):
        # default role
        fp = FormPermissions()
        assert fp.has_permission(READ) == True
        assert has_role.called is True
        assert has_role.call_args[-1]['role'] == [Anonymous]
        has_role.reset_mock()
        assert fp.has_permission(READ, obj=_MARK) == True
        assert has_role.called is False

        has_role.reset_mock()
        assert fp.has_permission(READ, obj=_ENTITY_MARK) == True
        assert has_role.called is True
        assert has_role.call_args[-1]['object'] is _ENTITY_MARK

        # change default
        has_role.reset_mock()
        fp = FormPermissions(default=MarkRole)
        fp.has_permission(READ)
        assert has_role.call_args[-1]['role'] == [MarkRole]
        has_role.reset_mock()
        fp.has_permission(READ, field='test')
        assert has_role.call_args[-1]['role'] == [MarkRole]

        has_role.reset_mock()
        fp = FormPermissions(default=MarkRole, read=Anonymous)
        fp.has_permission(READ)
        assert has_role.call_args[-1]['role'] == [Anonymous]
        has_role.reset_mock()
        fp.has_permission(READ, field='test')
        assert has_role.call_args[-1]['role'] == [MarkRole]
        has_role.reset_mock()
        fp.has_permission(WRITE)
        assert has_role.call_args[-1]['role'] == [MarkRole]

        # field roles
        has_role.reset_mock()
        fp = FormPermissions(default=MarkRole,
                             read=Anonymous,
                             fields_read={'test': Owner})
        fp.has_permission(READ)
        assert has_role.call_args[-1]['role'] == [Anonymous]

        has_role.reset_mock()
        fp.has_permission(READ, field='test')
        assert has_role.call_args[-1]['role'] == [Owner]

        has_role.reset_mock()
        fp.has_permission(READ, field='test')
        assert has_role.call_args[-1]['role'] == [Owner]

        # dynamic roles
        has_role.reset_mock()
        dyn_roles = mock.Mock()
        dyn_roles.return_value = [MarkRole]
        fp = FormPermissions(read=dyn_roles)
        fp.has_permission(READ)
        assert dyn_roles.call_args == [
            dict(permission=READ, field=None, obj=None)
        ]
        assert has_role.call_args[-1]['role'] == [MarkRole]

        has_role.reset_mock()
        dyn_roles.reset_mock()
        fp = FormPermissions(read=[Owner, dyn_roles])
        fp.has_permission(READ)
        assert dyn_roles.call_args == [
            dict(permission=READ, field=None, obj=None)
        ]
        assert has_role.call_args[-1]['role'] == [Owner, MarkRole]
Exemple #9
0
 def __init__(self, title="", body_src="", message="", *args, **kwargs):
     Entity.__init__(self, *args, **kwargs)
     self.title = title
     self.create_revision(body_src, message)
Exemple #10
0
def test_form_permissions_controller():
    security_mock = mock.Mock()
    has_role = security_mock.has_role = mock.Mock()
    has_role.return_value = True

    current_app_mock = mock.Mock()
    current_app_mock.services = {"security": security_mock}

    MarkRole = Role("tests:mark-role")
    _MARK = object()
    _ENTITY_MARK = Entity()

    with mock.patch("abilian.services.current_app", current_app_mock):
        # default role
        fp = FormPermissions()
        assert fp.has_permission(READ)
        assert has_role.called
        assert has_role.call_args[-1]["role"] == [Anonymous]

        has_role.reset_mock()
        assert fp.has_permission(READ, obj=_MARK)
        assert not has_role.called

        has_role.reset_mock()
        assert fp.has_permission(READ, obj=_ENTITY_MARK)
        assert has_role.called
        assert has_role.call_args[-1]["object"] is _ENTITY_MARK

        # change default
        has_role.reset_mock()
        fp = FormPermissions(default=MarkRole)
        fp.has_permission(READ)
        assert has_role.call_args[-1]["role"] == [MarkRole]

        has_role.reset_mock()
        fp.has_permission(READ, field="test")
        assert has_role.call_args[-1]["role"] == [MarkRole]

        has_role.reset_mock()
        fp = FormPermissions(default=MarkRole, read=Anonymous)
        fp.has_permission(READ)
        assert has_role.call_args[-1]["role"] == [Anonymous]

        has_role.reset_mock()
        fp.has_permission(READ, field="test")
        assert has_role.call_args[-1]["role"] == [MarkRole]

        has_role.reset_mock()
        fp.has_permission(WRITE)
        assert has_role.call_args[-1]["role"] == [MarkRole]

        # field roles
        has_role.reset_mock()
        fp = FormPermissions(default=MarkRole,
                             read=Anonymous,
                             fields_read={"test": Owner})
        fp.has_permission(READ)
        assert has_role.call_args[-1]["role"] == [Anonymous]

        has_role.reset_mock()
        fp.has_permission(READ, field="test")
        assert has_role.call_args[-1]["role"] == [Owner]

        has_role.reset_mock()
        fp.has_permission(READ, field="test")
        assert has_role.call_args[-1]["role"] == [Owner]

        # dynamic roles
        has_role.reset_mock()
        dyn_roles = mock.Mock()
        dyn_roles.return_value = [MarkRole]
        fp = FormPermissions(read=dyn_roles)
        fp.has_permission(READ)
        assert dyn_roles.call_args == [
            dict(permission=READ, field=None, obj=None)
        ]
        assert has_role.call_args[-1]["role"] == [MarkRole]

        has_role.reset_mock()
        dyn_roles.reset_mock()
        fp = FormPermissions(read=[Owner, dyn_roles])
        fp.has_permission(READ)
        assert dyn_roles.call_args == [
            dict(permission=READ, field=None, obj=None)
        ]
        assert has_role.call_args[-1]["role"] == [Owner, MarkRole]