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

Added mutation for multiple items #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ Getting started

monday.items.create_item(board_id='12345678', group_id='today', item_name='Do a thing')

**Available methods:** #### Items Resource (monday.items) -
**Available methods:**

Items Resource (monday.items)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
``create_item(board_id, group_id, item_name, column_values=None, create_labels_if_missing=False)``
- Create an item on a board in the given group with name item_name.

Expand Down
38 changes: 34 additions & 4 deletions monday/query_joins.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,37 @@ def mutate_subitem_query(parent_item_id, subitem_name, column_values,
str(create_labels_if_missing).lower())


def mutate_multiple_items_query(items_data):
Copy link
Collaborator

@chdastolfo chdastolfo Apr 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer item_data, and data as it is more grammatically correct than items_data and item_data. I think the function name makes it clear that the function acts on multiple items.

mutation_parts = []
for index, item_data in enumerate(items_data):
method = item_data.pop('method')
params = ', '.join([f"{key}: %s" for key in item_data.keys()])
values = [
monday_json_stringify(value) if not isinstance(value, (int, bool, str)) else
(str(value).lower() if isinstance(value, bool) else
f'"{value}"' if isinstance(value, str) else value)
for value in item_data.values()
]

mutation_part = f'''
item{index}: {method}({params}) {{
id
name
column_values {{
id
text
}}
}}
''' % tuple(values)

mutation_parts.append(mutation_part)

query = '''mutation {
%s
}''' % ' '.join(mutation_parts)
return query


def get_item_query(board_id, column_id, value):
query = '''query
{
Expand Down Expand Up @@ -290,7 +321,6 @@ def get_tags_query(tags):

# BOARD RESOURCE QUERIES
def get_board_items_query(board_id: Union[str, int], limit: Optional[int] = None, page: Optional[int] = None) -> str:

raw_params = locals().items()
item_params = gather_params(raw_params, exclusion_list=["board_id"])
joined_params = ', '.join(item_params)
Expand Down Expand Up @@ -319,7 +349,8 @@ def get_board_items_query(board_id: Union[str, int], limit: Optional[int] = None
return query


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):
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:
Expand All @@ -330,7 +361,6 @@ def get_boards_query(limit: int = None, page: int = None, ids: List[int] = None,

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


query = '''query
{
boards (%s) {
Expand Down Expand Up @@ -401,7 +431,7 @@ 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 create_board_by_workspace_query(board_name: str, board_kind: BoardKind, workspace_id=None) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

workspace_query = f'workspace_id: {workspace_id}' if workspace_id else ''
query = '''
mutation {
Expand Down
8 changes: 6 additions & 2 deletions monday/resources/items.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from monday.resources.base import BaseResource
from monday.query_joins import mutate_item_query, get_item_query, update_item_query, get_item_by_id_query, \
update_multiple_column_values_query, mutate_subitem_query, add_file_to_column_query, delete_item_query, \
archive_item_query, move_item_to_group_query
archive_item_query, move_item_to_group_query, mutate_multiple_items_query


class ItemResource(BaseResource):
Expand All @@ -20,6 +20,10 @@ def create_subitem(self, parent_item_id, subitem_name, column_values=None,
create_labels_if_missing)
return self.client.execute(query)

def mutate_multiple_items(self, items_data):
query = mutate_multiple_items_query(items_data)
return self.client.execute(query)

def fetch_items_by_column_value(self, board_id, column_id, value):
query = get_item_query(board_id, column_id, value)
return self.client.execute(query)
Expand Down Expand Up @@ -48,7 +52,7 @@ def move_item_to_group(self, item_id, group_id):
def archive_item_by_id(self, item_id):
query = archive_item_query(item_id)
return self.client.execute(query)

def delete_item_by_id(self, item_id):
query = delete_item_query(item_id)
return self.client.execute(query)