Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv3: return permissions expandable field on projects #10978

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions readthedocs/api/v3/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):

"""
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions readthedocs/api/v3/tests/responses/projects-detail.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
},
"modified": "2019-04-29T12:00:00Z",
"name": "project",
"permissions": {
"admin": true
},
"programming_language": {
"code": "words",
"name": "Only Words"
Expand Down
14 changes: 10 additions & 4 deletions readthedocs/api/v3/tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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"
),
}

Expand Down
1 change: 1 addition & 0 deletions readthedocs/api/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class ProjectsViewSetBase(
"active_versions.last_build",
"active_versions.last_build.config",
"organization",
"permissions",
"teams",
]

Expand Down