Skip to content
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

Feature/create board by workspace #60

Merged
merged 8 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,23 @@ monday.items.create_item(board_id='12345678', group_id='today', item_name='Do a


#### Boards Resource (monday.boards)
- `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.
- `fetch_boards(**kwargs)` - Fetch boards associated with an account. Returns boards and their groups, tags, and columns. Accepts keyword arguments:
- `limit` - The number of boards returned (*int*. Default is 25).
- `page` - The page number returned, should you implement pagination(*int*. Starts at 1).
- `ids` - A list of the unique board identifier(s) (*List[int]*).
- `board_kind` - The board's kind (*BoardKind*. public / private / share).
- `state` - The state of the board (*BoardState*. all / active / archived / deleted. Default is active).
- `order_by` - The order in which to retrieve your boards (*BoardsOrderBy*. created_at / used_at).


- `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.

- `fetch_columns_by_board_id([board_ids])` - Get all columns, as well as their ids, types, and settings. Accepts a comma separated list of board ids.

- `fetch_items_by_board_id([board_ids])` - Get all items on a board(s). Accepts a comma separated list of board ids.

- `create_board(board_name, board_kind, workspace_id)` - Create board with the given name and kind by (and optional) workspace id.


#### Users Resource (monday.users)
- `fetch_users(**kwargs)` - Fetch user information associated with an account. See Monday API docs for a list of accepted keyword arguments.
Expand Down
31 changes: 29 additions & 2 deletions monday/query_joins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from enum import Enum
import json
from typing import List
from monday.resources.types import BoardKind, BoardState, BoardsOrderBy

from monday.utils import monday_json_stringify

Expand Down Expand Up @@ -286,7 +289,18 @@ def get_board_items_query(board_id):
return query


def get_boards_query(**kwargs):
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):
parameters = locals().items()
query_params = []
for k, v in parameters:
if v is not None:
value = v
if isinstance(v, Enum):
value = v.value

query_params.append("%s: %s" % (k, value))


query = '''query
{
boards (%s) {
Expand All @@ -307,7 +321,8 @@ def get_boards_query(**kwargs):
type
}
}
}''' % ', '.join(["%s: %s" % (arg, kwargs.get(arg)) for arg in kwargs])
}''' % ', '.join(query_params)

return query


Expand Down Expand Up @@ -356,6 +371,18 @@ 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:
workspace_query = f'workspace_id: {workspace_id}' if workspace_id else ''
query = '''
mutation {
create_board (board_name:"%s", board_kind: %s, %s) {
id
}
}
''' % (board_name, board_kind.value, workspace_query)
return query


# USER RESOURCE QUERIES
def get_users_query(**kwargs):
query = '''query
Expand Down
19 changes: 15 additions & 4 deletions monday/resources/boards.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from typing import List
from monday.resources.base import BaseResource
from monday.query_joins import get_boards_query, get_boards_by_id_query, get_board_items_query, \
get_columns_by_board_query
from monday.query_joins import (
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


class BoardResource(BaseResource):
def __init__(self, token):
super().__init__(token)

def fetch_boards(self, **kwargs):
query = get_boards_query(**kwargs)
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):
query = get_boards_query(limit, page, ids, board_kind, state, order_by)
return self.client.execute(query)

def fetch_boards_by_id(self, board_ids):
Expand All @@ -22,3 +29,7 @@ def fetch_items_by_board_id(self, board_ids):
def fetch_columns_by_board_id(self, board_ids):
query = get_columns_by_board_query(board_ids)
return self.client.execute(query)

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)
25 changes: 25 additions & 0 deletions monday/resources/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from enum import Enum


class BoardKind(Enum):
"""Board kinds"""

PUBLIC = "public"
PRIVATE = "private"
SHARE = "share"


class BoardState(Enum):
"""Board available states"""

ALL = "all"
ACTIVE = "active"
ARCHIVED = "archived"
DELETED = "deleted"


class BoardsOrderBy(Enum):
"""Order to retrieve boards"""

CREATED_AT = "created_at"
USED_AT = "used_at"
39 changes: 36 additions & 3 deletions monday/tests/test_board_resource.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from monday.tests.test_case_resource import BaseTestCase
from monday.query_joins import get_boards_query, get_boards_by_id_query, get_board_items_query, \
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


Expand All @@ -8,8 +8,29 @@ def setUp(self):
super(BoardTestCase, self).setUp()

def test_get_boards_query(self):
query = get_boards_query(board_kind=self.board_kind)
self.assertIn(self.board_kind, query)
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)
self.assertIn('1', query_a)
self.assertIn('2', query_a)
self.assertIn('[888, 999]', query_a)
self.assertNotIn(str(self.board_kind), query_a)
self.assertIn(str(self.board_kind.value), query_a)
self.assertNotIn(str(self.board_state), query_a)
self.assertIn(str(self.board_state.value), query_a)
self.assertNotIn(str(self.boards_order_by), query_a)
self.assertIn(str(self.boards_order_by.value), query_a)

query_b = get_boards_query(board_kind=self.board_kind)
self.assertNotIn(str(self.board_kind), query_b)
self.assertIn(str(self.board_kind.value), query_b)

query_c = get_boards_query(limit=1,state=self.board_state)
self.assertIn('1', query_c)
self.assertNotIn(str(self.board_state), query_c)
self.assertIn(str(self.board_state.value), query_c)
self.assertNotIn(str(self.board_kind), query_c)
self.assertNotIn(str(self.boards_order_by), query_c)



def test_get_boards_by_id_query(self):
query = get_boards_by_id_query(board_ids=self.board_id)
Expand All @@ -22,3 +43,15 @@ def test_get_board_items_query(self):
def test_get_columns_by_board_query(self):
query = get_columns_by_board_query(board_ids=self.board_id)
self.assertIn(str(self.board_id), query)

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.assertNotIn(str(self.board_kind), query_a)
self.assertIn(str(self.board_kind.value), 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.assertNotIn(str(self.board_kind), query_b)
self.assertIn(str(self.board_kind.value), query_b)
self.assertNotIn(str(self.workspace_id), query_b)
7 changes: 6 additions & 1 deletion monday/tests/test_case_resource.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import unittest

from monday.resources.types import BoardKind, BoardState, BoardsOrderBy


class BaseTestCase(unittest.TestCase):

def setUp(self):
self.group_name = "my_group"
self.item_name = "Nerd"
self.item_id = 24
self.board_name = "my_board"
self.board_id = 12
self.board_kind = "public"
self.board_kind = BoardKind.PUBLIC
self.board_state = BoardState.ACTIVE
self.boards_order_by = BoardsOrderBy.USED_AT
self.group_id = 7
self.column_id = "file_column"
self.user_ids = [1287123, 1230919]
Expand Down