Skip to content

Commit

Permalink
add desk members api to fetch users for specific desk
Browse files Browse the repository at this point in the history
SDBELGA-894
  • Loading branch information
petrjasek committed Oct 18, 2024
1 parent 0b4d539 commit 1a3640c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
40 changes: 39 additions & 1 deletion apps/desks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from superdesk.activity import add_activity, ACTIVITY_UPDATE
from superdesk.metadata.item import FAMILY_ID, ITEM_STATE, CONTENT_STATE
from eve.utils import ParsedRequest
from superdesk.utils import ListCursor
from flask_babel import _, lazy_gettext


Expand Down Expand Up @@ -103,6 +102,9 @@ def init_app(app) -> None:
endpoint_name = "desk_overview"
service = OverviewService(endpoint_name, backend=superdesk.get_backend())
OverviewResource(endpoint_name, app=app, service=service)
endpoint_name = "desk_users"
service = DeskUsersService(endpoint_name, backend=superdesk.get_backend())
DeskUsersResource(endpoint_name, app=app, service=service)


superdesk.privilege(
Expand Down Expand Up @@ -371,6 +373,42 @@ def get_by_user(self, user_id):
return list(self.get(req=None, lookup={"user_id": user_id}))


class DeskUsersResource(Resource):
url = 'desks/<regex("[a-f0-9]{24}"):desk_id>/users'
resource_title = "desk_users"
datasource = {
"source": "users",
"default_sort": [("username", 1)],
"projection": {
"username": 1,
"first_name": 1,
"last_name": 1,
"display_name": 1,
"email": 1,
"picture_url": 1,
"avatar": 1,
"avatar_renditions": 1,
"role": 1,
"last_activity_at": 1,
"sign_off": 1,
},
}
resource_methods = ["GET"]


class DeskUsersService(BaseService):
def get(self, req, lookup):
desk_id = lookup.pop("desk_id", None)
desks_service = superdesk.get_resource_service("desks")
if desk_id:
desk = desks_service.find_one(req=None, _id=ObjectId(desk_id))
if desk and desk.get("members"):
lookup["_id"] = {"$in": [member["user"] for member in desk.get("members", [])]}
else:
lookup["_id"] = "" # return empty result
return super().get(req, lookup)


class SluglineDesksResource(Resource):
url = 'desks/<regex("[a-f0-9]{24}"):desk_id>/sluglines'
datasource = {
Expand Down
23 changes: 22 additions & 1 deletion features/desks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,25 @@ Feature: Desks
"template_desks": ["#desks._id#"]
}
"""


@auth
Scenario: Desk members resource
Given "users"
"""
[
{"username": "foo", "email": "[email protected]", "display_name": "foo"},
{"username": "bar", "email": "[email protected]", "display_name": "bar"}
]
"""
And "desks"
"""
[
{"name": "Finance", "members": []},
{"name": "Sports", "members": [{"user": "#users._id#"}]}
]
"""
When we get "/desks/#desks._id#/users"
Then we get list with 1 items
"""
{"_items": [{"username": "bar", "display_name": "bar"}]}
"""

0 comments on commit 1a3640c

Please sign in to comment.