Skip to content

Commit

Permalink
subcommunities: moved endpoint to communities resource
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Oct 11, 2024
1 parent cacde1c commit 79ec632
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 37 deletions.
1 change: 1 addition & 0 deletions invenio_communities/communities/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class CommunityResourceConfig(RecordResourceConfig, ConfiguratorMixin):
"user-communities": "/user/communities",
"community-requests": "/communities/<pid_value>/requests",
"restore-community": "/communities/<pid_value>/restore",
"list-subcommunities": "/communities/<pid_value>/subcommunities",
}

request_search_args = CommunitiesSearchRequestArgsSchema
Expand Down
16 changes: 16 additions & 0 deletions invenio_communities/communities/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions invenio_communities/communities/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
29 changes: 29 additions & 0 deletions invenio_communities/communities/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions invenio_communities/subcommunities/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class SubCommunityResourceConfig(ConfiguratorMixin, ResourceConfig):
url_prefix = ""
routes = {
"join": "/communities/<pid_value>/actions/join-request",
"list": "/communities/<pid_value>/subcommunities",
}
request_view_args = {
"pid_value": fields.UUID(),
Expand All @@ -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"

Expand Down
16 changes: 0 additions & 16 deletions invenio_communities/subcommunities/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
17 changes: 0 additions & 17 deletions invenio_communities/subcommunities/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 79ec632

Please sign in to comment.