-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat(api): expose project pages in the public REST API #9020
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
base: preview
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| # Django imports | ||
| from django.db import transaction | ||
| from django.shortcuts import get_object_or_404 | ||
|
|
||
| # Third party imports | ||
| from rest_framework import serializers | ||
|
|
||
| # Module imports | ||
| from .base import BaseSerializer | ||
| from plane.db.models import Page, ProjectPage, Project | ||
|
|
||
|
|
||
| class PageSerializer(BaseSerializer): | ||
| """ | ||
| Serializer for project pages. | ||
|
|
||
| Handles creation of a Page along with its ProjectPage join row so the | ||
| public API can create, list, retrieve and update pages scoped to a | ||
| project. Labels and revisions are not exposed in the MVP serializer. | ||
| """ | ||
|
|
||
| class Meta: | ||
| model = Page | ||
| fields = [ | ||
| "id", | ||
| "name", | ||
| "description_html", | ||
| "description_json", | ||
| "owned_by", | ||
| "access", | ||
| "color", | ||
| "parent", | ||
| "is_locked", | ||
| "archived_at", | ||
| "view_props", | ||
| "logo_props", | ||
| "sort_order", | ||
| "external_id", | ||
| "external_source", | ||
| "workspace", | ||
| "created_at", | ||
| "updated_at", | ||
| "created_by", | ||
| "updated_by", | ||
| ] | ||
| read_only_fields = [ | ||
| "id", | ||
| "owned_by", | ||
| "workspace", | ||
| "created_at", | ||
| "updated_at", | ||
| "created_by", | ||
| "updated_by", | ||
| ] | ||
|
|
||
| def validate_parent(self, value): | ||
| if value is None: | ||
| return value | ||
| project_id = self.context.get("project_id") | ||
| if project_id and not ProjectPage.objects.filter( | ||
| page_id=value.id, | ||
| project_id=project_id, | ||
| deleted_at__isnull=True, | ||
| ).exists(): | ||
| raise serializers.ValidationError( | ||
| "Parent page must belong to the same project." | ||
| ) | ||
| return value | ||
|
Comment on lines
+60
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The past review concern about cross-project parent assignment has been addressed. However, 🛡️ Proposed fix def validate_parent(self, value):
if value is None:
return value
+ if self.instance and value.id == self.instance.pk:
+ raise serializers.ValidationError(
+ "A page cannot be its own parent."
+ )
project_id = self.context.get("project_id")
if project_id and not ProjectPage.objects.filter(
page_id=value.id,
project_id=project_id,
deleted_at__isnull=True,
).exists():
raise serializers.ValidationError(
"Parent page must belong to the same project."
)
return value🤖 Prompt for AI Agents |
||
|
|
||
| @transaction.atomic | ||
| def create(self, validated_data): | ||
| project_id = self.context["project_id"] | ||
| owned_by_id = self.context["owned_by_id"] | ||
|
|
||
| project = get_object_or_404(Project, pk=project_id) | ||
|
|
||
| page = Page.objects.create( | ||
| **validated_data, | ||
| owned_by_id=owned_by_id, | ||
| workspace_id=project.workspace_id, | ||
| ) | ||
|
|
||
| ProjectPage.objects.create( | ||
| workspace_id=page.workspace_id, | ||
| project_id=project_id, | ||
| page_id=page.id, | ||
| created_by_id=page.created_by_id, | ||
| updated_by_id=page.updated_by_id, | ||
| ) | ||
|
|
||
| return page | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| from django.urls import path | ||
|
|
||
| from plane.api.views import ( | ||
| ProjectPageListCreateAPIEndpoint, | ||
| ProjectPageDetailAPIEndpoint, | ||
| ProjectPageArchiveAPIEndpoint, | ||
| ProjectPageLockAPIEndpoint, | ||
| ProjectPageAccessAPIEndpoint, | ||
| ProjectPageDuplicateAPIEndpoint, | ||
| ProjectPageSummaryAPIEndpoint, | ||
| ) | ||
|
|
||
| urlpatterns = [ | ||
| # CRUD | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/", | ||
| ProjectPageListCreateAPIEndpoint.as_view(http_method_names=["get", "post"]), | ||
| name="project-pages", | ||
| ), | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:page_id>/", | ||
| ProjectPageDetailAPIEndpoint.as_view(http_method_names=["get", "patch", "delete"]), | ||
| name="project-pages-detail", | ||
| ), | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| # Summary | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages-summary/", | ||
| ProjectPageSummaryAPIEndpoint.as_view(http_method_names=["get"]), | ||
| name="project-pages-summary", | ||
| ), | ||
| # Archive / unarchive | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:page_id>/archive/", | ||
| ProjectPageArchiveAPIEndpoint.as_view(http_method_names=["post", "delete"]), | ||
| name="project-page-archive-unarchive", | ||
| ), | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/archived-pages/", | ||
| ProjectPageArchiveAPIEndpoint.as_view(http_method_names=["get"]), | ||
| name="project-archived-pages", | ||
| ), | ||
| # Lock / unlock | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:page_id>/lock/", | ||
| ProjectPageLockAPIEndpoint.as_view(http_method_names=["post", "delete"]), | ||
| name="project-pages-lock-unlock", | ||
| ), | ||
| # Access toggle | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:page_id>/access/", | ||
| ProjectPageAccessAPIEndpoint.as_view(http_method_names=["post"]), | ||
| name="project-pages-access", | ||
| ), | ||
| # Duplicate | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:page_id>/duplicate/", | ||
| ProjectPageDuplicateAPIEndpoint.as_view(http_method_names=["post"]), | ||
| name="project-pages-duplicate", | ||
| ), | ||
| ] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_lockedandarchived_atshould be read-only.Both fields are managed exclusively by the dedicated
/lock/and/archive/endpoints (which enforce ownership checks, recursive cascade, etc.). Exposing them as writable in the serializer lets a client silently setis_locked=trueorarchived_at=<timestamp>on aPOST /pages/orPATCH /pages/<id>/call, completely bypassing that business logic.🛡️ Proposed fix
read_only_fields = [ "id", "owned_by", "workspace", "created_at", "updated_at", "created_by", "updated_by", + "is_locked", + "archived_at", ]🤖 Prompt for AI Agents