Skip to content

Commit

Permalink
Include translated role title in @roles GET:
Browse files Browse the repository at this point in the history
This returns the translated role name (as displayed in the
"Users and Groups" control panel) as the title property in the
output of the @roles endpoint.

Note that the sharing view uses a different set of translations
for the same role ID.
  • Loading branch information
lukasgraf committed Jun 22, 2018
1 parent 9cf12cf commit 1c105bf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
21 changes: 14 additions & 7 deletions docs/source/_json/roles.resp
Expand Up @@ -5,36 +5,43 @@ Content-Type: application/json
{
"@id": "http://localhost:55001/plone/@roles/Contributor",
"@type": "role",
"id": "Contributor"
"id": "Contributor",
"title": "Contributor"
},
{
"@id": "http://localhost:55001/plone/@roles/Editor",
"@type": "role",
"id": "Editor"
"id": "Editor",
"title": "Editor"
},
{
"@id": "http://localhost:55001/plone/@roles/Member",
"@type": "role",
"id": "Member"
"id": "Member",
"title": "Member"
},
{
"@id": "http://localhost:55001/plone/@roles/Reader",
"@type": "role",
"id": "Reader"
"id": "Reader",
"title": "Reader"
},
{
"@id": "http://localhost:55001/plone/@roles/Reviewer",
"@type": "role",
"id": "Reviewer"
"id": "Reviewer",
"title": "Reviewer"
},
{
"@id": "http://localhost:55001/plone/@roles/Site Administrator",
"@type": "role",
"id": "Site Administrator"
"id": "Site Administrator",
"title": "Site Administrator"
},
{
"@id": "http://localhost:55001/plone/@roles/Manager",
"@type": "role",
"id": "Manager"
"id": "Manager",
"title": "Manager"
}
]
3 changes: 3 additions & 0 deletions docs/source/roles.rst
Expand Up @@ -15,3 +15,6 @@ The server will respond with a list of all groups in the portal:

.. literalinclude:: _json/roles.resp
:language: http

The role ``title`` is the translated role title as displayed in Plone's
"Users and Groups" control panel.
2 changes: 2 additions & 0 deletions src/plone/restapi/services/roles/get.py
Expand Up @@ -2,6 +2,7 @@
from Acquisition import aq_inner
from plone.restapi.services import Service
from Products.CMFCore.utils import getToolByName
from zope.i18n import translate


class RolesGet(Service):
Expand All @@ -14,6 +15,7 @@ def reply(self):
'@type': 'role',
'@id': '{}/@roles/{}'.format(self.context.absolute_url(), r),
'id': r,
'title': translate(r, context=self.request, domain='plone'),
}
for r in roles
]
35 changes: 28 additions & 7 deletions src/plone/restapi/tests/test_roles.py
Expand Up @@ -25,23 +25,44 @@ def test_roles_endpoint_lists_roles(self):
self.assertEqual([
{u'@id': u'http://localhost:55001/plone/@roles/Contributor',
u'@type': u'role',
u'id': u'Contributor'},
u'id': u'Contributor',
u'title': u'Contributor'},
{u'@id': u'http://localhost:55001/plone/@roles/Editor',
u'@type': u'role',
u'id': u'Editor'},
u'id': u'Editor',
u'title': u'Editor'},
{u'@id': u'http://localhost:55001/plone/@roles/Member',
u'@type': u'role',
u'id': u'Member'},
u'id': u'Member',
u'title': u'Member'},
{u'@id': u'http://localhost:55001/plone/@roles/Reader',
u'@type': u'role',
u'id': u'Reader'},
u'id': u'Reader',
u'title': u'Reader'},
{u'@id': u'http://localhost:55001/plone/@roles/Reviewer',
u'@type': u'role',
u'id': u'Reviewer'},
u'id': u'Reviewer',
u'title': u'Reviewer'},
{u'@id': u'http://localhost:55001/plone/@roles/Site Administrator',
u'@type': u'role',
u'id': u'Site Administrator'},
u'id': u'Site Administrator',
u'title': u'Site Administrator'},
{u'@id': u'http://localhost:55001/plone/@roles/Manager',
u'@type': u'role',
u'id': u'Manager'}],
u'id': u'Manager',
u'title': u'Manager'}],
response.json())

def test_roles_endpoint_translates_role_titles(self):
self.api_session.headers.update({'Accept-Language': 'de'})
response = self.api_session.get('/@roles')

self.assertItemsEqual([
u'Hinzuf\xfcgen',
u'Bearbeiten',
u'Benutzer',
u'Ansehen',
u'Ver\xf6ffentlichen',
u'Website-Administrator',
u'Verwalten'],
[item['title'] for item in response.json()])

0 comments on commit 1c105bf

Please sign in to comment.