Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

views: Conditional New Community btn visibility #1070

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading