|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from plane.api.base_resource import BaseResource |
| 6 | +from plane.models.collections import ( |
| 7 | + AddCollectionPages, |
| 8 | + CollectionPage, |
| 9 | + CollectionPageSearchResult, |
| 10 | + PaginatedCollectionPageResponse, |
| 11 | + UpdateCollectionPage, |
| 12 | +) |
| 13 | +from plane.models.query_params import CollectionPageQueryParams |
| 14 | + |
| 15 | + |
| 16 | +class CollectionPages(BaseResource): |
| 17 | + def __init__(self, config: Any) -> None: |
| 18 | + super().__init__(config, "/workspaces/") |
| 19 | + |
| 20 | + def list( |
| 21 | + self, |
| 22 | + workspace_slug: str, |
| 23 | + collection_id: str, |
| 24 | + params: CollectionPageQueryParams | None = None, |
| 25 | + ) -> PaginatedCollectionPageResponse: |
| 26 | + """List pages that belong to a collection. |
| 27 | +
|
| 28 | + Args: |
| 29 | + workspace_slug: The workspace slug identifier |
| 30 | + collection_id: UUID of the collection |
| 31 | + params: Optional search/parent_id/pagination filters |
| 32 | + """ |
| 33 | + query_params = params.model_dump(exclude_none=True) if params else None |
| 34 | + response = self._get( |
| 35 | + f"{workspace_slug}/collections/{collection_id}/pages", params=query_params |
| 36 | + ) |
| 37 | + return PaginatedCollectionPageResponse.model_validate(response) |
| 38 | + |
| 39 | + def add( |
| 40 | + self, workspace_slug: str, collection_id: str, data: AddCollectionPages |
| 41 | + ) -> list[CollectionPage]: |
| 42 | + """Add existing page(s) to a collection. |
| 43 | +
|
| 44 | + Args: |
| 45 | + workspace_slug: The workspace slug identifier |
| 46 | + collection_id: UUID of the collection |
| 47 | + data: Page IDs to add, with optional sort_orders/placement |
| 48 | + """ |
| 49 | + response = self._post( |
| 50 | + f"{workspace_slug}/collections/{collection_id}/pages", |
| 51 | + data.model_dump(exclude_none=True), |
| 52 | + ) |
| 53 | + return [CollectionPage.model_validate(item) for item in response] |
| 54 | + |
| 55 | + def search( |
| 56 | + self, workspace_slug: str, collection_id: str, search: str | None = None |
| 57 | + ) -> list[CollectionPageSearchResult]: |
| 58 | + """Search pages that are not yet in a collection, to add them. |
| 59 | +
|
| 60 | + Args: |
| 61 | + workspace_slug: The workspace slug identifier |
| 62 | + collection_id: UUID of the collection |
| 63 | + search: Optional case-insensitive substring filter on page name |
| 64 | + """ |
| 65 | + query_params = {"search": search} if search else None |
| 66 | + response = self._get( |
| 67 | + f"{workspace_slug}/collections/{collection_id}/pages-search", |
| 68 | + params=query_params, |
| 69 | + ) |
| 70 | + return [CollectionPageSearchResult.model_validate(item) for item in response] |
| 71 | + |
| 72 | + def update( |
| 73 | + self, |
| 74 | + workspace_slug: str, |
| 75 | + collection_id: str, |
| 76 | + page_collection_id: str, |
| 77 | + data: UpdateCollectionPage, |
| 78 | + ) -> CollectionPage: |
| 79 | + """Move a page to a different collection, or reorder it within the current one. |
| 80 | +
|
| 81 | + Args: |
| 82 | + workspace_slug: The workspace slug identifier |
| 83 | + collection_id: UUID of the page's current collection |
| 84 | + page_collection_id: UUID of the page-collection membership row |
| 85 | + data: `collection` to move (omit/leave unset to just reorder), |
| 86 | + and/or `sort_order`/`placement` to reorder |
| 87 | + """ |
| 88 | + response = self._patch( |
| 89 | + f"{workspace_slug}/collections/{collection_id}/pages/{page_collection_id}", |
| 90 | + data.model_dump(exclude_none=True), |
| 91 | + ) |
| 92 | + return CollectionPage.model_validate(response) |
| 93 | + |
| 94 | + def remove(self, workspace_slug: str, collection_id: str, page_collection_id: str) -> None: |
| 95 | + """Remove a page from a collection (does not delete the page itself). |
| 96 | +
|
| 97 | + Args: |
| 98 | + workspace_slug: The workspace slug identifier |
| 99 | + collection_id: UUID of the collection |
| 100 | + page_collection_id: UUID of the page-collection membership row |
| 101 | + """ |
| 102 | + return self._delete( |
| 103 | + f"{workspace_slug}/collections/{collection_id}/pages/{page_collection_id}" |
| 104 | + ) |
0 commit comments