diff --git a/.gitignore b/.gitignore index 0865e5d..7e4e78a 100644 --- a/.gitignore +++ b/.gitignore @@ -90,3 +90,6 @@ dist/ .model_venv/ .vscode + +.venv-builder +.venv-tests \ No newline at end of file diff --git a/oarepo_communities/administration/__init__.py b/oarepo_communities/administration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oarepo_communities/administration/communities.py b/oarepo_communities/administration/communities.py new file mode 100644 index 0000000..ce1daed --- /dev/null +++ b/oarepo_communities/administration/communities.py @@ -0,0 +1,254 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2022 CERN. +# +# 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. + +"""Invenio administration OAI-PMH view module.""" +from functools import partial + +from flask import current_app +from invenio_administration.views.base import ( + AdminResourceDetailView, + AdminResourceListView, AdminResourceCreateView, AdminResourceEditView, +) +from invenio_search_ui.searchconfig import search_app_config + +from invenio_communities.communities.schema import CommunityFeaturedSchema + +from invenio_i18n import lazy_gettext as _ + +import marshmallow as ma + +class CommunityListView(AdminResourceListView): + """Search admin view.""" + + api_endpoint = "/communities" + name = "oarepo-communities" + resource_config = "communities_resource" + search_request_headers = {"Accept": "application/vnd.inveniordm.v1+json"} + title = _("Communities") + menu_label = _("Communities") + category = "Communities" + pid_path = "id" + icon = "users" + template = "invenio_communities/administration/community_search.html" + + display_search = True + display_delete = False + display_create = True + display_edit = True + + create_view_name = "oarepo_community_create" + + item_field_list = { + "slug": {"text": "Slug", "order": 1, "width": 1}, + "metadata.title": {"text": "Title", "order": 2, "width": 4}, + "ui.type.title_l10n": {"text": "Type", "order": 3, "width": 2}, + "featured": {"text": "Featured", "order": 4, "width": 1}, + "created": {"text": "Created", "order": 5, "width": 2}, + } + + actions = { + "featured": { + "text": _("Feature"), + "payload_schema": CommunityFeaturedSchema, + "order": 1, + }, + # custom components in the UI + "delete": { + "text": _("Delete"), + "payload_schema": None, + "order": 2, + }, + # custom components in the UI + "restore": { + "text": _("Restore"), + "payload_schema": None, + "order": 2, + }, + } + search_config_name = "COMMUNITIES_SEARCH" + search_facets_config_name = "COMMUNITIES_FACETS" + search_sort_config_name = "COMMUNITIES_SORT_OPTIONS" + + def init_search_config(self): + """Build search view config.""" + return partial( + search_app_config, + config_name=self.get_search_app_name(), + available_facets=current_app.config.get(self.search_facets_config_name), + sort_options=current_app.config[self.search_sort_config_name], + endpoint=self.get_api_endpoint(), + headers=self.get_search_request_headers(), + initial_filters=[["status", "P"]], + hidden_params=[ + ["include_deleted", "1"], + ], + page=1, + size=30, + ) + +class AddCuratorSchema(ma.Schema): + curator = ma.fields.String(required=True, metadata={"type_override": "Blah.jsx"}) + +class CommunityDetailView(AdminResourceDetailView): + """Admin community detail view.""" + + url = "/oarepo-communities/" + api_endpoint = "/communities" + name = "oarepo-community-details" + resource_config = "communities_resource" + title = _("Community") + + # template = "invenio_communities/administration/community_details.html" + display_delete = False + display_edit = False + + list_view_name = "oarepo-communities" + pid_path = "id" + request_headers = {"Accept": "application/vnd.inveniordm.v1+json"} + + actions = { + "add_curator": { + "text": _("Add Curator"), + "payload_schema": AddCuratorSchema, + "order": 2, + }, + "remove_curator": { + "text": _("Remove Curator"), + "payload_schema": None, + "order": 3, + }, + } + + item_field_list = { + "slug": { + "text": "Slug", + "order": 1, + }, + "metadata.title": {"text": "Title", "order": 2}, + "created": {"text": "Created", "order": 5}, + } + + def _schema_to_json(self, schema): + ret = super()._schema_to_json(schema) + for k, v in ret.items(): + type_override = v.get('metadata', {}).get("type_override") + if type_override: + v['type'] = type_override + return ret + +COMMUNITY_EDIT_FIELDS = { + "metadata.title": { + "order": 2, + "text": _("Community Name"), + }, + "metadata.description": { + "order": 3, + "text": _("Community Description"), + "rows": 10, + }, + "access.visibility": { + "order": 4, + "text": _("Visibility"), + "options": [ + {"title_l10n": "Public", "id": "public"}, + {"title_l10n": "Restricted", "id": "restricted"}, + ], + }, + "access.members_visibility": { + "order": 5, + "text": _("Members Visibility"), + "options": [ + {"title_l10n": "Public", "id": "public"}, + {"title_l10n": "Restricted", "id": "restricted"}, + ], + } +} + + +class CommunityCreateView(AdminResourceCreateView): + """Configuration for Banner create view.""" + + name = "oarepo_community_create" + url = "/oarepo-communities/create" + resource_config = "communities_resource" + pid_path = "id" + api_endpoint = "/communities" + title = _("Create Community") + + list_view_name = "oarepo-communities" + + form_fields = { + "slug": { + "order": 1, + "text": _("Community Slug"), + }, + **COMMUNITY_EDIT_FIELDS, + # "access.member_policy": { + # "order": 3, + # "text": _("Member Policy"), + # "options": [ + # {"title_l10n": "Open", "id": "open"}, + # {"title_l10n": "Closed", "id": "closed"}, + # ], + # }, + # "access.record_policy": { + # "order": 4, + # "text": _("Record Policy"), + # "options": [ + # {"title_l10n": "Open", "id": "open"}, + # {"title_l10n": "Closed", "id": "closed"}, + # ], + # }, + # "access.review_policy": { + # "order": 5, + # "text": _("Review Policy"), + # "options": [ + # {"title_l10n": "Open", "id": "open"}, + # {"title_l10n": "Closed", "id": "closed"}, + # ], + # }, + } + + def _schema_to_json(self, schema): + ret = super()._schema_to_json(schema) + # TODO: probably bug in invenio schema_to_json, workaround + if 'properties' in ret['metadata']: + ret['metadata'].update(ret['metadata']['properties']) + if 'properties' in ret['access']: + ret['access'].update(ret['access']['properties']) + ret.pop('custom_fields', None) + ret.pop('theme', None) + ret.pop('children', None) + return ret + + +class CommunityEditView(AdminResourceEditView): + """Configuration for Banner edit view.""" + + name = "oarepo_community_edit" + url = "/oarepo-communities//edit" + resource_config = "communities_resource" + pid_path = "id" + api_endpoint = "/communities" + title = _("Edit Community") + + list_view_name = "oarepo-communities" + + form_fields = COMMUNITY_EDIT_FIELDS + + def _schema_to_json(self, schema): + ret = super()._schema_to_json(schema) + # TODO: probably bug in invenio schema_to_json, workaround + if 'properties' in ret['metadata']: + ret['metadata'].update(ret['metadata']['properties']) + if 'properties' in ret['access']: + ret['access'].update(ret['access']['properties']) + ret.pop('custom_fields', None) + ret.pop('theme', None) + ret.pop('children', None) + return ret diff --git a/oarepo_communities/ext.py b/oarepo_communities/ext.py index 76cf323..7a9610f 100644 --- a/oarepo_communities/ext.py +++ b/oarepo_communities/ext.py @@ -18,6 +18,7 @@ def __init__(self, app=None): def init_app(self, app): """Flask application initialization.""" + self.app = app self.init_config(app) self.init_services(app) self.init_resources(app) @@ -57,3 +58,7 @@ def init_resources(self, app): config=CommunityRecordsResourceConfig.build(app), service=self.community_records_service, ) + + @property + def communities_resource(self): + return self.app.extensions['invenio-communities'].communities_resource diff --git a/oarepo_communities/resolvers/communities.py b/oarepo_communities/resolvers/communities.py index 588cff0..bd1b738 100644 --- a/oarepo_communities/resolvers/communities.py +++ b/oarepo_communities/resolvers/communities.py @@ -21,6 +21,8 @@ class OARepoCommunityPKProxy(CommunityPKProxy): def get_needs(self, ctx=None): """Return community member need.""" comid = str(self._parse_ref_dict_id()) + if "community_permission_name" not in ctx: + return [] needs = needs_from_community_ids({comid}, ctx["community_permission_name"]) return needs diff --git a/oarepo_communities/services/members/__init__.py b/oarepo_communities/services/members/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/run-tests.sh b/run-tests.sh index 300267c..687506e 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -33,6 +33,6 @@ python3 -m venv $TESTS_VENV pip install -U setuptools pip wheel pip install "oarepo[tests]==${OAREPO_VERSION}.*" pip install "./$BUILD_TEST_DIR/${MODEL}[tests]" -pip install . +pip install ".[tests]" #editable_install /home/ron/prace/oarepo-requests pytest ./$CODE_TEST_DIR/test_communities diff --git a/setup.cfg b/setup.cfg index 183608f..63aa660 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-communities -version = 4.0.13 +version = 4.0.14 description = authors = Ronald Krist readme = README.md @@ -11,12 +11,17 @@ long_description_content_type = text/markdown [options] python = >=3.9 install_requires = - invenio-communities # not setting version here, will be limited by oarepo - invenio-drafts-resources # not setting version here, will be limited by oarepo - oarepo-runtime + # invenio-communities # not setting version here, will be limited by oarepo + # invenio-drafts-resources # not setting version here, will be limited by oarepo cachetools #packages = find: +[options.extras_require] +tests = + pytest-invenio + oarepo-global-search + oarepo-runtime + [options.package_data] * = *.json, *.rst, *.md, *.json5, *.jinja2 @@ -31,4 +36,8 @@ invenio_base.blueprints = oarepo_communities = oarepo_communities.views.app:create_app_blueprint invenio_requests.entity_resolvers = oarepo_community = oarepo_communities.resolvers.communities:OARepoCommunityResolver - +invenio_administration.views = + oarepo_communities_details = oarepo_communities.administration.communities:CommunityDetailView + oarepo_communities_list = oarepo_communities.administration.communities:CommunityListView + oarepo_communities_create = oarepo_communities.administration.communities:CommunityCreateView + oarepo_communities_edit = oarepo_communities.administration.communities:CommunityEditView