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

Administration #51

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,6 @@ dist/

.model_venv/
.vscode

.venv-builder
.venv-tests
Empty file.
254 changes: 254 additions & 0 deletions oarepo_communities/administration/communities.py
Original file line number Diff line number Diff line change
@@ -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/<pid_value>"
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/<pid_value>/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
5 changes: 5 additions & 0 deletions oarepo_communities/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions oarepo_communities/resolvers/communities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file.
2 changes: 1 addition & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 14 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = oarepo-communities
version = 4.0.13
version = 4.0.14
description =
authors = Ronald Krist <[email protected]>
readme = README.md
Expand All @@ -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

Expand All @@ -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
Loading