-
Notifications
You must be signed in to change notification settings - Fork 5.2k
feat(api): expose project pages in the public REST API #9528
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
Open
RemoYukoff
wants to merge
5
commits into
makeplane:preview
Choose a base branch
from
RemoYukoff:feat/public-api-project-pages
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46ebdb4
feat(api): expose project pages in the public REST API
RemoYukoff 54245aa
fix(api): allow blank description_html on page create/update
RemoYukoff 67ae632
test(api): add contract tests for public pages endpoints
RemoYukoff 7cedb80
fix(api): address review feedback on pages endpoints
RemoYukoff 331ddaa
fix(api): address second review round on pages endpoints
RemoYukoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # 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 | ||
|
|
||
| # Third party imports | ||
| from rest_framework import serializers | ||
|
|
||
| # Module imports | ||
| from .base import BaseSerializer | ||
| from plane.db.models import Page, Project, ProjectPage | ||
| from plane.utils.content_validator import validate_html_content | ||
|
|
||
|
|
||
| class PageSerializer(BaseSerializer): | ||
| """ | ||
| Serializer for pages with metadata fields. | ||
|
|
||
| Handles page metadata (name, access, color, parent, lock and archive | ||
| state) for list responses. Page content is exposed through | ||
| PageDetailSerializer. | ||
| """ | ||
|
|
||
| class Meta: | ||
| model = Page | ||
| fields = [ | ||
| "id", | ||
| "name", | ||
| "access", | ||
| "color", | ||
| "parent", | ||
| "is_locked", | ||
| "archived_at", | ||
| "workspace", | ||
| "view_props", | ||
| "logo_props", | ||
| "external_id", | ||
| "external_source", | ||
| "owned_by", | ||
| "created_at", | ||
| "updated_at", | ||
| "created_by", | ||
| "updated_by", | ||
| ] | ||
| read_only_fields = [ | ||
| "id", | ||
| "workspace", | ||
| "owned_by", | ||
| "archived_at", | ||
| "created_at", | ||
| "updated_at", | ||
| "created_by", | ||
| "updated_by", | ||
| ] | ||
| extra_kwargs = {"name": {"required": True, "allow_blank": False}} | ||
|
|
||
| def validate_parent(self, value): | ||
| if value is None: | ||
| return value | ||
|
|
||
| # Reject the page itself and any of its descendants to avoid cycles; | ||
| # track visited ids so a pre-existing cycle in the chain cannot loop forever | ||
| if self.instance: | ||
| visited = set() | ||
| ancestor = value | ||
| while ancestor is not None: | ||
| if ancestor.id == self.instance.id: | ||
| raise serializers.ValidationError("Parent page cannot be the page itself or one of its descendants") | ||
| if ancestor.id in visited: | ||
| raise serializers.ValidationError("Parent page hierarchy contains a cycle") | ||
| visited.add(ancestor.id) | ||
| ancestor = ancestor.parent | ||
|
|
||
| # The parent page must belong to the same project | ||
| project_id = self.context.get("project_id") or ( | ||
| self.instance and self.instance.project_pages.values_list("project_id", flat=True).first() | ||
| ) | ||
| 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 | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class PageDetailSerializer(PageSerializer): | ||
| """ | ||
| Extended page serializer including HTML content. | ||
|
|
||
| Provides the full page representation with description_html for | ||
| create, retrieve and update operations. | ||
| """ | ||
|
|
||
| description_html = serializers.CharField(required=False, allow_blank=True) | ||
|
|
||
| class Meta(PageSerializer.Meta): | ||
| fields = PageSerializer.Meta.fields + ["description_html"] | ||
|
|
||
| def validate_description_html(self, value): | ||
| # Validate and sanitize the HTML content for security | ||
| if value: | ||
| is_valid, error_msg, sanitized_html = validate_html_content(value) | ||
| if not is_valid: | ||
| raise serializers.ValidationError(error_msg or "html content is not valid") | ||
| if sanitized_html is not None: | ||
| return sanitized_html | ||
| return value | ||
|
|
||
| def create(self, validated_data): | ||
| project_id = self.context["project_id"] | ||
| owned_by_id = self.context["owned_by_id"] | ||
|
|
||
| # Get the workspace id from the project | ||
| project = Project.objects.get(pk=project_id) | ||
|
|
||
| with transaction.atomic(): | ||
| # Create the page | ||
| page = Page.objects.create( | ||
| **validated_data, | ||
| owned_by_id=owned_by_id, | ||
| created_by_id=owned_by_id, | ||
| updated_by_id=owned_by_id, | ||
| workspace_id=project.workspace_id, | ||
| ) | ||
|
|
||
| # Create the project page | ||
| 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.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # 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.page import ( | ||
| PageListCreateAPIEndpoint, | ||
| PageDetailAPIEndpoint, | ||
| PageArchiveUnarchiveAPIEndpoint, | ||
| ) | ||
|
|
||
| urlpatterns = [ | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/", | ||
| PageListCreateAPIEndpoint.as_view(http_method_names=["get", "post"]), | ||
| name="pages", | ||
| ), | ||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:pk>/", | ||
| PageDetailAPIEndpoint.as_view(http_method_names=["get", "patch", "delete"]), | ||
| name="page-detail", | ||
| ), | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| path( | ||
| "workspaces/<str:slug>/projects/<uuid:project_id>/pages/<uuid:pk>/archive/", | ||
| PageArchiveUnarchiveAPIEndpoint.as_view(http_method_names=["post", "delete"]), | ||
| name="page-archive-unarchive", | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.