diff --git a/docs/README.rst b/docs/README.rst index 2390b4d..1eec07f 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -124,6 +124,13 @@ Boards Resource (monday.boards) - ``create_board(board_name, board_kind, workspace_id)`` - Create board with the given name and kind by (and optional) workspace id. +- ``duplicate_board(board_id, duplicate_type, board_name, workspace_id, folder_id, keep_subscribers)`` - Duplicate a + board by its id. It requires a duplication type to be chosen (*duplicate_board_with_structure / duplicate_board_with_pulses / duplicate_board_with_pulses_and_updates*). Optionaly you can use: + - ``board_name`` - The duplicated board's name (*string*). + - ``workspace_id`` - The destination workspace (*int* Defaults to the original board's workspace). + - ``folder_id`` - The destination folder within the destination workspace. The folder_id is required if you are duplicating to another workspace. (*int* Defaults to originals board's folder). + - ``keep_subscribers`` - Ability to duplicate the subscribers to the new board (*Boolean* Defaults to false). + Users Resource (monday.users) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/monday/query_joins.py b/monday/query_joins.py index d075643..fcfc0a4 100644 --- a/monday/query_joins.py +++ b/monday/query_joins.py @@ -1,7 +1,7 @@ from enum import Enum import json from typing import List, Union, Optional -from monday.resources.types import BoardKind, BoardState, BoardsOrderBy +from monday.resources.types import BoardKind, BoardState, BoardsOrderBy, DuplicateTypes from monday.utils import monday_json_stringify, gather_params @@ -401,7 +401,47 @@ def get_columns_by_board_query(board_ids): }''' % board_ids -def create_board_by_workspace_query(board_name: str, board_kind: BoardKind, workspace_id = None) -> str: +def duplicate_board_query( + board_id: int, + duplicate_type: DuplicateTypes, + board_name: str = None, + workspace_id: int = None, + folder_id: int = None, + keep_subscribers: bool = None, +) -> str: + board_name = board_name if board_name else "" + workspace_id = workspace_id if workspace_id else None + folder_id = folder_id if folder_id else None + keep_subscribers = keep_subscribers if keep_subscribers else False + + params = """board_id: %s, duplicate_type: %s, board_name: \"%s\"""" % ( + board_id, + duplicate_type.value, + board_name, + ) + + if workspace_id: + params += """, workspace_id: %s""" + + query = """ + mutation { + duplicate_board(%s) { + board { + id + groups{ + id + } + } + } + } + """ % ( + params + ) + + return query + + +def create_board_by_workspace_query(board_name, board_kind, workspace_id = None): workspace_query = f'workspace_id: {workspace_id}' if workspace_id else '' query = ''' mutation { @@ -409,7 +449,7 @@ def create_board_by_workspace_query(board_name: str, board_kind: BoardKind, work id } } - ''' % (board_name, board_kind.value, workspace_query) + ''' % (board_name, board_kind, workspace_query) return query diff --git a/monday/resources/boards.py b/monday/resources/boards.py index ce506a6..3edf135 100644 --- a/monday/resources/boards.py +++ b/monday/resources/boards.py @@ -1,13 +1,14 @@ from typing import List, Optional from monday.resources.base import BaseResource from monday.query_joins import ( + duplicate_board_query, get_boards_query, get_boards_by_id_query, get_board_items_query, get_columns_by_board_query, create_board_by_workspace_query, ) -from monday.resources.types import BoardKind, BoardState, BoardsOrderBy +from monday.resources.types import BoardKind, BoardState, BoardsOrderBy, DuplicateTypes class BoardResource(BaseResource): @@ -33,3 +34,15 @@ def fetch_columns_by_board_id(self, board_ids): def create_board(self, board_name: str, board_kind: BoardKind, workspace_id: int = None): query = create_board_by_workspace_query(board_name, board_kind, workspace_id) return self.client.execute(query) + + def duplicate_board( + self, + board_id: int, + duplicate_type: DuplicateTypes, + board_name: str = None, + workspace_id: int = None, + folder_id: int = None, + keep_subscribers: bool = None, + ): + query = duplicate_board_query(board_id, duplicate_type, board_name, workspace_id, folder_id, keep_subscribers) + return self.client.execute(query) diff --git a/monday/resources/types.py b/monday/resources/types.py index ba160e1..8031f19 100644 --- a/monday/resources/types.py +++ b/monday/resources/types.py @@ -23,3 +23,11 @@ class BoardsOrderBy(Enum): CREATED_AT = "created_at" USED_AT = "used_at" + + +class DuplicateTypes(Enum): + """Board duplication types""" + + WITH_STRUCTURE = "duplicate_board_with_structure" + WITH_PULSES = "duplicate_board_with_pulses" + WITH_PULSES_AND_UPDATES = "duplicate_board_with_pulses_and_updates" diff --git a/monday/tests/test_board_resource.py b/monday/tests/test_board_resource.py index f8dfd85..967373f 100644 --- a/monday/tests/test_board_resource.py +++ b/monday/tests/test_board_resource.py @@ -1,6 +1,5 @@ from monday.tests.test_case_resource import BaseTestCase -from monday.query_joins import create_board_by_workspace_query, get_boards_query, get_boards_by_id_query, get_board_items_query, \ - get_columns_by_board_query +from monday.query_joins import duplicate_board_query, create_board_by_workspace_query, get_boards_query, get_boards_by_id_query, get_board_items_query, get_columns_by_board_query class BoardTestCase(BaseTestCase): @@ -64,3 +63,26 @@ def test_create_board_by_workspace_query(self): self.assertNotIn(str(self.board_kind), query_b) self.assertIn(str(self.board_kind.value), query_b) self.assertNotIn(str(self.workspace_id), query_b) + + def test_duplicate_board_query(self): + query_a = duplicate_board_query(board_id=self.board_id, duplicate_type=self.duplicate_type) + self.assertIn(str(self.board_id), query_a) + self.assertNotIn(str(self.duplicate_type), query_a) + self.assertIn(str(self.duplicate_type.value), query_a) + query_b = duplicate_board_query(board_id=self.board_id, duplicate_type=self.duplicate_type, board_name='testing_name', workspace_id=1, folder_id=2) + self.assertIn(str(self.board_id), query_b) + self.assertNotIn(str(self.duplicate_type), query_b) + self.assertIn(str(self.duplicate_type.value), query_b) + self.assertIn(str('testing_name'), query_b) + self.assertIn(str(1), query_b) + self.assertIn(str(2), query_b) + + def test_create_board_by_workspace_query(self): + query_a = create_board_by_workspace_query(board_name=self.board_name, board_kind=self.board_kind, workspace_id=self.workspace_id) + self.assertIn(str(self.board_name), query_a) + self.assertIn(str(self.board_kind), query_a) + self.assertIn(str(self.workspace_id), query_a) + query_b = create_board_by_workspace_query(board_name=self.board_name, board_kind=self.board_kind) + self.assertIn(str(self.board_name), query_b) + self.assertIn(str(self.board_kind), query_b) + self.assertNotIn(str(self.workspace_id), query_b) diff --git a/monday/tests/test_case_resource.py b/monday/tests/test_case_resource.py index f6a4d5c..46f9de7 100644 --- a/monday/tests/test_case_resource.py +++ b/monday/tests/test_case_resource.py @@ -1,6 +1,6 @@ import unittest -from monday.resources.types import BoardKind, BoardState, BoardsOrderBy +from monday.resources.types import BoardKind, BoardState, BoardsOrderBy, DuplicateTypes class BaseTestCase(unittest.TestCase): @@ -11,9 +11,11 @@ def setUp(self): self.item_id = 24 self.board_name = "my_board" self.board_id = 12 + self.duplicate_type = DuplicateTypes.WITH_PULSES self.board_kind = BoardKind.PUBLIC self.board_state = BoardState.ACTIVE self.boards_order_by = BoardsOrderBy.USED_AT + self.duplicate_type = DuplicateTypes.WITH_PULSES self.group_id = 7 self.column_id = "file_column" self.user_ids = [1287123, 1230919] @@ -28,4 +30,5 @@ def setUp(self): self.team_ids = [105939, 105940, 105941] self.notification_text = "This is an awesome notification." self.notification_target_type = "Project" + self.folder_id = "678549"