Skip to content

oarepo_config.communities

oarepo_config.communities

Configuration for communities.

DefaultCommunitiesPermissionPolicy

Bases: BooleanPermissionPolicyMixin, CommunityPermissionPolicy

Default permission policy for communities.

Source code in oarepo_config/communities.py
class DefaultCommunitiesPermissionPolicy(BooleanPermissionPolicyMixin, CommunityPermissionPolicy):  # type: ignore[reportIncompatibleVariableOverride]
    """Default permission policy for communities."""

    can_create = (Administration(), SystemProcess())
    can_submit_record = (CanSubmitRecordInCommunity(), SystemProcess())
    can_include_directly = (SystemProcess(),)
    can_members_add = (SystemProcess(),)
    can_members_search = (
        PrimaryCommunityRole("owner"),
        PrimaryCommunityRole("curator"),
        SystemProcess(),
    )
    can_members_search_public = (
        PrimaryCommunityRole("owner"),
        PrimaryCommunityRole("curator"),
        SystemProcess(),
    )
    can_members_update = (
        CommunityManagersForRole(),
        SystemProcess(),
    )
    can_members_delete = can_members_update
    # Match invenio core: any authenticated non-member may request membership,
    # but only when the feature flag is on and the community's member policy is
    # "open" (otherwise nobody, not even a superuser).
    can_request_membership = (
        IfCommunityAllowsMembershipRequests(
            then_=[AuthenticatedUserButNotCommunityMember()],
            else_=[Disable()],
        ),
    )

configure_communities(communities_roles: list | None = None) -> None

Set up the configuration for communities.

Communities let you organize records into collections with their own managers and settings. This function configures who can do what in each community (the roles), how records are submitted to communities, and other community-related settings.

Parameters:

Name Type Description Default
communities_roles list | None

Custom role definitions for communities. If not provided, defaults to owner/curator/member roles. If omitted, three default roles are used: "owner" (can manage the community and its members), "curator" (can review/curate submitted records) and "member" (can only view). Pass your own list to replace the defaults, e.g. to add a "submitter" role. Each role dictionary understands the keys name, title, description, is_owner, can_manage, can_curate and can_manage_roles (the names of the roles this role is allowed to add/remove).

None

Invenio configuration variables set:

  • COMMUNITIES_REGISTER_UI_BLUEPRINT - always True; enables the communities UI.
  • COMMUNITIES_PERMISSION_POLICY - the permission policy class for communities.
  • COMMUNITIES_ROLES - the list of available roles in communities.
Example
config.configure_communities()
# or with custom settings:

config.configure_communities(
    communities_roles=[
        dict(
            name="owner",
            title=_("Community owner"),
            is_owner=True,
            can_manage=True,
            can_manage_roles=[
                "owner",
                "curator",
                "member",
                "submitter",
            ],
        ),
        dict(
            name="curator",
            title=_("Curator"),
            can_manage=True,
            can_manage_roles=[
                "member",
                "submitter",
            ],
        ),
        dict(
            name="submitter",
            title=_("Submitter"),
            can_manage=False,
        ),
        dict(
            name="member",
            title=_("Member"),
        ),
    ]
)
Source code in oarepo_config/communities.py
def configure_communities(
    communities_roles: list | None = None,
) -> None:
    """Set up the configuration for communities.

    Communities let you organize records into collections with their own
    managers and settings. This function configures who can do what in
    each community (the roles), how records are submitted to communities,
    and other community-related settings.

    Args:
        communities_roles: Custom role definitions for communities. If not
            provided, defaults to owner/curator/member roles. If omitted, three
            default roles are used: "owner" (can manage the community
            and its members), "curator" (can review/curate submitted
            records) and "member" (can only view). Pass your own list to
            replace the defaults, e.g. to add a "submitter" role. Each
            role dictionary understands the keys `name`, `title`,
            `description`, `is_owner`, `can_manage`,
            `can_curate` and `can_manage_roles` (the names of the
            roles this role is allowed to add/remove).

    Invenio configuration variables set:

    * `COMMUNITIES_REGISTER_UI_BLUEPRINT` - always `True`; enables
      the communities UI.
    * `COMMUNITIES_PERMISSION_POLICY` - the permission policy class for
      communities.
    * `COMMUNITIES_ROLES` - the list of available roles in communities.

    Example:
        ```python
        config.configure_communities()
        # or with custom settings:

        config.configure_communities(
            communities_roles=[
                dict(
                    name="owner",
                    title=_("Community owner"),
                    is_owner=True,
                    can_manage=True,
                    can_manage_roles=[
                        "owner",
                        "curator",
                        "member",
                        "submitter",
                    ],
                ),
                dict(
                    name="curator",
                    title=_("Curator"),
                    can_manage=True,
                    can_manage_roles=[
                        "member",
                        "submitter",
                    ],
                ),
                dict(
                    name="submitter",
                    title=_("Submitter"),
                    can_manage=False,
                ),
                dict(
                    name="member",
                    title=_("Member"),
                ),
            ]
        )
        ```

    """
    COMMUNITIES_REGISTER_UI_BLUEPRINT = True
    COMMUNITIES_PERMISSION_POLICY = DefaultCommunitiesPermissionPolicy
    COMMUNITIES_ROLES = communities_roles or [
        # note: order matters, roles should be sorted by importance
        # from the most important to the least
        {
            "name": "owner",
            "title": _("Community owner"),
            "description": _("Can manage community."),
            "is_owner": True,
            "can_manage": True,
            "can_manage_roles": ["owner", "curator", "member"],
        },
        {
            "name": "curator",
            "title": _("Curator"),
            "description": _("Can curate records."),
            "can_manage": True,
            "can_manage_roles": ["member"],
        },
        {
            "name": "member",
            "title": _("Member"),
            "description": _("Community member with read permissions."),
        },
    ]

    set_constants_in_caller(locals())