Skip to content

Commit 050c32d

Browse files
committed
Feature/create board by workspace (ProdPerfect#60)
* Added "create board by workspace id" * Update readme * Added missing type hints * Updated test * More custom type hints * Type hints for 'get_boards_query' * Fixed tests * Updated Readme and 'fetch_boards' method
1 parent cdeaa30 commit 050c32d

File tree

6 files changed

+115
-19
lines changed

6 files changed

+115
-19
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ monday.items.create_item(board_id='12345678', group_id='today', item_name='Do a
5757

5858

5959
#### Boards Resource (monday.boards)
60-
- `fetch_boards(**kwargs)` - Fetch boards associated with an account. Returns boards and their groups, tags, and columns. Accepts keyword arguments. See Monday API docs for a list of accepted keyword arguments.
60+
- `fetch_boards(**kwargs)` - Fetch boards associated with an account. Returns boards and their groups, tags, and columns. Accepts keyword arguments:
61+
- `limit` - The number of boards returned (*int*. Default is 25).
62+
- `page` - The page number returned, should you implement pagination(*int*. Starts at 1).
63+
- `ids` - A list of the unique board identifier(s) (*List[int]*).
64+
- `board_kind` - The board's kind (*BoardKind*. public / private / share).
65+
- `state` - The state of the board (*BoardState*. all / active / archived / deleted. Default is active).
66+
- `order_by` - The order in which to retrieve your boards (*BoardsOrderBy*. created_at / used_at).
67+
6168

6269
- `fetch_boards_by_id([board_ids])` - Since Monday does not allow querying boards by name, you can use `fetch_boards` to get a list of boards, and then `fetch_boards_by_id` to get more detailed info about the groups and columns on that board. Accepts a comma separated list of board ids.
6370

monday/query_joins.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
2-
from monday.resources.types import DuplicateTypes
3-
2+
from enum import Enum
3+
from typing import List
4+
from monday.resources.types import BoardKind, BoardState, BoardsOrderBy, DuplicateTypes
45
from monday.utils import monday_json_stringify
56

67

@@ -312,7 +313,18 @@ def get_board_items_query(board_id):
312313
return query
313314

314315

315-
def get_boards_query(**kwargs):
316+
def get_boards_query(limit: int = None, page: int = None, ids: List[int] = None, board_kind: BoardKind = None, state: BoardState = None, order_by: BoardsOrderBy = None):
317+
parameters = locals().items()
318+
query_params = []
319+
for k, v in parameters:
320+
if v is not None:
321+
value = v
322+
if isinstance(v, Enum):
323+
value = v.value
324+
325+
query_params.append("%s: %s" % (k, value))
326+
327+
316328
query = '''query
317329
{
318330
boards (%s) {
@@ -333,7 +345,8 @@ def get_boards_query(**kwargs):
333345
type
334346
}
335347
}
336-
}''' % ', '.join(["%s: %s" % (arg, kwargs.get(arg)) for arg in kwargs])
348+
}''' % ', '.join(query_params)
349+
337350
return query
338351

339352

@@ -362,6 +375,26 @@ def get_boards_by_id_query(board_ids):
362375
}''' % board_ids
363376

364377

378+
def get_columns_by_board_query(board_ids):
379+
return '''query
380+
{
381+
boards(ids: %s) {
382+
id
383+
name
384+
groups {
385+
id
386+
title
387+
}
388+
columns {
389+
title
390+
id
391+
type
392+
settings_str
393+
}
394+
}
395+
}''' % board_ids
396+
397+
365398
def duplicate_board_query(
366399
board_id: int,
367400
duplicate_type: DuplicateTypes,
@@ -402,15 +435,15 @@ def duplicate_board_query(
402435
return query
403436

404437

405-
def create_board_by_workspace_query(board_name, board_kind, workspace_id = None):
438+
def create_board_by_workspace_query(board_name: str, board_kind: BoardKind, workspace_id = None) -> str:
406439
workspace_query = f'workspace_id: {workspace_id}' if workspace_id else ''
407440
query = '''
408441
mutation {
409442
create_board (board_name:"%s", board_kind: %s, %s) {
410443
id
411444
}
412445
}
413-
''' % (board_name, board_kind, workspace_query)
446+
''' % (board_name, board_kind.value, workspace_query)
414447
return query
415448

416449

monday/resources/boards.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
from typing import List
12
from monday.resources.base import BaseResource
2-
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
3-
from monday.resources.types import DuplicateTypes
3+
from monday.query_joins import (
4+
duplicate_board_query,
5+
get_boards_query,
6+
get_boards_by_id_query,
7+
get_board_items_query,
8+
get_columns_by_board_query,
9+
create_board_by_workspace_query,
10+
)
11+
from monday.resources.types import BoardKind, BoardState, BoardsOrderBy, DuplicateTypes
412

513

614
class BoardResource(BaseResource):
715
def __init__(self, token):
816
super().__init__(token)
917

10-
def fetch_boards(self, **kwargs):
11-
query = get_boards_query(**kwargs)
18+
def fetch_boards(self, limit: int = None, page: int = None, ids: List[int] = None, board_kind: BoardKind = None, state: BoardState = None, order_by: BoardsOrderBy = None):
19+
query = get_boards_query(limit, page, ids, board_kind, state, order_by)
1220
return self.client.execute(query)
1321

1422
def fetch_boards_by_id(self, board_ids):
@@ -34,8 +42,7 @@ def duplicate_board(
3442
):
3543
query = duplicate_board_query(board_id, duplicate_type, board_name, workspace_id, folder_id, keep_subscribers)
3644
return self.client.execute(query)
37-
return self.client.execute(query)
3845

39-
def create_board(self, board_name, board_kind, workspace_id):
46+
def create_board(self, board_name: str, board_kind: BoardKind, workspace_id: int = None):
4047
query = create_board_by_workspace_query(board_name, board_kind, workspace_id)
4148
return self.client.execute(query)

monday/resources/types.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
from enum import Enum
22

33

4+
class BoardKind(Enum):
5+
"""Board kinds"""
6+
7+
PUBLIC = "public"
8+
PRIVATE = "private"
9+
SHARE = "share"
10+
11+
12+
class BoardState(Enum):
13+
"""Board available states"""
14+
15+
ALL = "all"
16+
ACTIVE = "active"
17+
ARCHIVED = "archived"
18+
DELETED = "deleted"
19+
20+
21+
class BoardsOrderBy(Enum):
22+
"""Order to retrieve boards"""
23+
24+
CREATED_AT = "created_at"
25+
USED_AT = "used_at"
26+
27+
428
class DuplicateTypes(Enum):
529
"""Board duplication types"""
630

monday/tests/test_board_resource.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,29 @@ def setUp(self):
77
super(BoardTestCase, self).setUp()
88

99
def test_get_boards_query(self):
10-
query = get_boards_query(board_kind=self.board_kind)
11-
self.assertIn(self.board_kind, query)
10+
query_a = get_boards_query(limit=1, page=2, ids=[888,999], board_kind=self.board_kind, state=self.board_state, order_by=self.boards_order_by)
11+
self.assertIn('1', query_a)
12+
self.assertIn('2', query_a)
13+
self.assertIn('[888, 999]', query_a)
14+
self.assertNotIn(str(self.board_kind), query_a)
15+
self.assertIn(str(self.board_kind.value), query_a)
16+
self.assertNotIn(str(self.board_state), query_a)
17+
self.assertIn(str(self.board_state.value), query_a)
18+
self.assertNotIn(str(self.boards_order_by), query_a)
19+
self.assertIn(str(self.boards_order_by.value), query_a)
20+
21+
query_b = get_boards_query(board_kind=self.board_kind)
22+
self.assertNotIn(str(self.board_kind), query_b)
23+
self.assertIn(str(self.board_kind.value), query_b)
24+
25+
query_c = get_boards_query(limit=1,state=self.board_state)
26+
self.assertIn('1', query_c)
27+
self.assertNotIn(str(self.board_state), query_c)
28+
self.assertIn(str(self.board_state.value), query_c)
29+
self.assertNotIn(str(self.board_kind), query_c)
30+
self.assertNotIn(str(self.boards_order_by), query_c)
31+
32+
1233

1334
def test_get_boards_by_id_query(self):
1435
query = get_boards_by_id_query(board_ids=self.board_id)
@@ -38,9 +59,11 @@ def test_duplicate_board_query(self):
3859
def test_create_board_by_workspace_query(self):
3960
query_a = create_board_by_workspace_query(board_name=self.board_name, board_kind=self.board_kind, workspace_id=self.workspace_id)
4061
self.assertIn(str(self.board_name), query_a)
41-
self.assertIn(str(self.board_kind), query_a)
62+
self.assertNotIn(str(self.board_kind), query_a)
63+
self.assertIn(str(self.board_kind.value), query_a)
4264
self.assertIn(str(self.workspace_id), query_a)
4365
query_b = create_board_by_workspace_query(board_name=self.board_name, board_kind=self.board_kind)
4466
self.assertIn(str(self.board_name), query_b)
45-
self.assertIn(str(self.board_kind), query_b)
67+
self.assertNotIn(str(self.board_kind), query_b)
68+
self.assertIn(str(self.board_kind.value), query_b)
4669
self.assertNotIn(str(self.workspace_id), query_b)

monday/tests/test_case_resource.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22

3-
from monday.resources.types import DuplicateTypes
3+
from monday.resources.types import BoardKind, BoardState, BoardsOrderBy, DuplicateTypes
44

55

66
class BaseTestCase(unittest.TestCase):
@@ -11,8 +11,10 @@ def setUp(self):
1111
self.item_id = 24
1212
self.board_name = "my_board"
1313
self.board_id = 12
14-
self.board_kind = "public"
1514
self.duplicate_type = DuplicateTypes.WITH_PULSES
15+
self.board_kind = BoardKind.PUBLIC
16+
self.board_state = BoardState.ACTIVE
17+
self.boards_order_by = BoardsOrderBy.USED_AT
1618
self.group_id = 7
1719
self.column_id = "file_column"
1820
self.user_ids = [1287123, 1230919]

0 commit comments

Comments
 (0)