From 79ec632346e7c870f9b79a4fbf6f1df70f0718bf Mon Sep 17 00:00:00 2001 From: alejandromumo Date: Thu, 10 Oct 2024 10:01:07 +0200 Subject: [PATCH] subcommunities: moved endpoint to communities resource --- .../communities/resources/config.py | 1 + .../communities/resources/resource.py | 16 ++++++++++ .../communities/services/config.py | 3 ++ .../communities/services/service.py | 29 +++++++++++++++++++ .../subcommunities/resources/config.py | 4 --- .../subcommunities/resources/resource.py | 16 ---------- .../subcommunities/services/service.py | 17 ----------- 7 files changed, 49 insertions(+), 37 deletions(-) diff --git a/invenio_communities/communities/resources/config.py b/invenio_communities/communities/resources/config.py index 302a81b8b..aa60e55cc 100644 --- a/invenio_communities/communities/resources/config.py +++ b/invenio_communities/communities/resources/config.py @@ -97,6 +97,7 @@ class CommunityResourceConfig(RecordResourceConfig, ConfiguratorMixin): "user-communities": "/user/communities", "community-requests": "/communities//requests", "restore-community": "/communities//restore", + "list-subcommunities": "/communities//subcommunities", } request_search_args = CommunitiesSearchRequestArgsSchema diff --git a/invenio_communities/communities/resources/resource.py b/invenio_communities/communities/resources/resource.py index d6c5d2d2c..1afbb992d 100644 --- a/invenio_communities/communities/resources/resource.py +++ b/invenio_communities/communities/resources/resource.py @@ -60,6 +60,7 @@ def create_url_rules(self): route("DELETE", routes["featured-item"], self.featured_delete), route("GET", routes["community-requests"], self.search_community_requests), route("POST", routes["restore-community"], self.restore_community), + route("GET", routes["list-subcommunities"], self.search_subcommunities), ] @request_search_args @@ -230,3 +231,18 @@ def featured_delete(self): featured_id=resource_requestctx.view_args["featured_id"], ) return "", 204 + + @request_view_args + @response_handler(many=True) + @request_extra_args + @request_search_args + def search_subcommunities(self): + """List subcommunities.""" + result = self.service.search_subcommunities( + identity=g.identity, + id_=resource_requestctx.view_args["pid_value"], + params=resource_requestctx.args, + search_preference=search_preference(), + expand=resource_requestctx.args.get("expand", False), + ) + return result.to_dict(), 200 diff --git a/invenio_communities/communities/services/config.py b/invenio_communities/communities/services/config.py index c56c4158a..208dc8027 100644 --- a/invenio_communities/communities/services/config.py +++ b/invenio_communities/communities/services/config.py @@ -144,6 +144,9 @@ class CommunityServiceConfig(RecordServiceConfig, ConfiguratorMixin): links_community_requests_search = pagination_links( "{+api}/communities/{community_id}/requests{?args*}" ) + links_subcommunities_search = pagination_links( + "{+api}/communities/{community_id}/subcommunities{?args*}" + ) available_actions = [ {"action_name": "featured", "action_permission": "featured_create"} diff --git a/invenio_communities/communities/services/service.py b/invenio_communities/communities/services/service.py index abba90455..e653125ec 100644 --- a/invenio_communities/communities/services/service.py +++ b/invenio_communities/communities/services/service.py @@ -735,6 +735,35 @@ def bulk_update_parent(self, identity, community_ids, parent_id, uow=None): ) return True + def search_subcommunities(self, identity, id_, params=None, **kwargs): + """Search for subcommunities of a community.""" + community = self.record_cls.pid.resolve(id_) + self.require_permission(identity, "search", record=community) + + params = params or {} + search_result = self._search( + "search", + identity, + params=params, + extra_filter=dsl.query.Bool( + "must", must=[dsl.Q("term", **{"parent.id": str(community.id)})] + ), + permission_action="read", + **kwargs, + ).execute() + + return self.result_list( + self, + identity, + search_result, + params, + links_tpl=LinksTemplate( + self.config.links_subcommunities_search, + context={"community_id": id_, "args": params}, + ), + links_item_tpl=self.links_item_tpl, + ) + @cached_with_expiration def get_cached_community_slug( diff --git a/invenio_communities/subcommunities/resources/config.py b/invenio_communities/subcommunities/resources/config.py index 103bcc25d..e30d30641 100644 --- a/invenio_communities/subcommunities/resources/config.py +++ b/invenio_communities/subcommunities/resources/config.py @@ -39,7 +39,6 @@ class SubCommunityResourceConfig(ConfiguratorMixin, ResourceConfig): url_prefix = "" routes = { "join": "/communities//actions/join-request", - "list": "/communities//subcommunities", } request_view_args = { "pid_value": fields.UUID(), @@ -56,9 +55,6 @@ class SubCommunityResourceConfig(ConfiguratorMixin, ResourceConfig): # Response handling response_handlers = { "application/json": json_response_handler, - "application/vnd.inveniordm.v1+json": ResponseHandler( - UICommunityJSONSerializer(), headers=etag_headers - ), } default_accept_mimetype = "application/json" diff --git a/invenio_communities/subcommunities/resources/resource.py b/invenio_communities/subcommunities/resources/resource.py index 2ee4adb3e..1c45e2b49 100644 --- a/invenio_communities/subcommunities/resources/resource.py +++ b/invenio_communities/subcommunities/resources/resource.py @@ -31,7 +31,6 @@ def create_url_rules(self): routes = self.config.routes url_rules = [ route("POST", routes["join"], self.join), - route("GET", routes["list"], self.search), ] return url_rules @@ -46,18 +45,3 @@ def join(self): data=resource_requestctx.data, ) return result.to_dict(), 201 - - @request_view_args - @response_handler(many=True) - @request_extra_args - @request_search_args - def search(self): - """List subcommunities.""" - result = self.service.search( - identity=g.identity, - id_=resource_requestctx.view_args["pid_value"], - params=resource_requestctx.args, - search_preference=search_preference(), - expand=resource_requestctx.args.get("expand", False), - ) - return result.to_dict(), 200 diff --git a/invenio_communities/subcommunities/services/service.py b/invenio_communities/subcommunities/services/service.py index 1b8d93c23..b60a70bbd 100644 --- a/invenio_communities/subcommunities/services/service.py +++ b/invenio_communities/subcommunities/services/service.py @@ -65,23 +65,6 @@ def _is_owner_of(self, identity, community): None, ) - def search(self, identity, id_, **kwargs): - """Return list of subcommunities.""" - subcommunities = community_service.search( - identity, - extra_filter=dsl.query.Bool( - "must", must=[dsl.Q("term", **{"parent.id": id_})] - ), - **kwargs - ) - self_link = LinksTemplate( - # https://github.com/inveniosoftware/invenio-communities/issues/1218 - # pagination_links("{+api}/communities/{community_id}/subcommunities{?args*}"), - pagination_links("{+api}/communities/{community_id}/subcommunities"), - context={"community_id": id_}, - ) - subcommunities._links_tpl = self_link - return subcommunities @unit_of_work() def join(self, identity, id_, data, uow=None):