diff --git a/readthedocs/api/v3/serializers.py b/readthedocs/api/v3/serializers.py index 12629122c57..a5d9c0c0301 100644 --- a/readthedocs/api/v3/serializers.py +++ b/readthedocs/api/v3/serializers.py @@ -13,6 +13,7 @@ from readthedocs.builds.constants import LATEST, STABLE from readthedocs.builds.models import Build, Version +from readthedocs.core.permissions import AdminPermission from readthedocs.core.resolver import Resolver from readthedocs.core.utils import slugify from readthedocs.core.utils.extend import SettingsOverrideObject @@ -728,6 +729,14 @@ class ProjectUpdateSerializer(SettingsOverrideObject): _default_class = ProjectUpdateSerializerBase +class ProjectPermissionSerializer(serializers.Serializer): + admin = serializers.SerializerMethodField() + + def get_admin(self, obj): + user = self.context.get("request").user + return AdminPermission.is_admin(user, obj) + + class ProjectSerializer(FlexFieldsModelSerializer): """ @@ -806,6 +815,12 @@ class Meta: # NOTE: we cannot have a Project with multiple organizations. {"source": "organizations.first"}, ), + "permissions": ( + ProjectPermissionSerializer, + { + "source": "*", + }, + ), } def __init__(self, *args, **kwargs): diff --git a/readthedocs/api/v3/tests/responses/projects-detail.json b/readthedocs/api/v3/tests/responses/projects-detail.json index a27c32ac04a..a3c9e4046e1 100644 --- a/readthedocs/api/v3/tests/responses/projects-detail.json +++ b/readthedocs/api/v3/tests/responses/projects-detail.json @@ -82,6 +82,9 @@ }, "modified": "2019-04-29T12:00:00Z", "name": "project", + "permissions": { + "admin": true + }, "programming_language": { "code": "words", "name": "Only Words" diff --git a/readthedocs/api/v3/tests/test_projects.py b/readthedocs/api/v3/tests/test_projects.py index 40cf19bb3e2..08ba349c687 100644 --- a/readthedocs/api/v3/tests/test_projects.py +++ b/readthedocs/api/v3/tests/test_projects.py @@ -111,10 +111,12 @@ def test_projects_detail_anonymous_user(self): "expand": ( "active_versions," "active_versions.last_build," - "active_versions.last_build.config" + "active_versions.last_build.config," + "permissions" ), } expected_response = self._get_response_dict("projects-detail") + expected_response["permissions"]["admin"] = False self.client.logout() @@ -139,7 +141,8 @@ def test_projects_detail(self): "expand": ( "active_versions," "active_versions.last_build," - "active_versions.last_build.config" + "active_versions.last_build.config," + "permissions" ), } expected_response = self._get_response_dict("projects-detail") @@ -170,10 +173,12 @@ def test_projects_detail_other_user(self): "expand": ( "active_versions," "active_versions.last_build," - "active_versions.last_build.config" + "active_versions.last_build.config," + "permissions" ), } expected_response = self._get_response_dict("projects-detail") + expected_response["permissions"]["admin"] = False self.client.logout() self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.others_token.key}") @@ -201,7 +206,8 @@ def test_own_projects_detail_privacy_levels_enabled(self): "expand": ( "active_versions," "active_versions.last_build," - "active_versions.last_build.config" + "active_versions.last_build.config," + "permissions" ), } diff --git a/readthedocs/api/v3/views.py b/readthedocs/api/v3/views.py index 0104bcdce0e..5bdc9f7f52d 100644 --- a/readthedocs/api/v3/views.py +++ b/readthedocs/api/v3/views.py @@ -136,6 +136,7 @@ class ProjectsViewSetBase( "active_versions.last_build", "active_versions.last_build.config", "organization", + "permissions", "teams", ]