Skip to content

Commit

Permalink
views: New Community link visibility config
Browse files Browse the repository at this point in the history
  • Loading branch information
Samk13 authored and slint committed Nov 27, 2023
1 parent 6c9669d commit c8104bf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions invenio_communities/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file is part of Invenio.
# Copyright (C) 2016-2022 CERN.
# Copyright (C) 2023 Graz University of Technology.
# Copyright (C) 2023 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -312,3 +313,6 @@
)

COMMUNITIES_OAI_SETS_PREFIX = "community-"

COMMUNITIES_ALWAYS_SHOW_CREATE_LINK = False
"""Controls visibility of 'New Community' btn based on user's permission when set to True."""
19 changes: 17 additions & 2 deletions invenio_communities/views/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# This file is part of Invenio.
# Copyright (C) 2016-2021 CERN.
# Copyright (C) 2023 Graz University of Technology.
# Copyright (C) 2023 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -83,8 +84,21 @@ def record_permission_denied_error(error):

def _can_create_community():
"""Function used to check if a user has permissions to create a community."""
can_create = current_communities.service.check_permission(g.identity, "create")
return can_create
return current_communities.service.check_permission(g.identity, "create")


def _show_create_community_link():
"""
Determine if the 'New community' button should always be visible.
If the 'COMMUNITIES_ALWAYS_SHOW_CREATE_LINK' config is False,
check the user's permission to create a community link. If the config is
True, the button is always visible.
"""
should_show = current_app.config.get("COMMUNITIES_ALWAYS_SHOW_CREATE_LINK", False)
if not should_show: # show only when user can create
should_show = _can_create_community()
return should_show


def _has_about_page_content():
Expand Down Expand Up @@ -192,6 +206,7 @@ def register_menus():
"invenio_communities.communities_new",
_("New community"),
order=3,
visible_when=_show_create_community_link,
)

communities = current_menu.submenu("communities")
Expand Down
36 changes: 36 additions & 0 deletions tests/communities/tests_views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 CERN.
# Copyright (C) 2023 KTH Royal Institute of Technology.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Test community views."""

from flask import g
from invenio_records_permissions.generators import SystemProcess

from invenio_communities.permissions import CommunityPermissionPolicy
from invenio_communities.views.communities import _filter_roles
from invenio_communities.views.ui import _show_create_community_link


def _test_filter_roles(app, members, action, member_types, community_id):
Expand Down Expand Up @@ -60,3 +66,33 @@ def test_filter_roles_when_adding_groups(
{"group"},
community.id,
)


def test_show_create_community_link(app, users, superuser_identity):
"""Test the _can_create_community function under different config settings."""
test_users = ["reader", "curator", "manager", "owner"]
ALWAYS_SHOW_CREATE_LINK = "COMMUNITIES_ALWAYS_SHOW_CREATE_LINK"

# Test default config allows community creation
assert app.config.get(ALWAYS_SHOW_CREATE_LINK) == False
assert _show_create_community_link() == True

# Test with config set to True
app.config[ALWAYS_SHOW_CREATE_LINK] = True
assert app.config.get(ALWAYS_SHOW_CREATE_LINK) == True
assert _show_create_community_link() == True

# Test with different user identities
for user in test_users:
g.identity = users[user].identity
assert _show_create_community_link() == True

# Test with community creation disabled
CommunityPermissionPolicy.can_create = [SystemProcess()]
for user in test_users:
g.identity = users[user].identity
assert _show_create_community_link() == False

# Test superuser
g.identity = superuser_identity
assert _show_create_community_link() == True

0 comments on commit c8104bf

Please sign in to comment.