-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_units_monday16.py
182 lines (153 loc) · 5.96 KB
/
14_units_monday16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import json
from dotenv import load_dotenv
import requests
from datetime import datetime, timedelta
from pytz import timezone
# Load environment variables
load_dotenv()
api = os.getenv("MONDAY_API_KEY")
url = "https://api.monday.com/v2"
headers = {
"Content-Type": "application/json",
"Authorization": api,
}
# New status mapping for flag statuses
FLAG_STATUS_MAP = {"Good": "1", "Flagged": "0"}
def update_column_value(item_id, board_id, column_id, value):
if value is None:
print(f"Skipping update for {column_id} due to null value")
return True
mutation = """
mutation {
change_simple_column_value (
item_id: %s,
board_id: %s,
column_id: "%s",
value: "%s"
) {
id
}
}
""" % (
item_id,
board_id,
column_id,
str(value),
)
try:
response = requests.post(url, json={"query": mutation}, headers=headers)
response_data = response.json()
if response.status_code == 200 and "data" in response_data:
print(f"Updated {column_id} column for item {item_id} with value {value}")
return True
else:
error_message = response_data.get("errors", [{"message": "Unknown error"}])[
0
]["message"]
print(f"Error updating {column_id} for item {item_id}: {error_message}")
return False
except Exception as e:
print(f"Error updating {column_id} for item {item_id}: {str(e)}")
return False
def safe_get(note, key, default=None):
"""Safely get a value from the note dictionary"""
value = note.get(key, default)
return value if value is not None else default
def update_all_columns(item_id, board_id, note, board_id_from_filename):
try:
cst = timezone("US/Central")
# target_date = (datetime.now(cst).strftime("%Y-%m-%d"))
date_str = (datetime.now(cst) - timedelta(days=7)).strftime("%Y-%m-%d")
if date_str is not None:
update_column_value(item_id, board_id, "date4", date_str)
units_added = safe_get(note, "total_units")
if units_added is not None:
update_column_value(item_id, board_id, "numbers_mkm6axzx", units_added)
units_status = safe_get(note, "units_status")
if units_status is not None:
units_status_value = FLAG_STATUS_MAP.get(
units_status, "1"
) # Default to "Good" if not found
update_column_value(item_id, board_id, "status_mkm61j", units_status_value)
units_reason = safe_get(note, "units_reason")
if units_reason is not None:
update_column_value(item_id, board_id, "long_text_mkm6wg9t", units_reason)
except Exception as e:
print(f"Error in update_all_columns for item {item_id}: {str(e)}")
return False
return True
def process_json_file(filename):
file_path = filename
created_items = []
try:
with open(file_path, "r") as file:
data = json.load(file)
notes = data.get("notes", [])
if not notes:
print(f"No notes found in {filename}")
return created_items
group_id = notes[0].get("group_name", "")
if not group_id:
print(f"No group_id found in {filename}")
return created_items
board_id_from_filename = os.path.splitext(os.path.basename(filename))[0]
print(f"Processing {board_id_from_filename} with group_id: {group_id}")
for day_number, note in enumerate(notes, 1):
group_title = "Daily Units Report"
mutation = """
mutation {
create_item(
board_id: 8198737855,
group_id: "%s",
item_name: "%s") {
id
}
}
""" % (
group_id,
group_title.replace('"', '\\"'),
)
try:
response = requests.post(url, json={"query": mutation}, headers=headers)
response_data = response.json()
if response.status_code == 200 and "data" in response_data:
item_id = response_data["data"]["create_item"]["id"]
created_items.append(item_id)
print(
f"Created item with ID: {item_id} for group {group_id}, title: {group_title}"
)
update_all_columns(
item_id, "8198737855", note, board_id_from_filename
)
else:
error_message = response_data.get(
"errors", [{"message": "Unknown error"}]
)[0]["message"]
print(
f"Error creating item for {filename}, note {note.get('item_name')}: {error_message}"
)
except requests.exceptions.RequestException as e:
print(f"Request error for {filename}: {str(e)}")
except Exception as e:
print(f"Unexpected error processing note in {filename}: {str(e)}")
except json.JSONDecodeError as e:
print(f"Error parsing JSON in {filename}: {str(e)}")
except Exception as e:
print(f"Error processing file {filename}: {str(e)}")
return created_items
def main():
dir_path = "Output_units/" # Update this to your directory path
all_created_items = []
for filename in os.listdir(dir_path):
if filename.endswith(".json"):
full_path = os.path.join(dir_path, filename)
print(f"\nProcessing {filename}...")
created_items = process_json_file(full_path)
all_created_items.extend(created_items)
print("\nAll created item IDs in order:")
for item_id in all_created_items:
print(item_id)
print(f"\nTotal items created: {len(all_created_items)}")
if __name__ == "__main__":
main()