api.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # vi:ts=4:et
  4. # Wekan API Python CLI, originally from here, where is more details:
  5. # https://github.com/wekan/wekan/wiki/New-card-with-Python3-and-REST-API
  6. # TODO:
  7. # addcustomfieldtoboard: There is error: Settings must be object. So adding does not work yet.
  8. try:
  9. # python 3
  10. from urllib.parse import urlencode
  11. except ImportError:
  12. # python 2
  13. from urllib import urlencode
  14. import json
  15. import requests
  16. import sys
  17. arguments = len(sys.argv) - 1
  18. syntax = """=== Wekan API Python CLI: Shows IDs for addcard ===
  19. # AUTHORID is USERID that writes card or custom field.
  20. If *nix: chmod +x api.py => ./api.py users
  21. Syntax:
  22. User API:
  23. python3 api.py user # Current user and list of current user boards
  24. python3 api.py boards USERID # Boards of USERID
  25. python3 api.py swimlanes BOARDID # Swimlanes of BOARDID
  26. python3 api.py lists BOARDID # Lists of BOARDID
  27. python3 api.py list BOARDID LISTID # Info of LISTID
  28. python3 api.py createlist BOARDID LISTTITLE # Create list
  29. python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  30. python3 api.py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION
  31. python3 api.py customfields BOARDID # Custom Fields of BOARDID
  32. python3 api.py customfield BOARDID CUSTOMFIELDID # Info of CUSTOMFIELDID
  33. python3 api.py addcustomfieldtoboard AUTHORID BOARDID NAME TYPE SETTINGS SHOWONCARD AUTOMATICALLYONCARD SHOWLABELONMINICARD SHOWSUMATTOPOFLIST # Add Custom Field to Board
  34. python3 api.py editcustomfield BOARDID LISTID CARDID CUSTOMFIELDID NEWCUSTOMFIELDVALUE # Edit Custom Field
  35. python3 api.py listattachments BOARDID # List attachments
  36. python3 api.py cardsbyswimlane SWIMLANEID LISTID # Retrieve cards list on a swimlane
  37. python3 api.py getcard BOARDID LISTID CARDID # Get card info
  38. python3 api.py addlabel BOARDID LISTID CARDID LABELID # Add label to a card
  39. python3 api.py addcardwithlabel AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION LABELIDS # Add a card and a label
  40. python3 api.py editboardtitle BOARDID NEWBOARDTITLE # Edit board title
  41. python3 api.py copyboard BOARDID NEWBOARDTITLE # Copy a board
  42. python3 api.py createlabel BOARDID LABELCOLOR LABELNAME (Color available: `white`, `green`, `yellow`, `orange`, `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`, `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`, `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`, `indigo`) # Create a new label
  43. python3 api.py editcardcolor BOARDID LISTID CARDID COLOR (Color available: `white`, `green`, `yellow`, `orange`, `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`, `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`, `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`, `indigo`) # Edit card color
  44. python3 api.py addchecklist BOARDID CARDID TITLE ITEM1 ITEM2 ITEM3 ITEM4 (You can add multiple items or just one, or also without any item, just TITLE works as well. * If items or Title contains spaces, you should add ' between them.) # Add checklist + item on a card
  45. python3 api.py deleteallcards BOARDID SWIMLANEID ( * Be careful will delete ALL CARDS INSIDE the swimlanes automatically in every list * ) # Delete all cards on a swimlane
  46. python3 api.py checklistid BOARDID CARDID # Retrieve Checklist ID attached to a card
  47. python3 api.py checklistinfo BOARDID CARDID CHECKLISTID # Get checklist info
  48. python3 api.py get_list_cards_count BOARDID LISTID # Retrieve how many cards in a list
  49. python3 api.py get_board_cards_count BOARDID # Retrieve how many cards in a board
  50. Admin API:
  51. python3 api.py users # All users
  52. python3 api.py boards # All Public Boards
  53. python3 api.py newuser USERNAME EMAIL PASSWORD
  54. """
  55. if arguments == 0:
  56. print(syntax)
  57. exit
  58. # TODO:
  59. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  60. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  61. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  62. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  63. # ------- SETTINGS START -------------
  64. # Username is your Wekan username or email address.
  65. # OIDC/OAuth2 etc uses email address as username.
  66. username = 'testtest'
  67. password = 'testtest'
  68. wekanurl = 'http://localhost:4000/'
  69. # ------- SETTINGS END -------------
  70. """
  71. === ADD CUSTOM FIELD TO BOARD ===
  72. Type: text, number, date, dropdown, checkbox, currency, stringtemplate.
  73. python3 api.py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 "SomeField" "date" "" true true true true
  74. === USERS ===
  75. python3 api.py users
  76. => abcd1234
  77. === BOARDS ===
  78. python3 api.py boards abcd1234
  79. === SWIMLANES ===
  80. python3 api.py swimlanes dYZ
  81. [{"_id":"Jiv","title":"Default"}
  82. ]
  83. === LISTS ===
  84. python3 api.py lists dYZ
  85. []
  86. There is no lists, so create a list:
  87. === CREATE LIST ===
  88. python3 api.py createlist dYZ 'Test'
  89. {"_id":"7Kp"}
  90. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  91. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  92. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  93. python3 api.py listattachments BOARDID
  94. """
  95. # ------- API URL GENERATION START -----------
  96. loginurl = 'users/login'
  97. wekanloginurl = wekanurl + loginurl
  98. apiboards = 'api/boards/'
  99. apiattachments = 'api/attachments/'
  100. apiusers = 'api/users'
  101. apiuser = 'api/user'
  102. apiallusers = 'api/allusers'
  103. e = 'export'
  104. s = '/'
  105. l = 'lists'
  106. sw = 'swimlane'
  107. sws = 'swimlanes'
  108. cs = 'cards'
  109. cf = 'custom-fields'
  110. bs = 'boards'
  111. apbs = 'allpublicboards'
  112. atl = 'attachmentslist'
  113. at = 'attachment'
  114. ats = 'attachments'
  115. users = wekanurl + apiusers
  116. user = wekanurl + apiuser
  117. allusers = wekanurl + apiallusers
  118. # ------- API URL GENERATION END -----------
  119. # ------- LOGIN TOKEN START -----------
  120. data = {"username": username, "password": password}
  121. body = requests.post(wekanloginurl, json=data)
  122. d = body.json()
  123. apikey = d['token']
  124. # ------- LOGIN TOKEN END -----------
  125. if arguments == 10:
  126. if sys.argv[1] == 'addcustomfieldtoboard':
  127. # ------- ADD CUSTOM FIELD TO BOARD START -----------
  128. authorid = sys.argv[2]
  129. boardid = sys.argv[3]
  130. name = sys.argv[4]
  131. type1 = sys.argv[5]
  132. settings = str(json.loads(sys.argv[6]))
  133. # There is error: Settings must be object. So this does not work yet.
  134. #settings = {'currencyCode': 'EUR'}
  135. print(type(settings))
  136. showoncard = sys.argv[7]
  137. automaticallyoncard = sys.argv[8]
  138. showlabelonminicard = sys.argv[9]
  139. showsumattopoflist = sys.argv[10]
  140. customfieldtoboard = wekanurl + apiboards + boardid + s + cf
  141. # Add Custom Field to Board
  142. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  143. post_data = {'authorId': '{}'.format(authorid), 'name': '{}'.format(name), 'type': '{}'.format(type1), 'settings': '{}'.format(settings), 'showoncard': '{}'.format(showoncard), 'automaticallyoncard': '{}'.format(automaticallyoncard), 'showlabelonminicard': '{}'.format(showlabelonminicard), 'showsumattopoflist': '{}'.format(showsumattopoflist)}
  144. body = requests.post(customfieldtoboard, data=post_data, headers=headers)
  145. print(body.text)
  146. # ------- ADD CUSTOM FIELD TO BOARD END -----------
  147. if arguments == 8:
  148. if sys.argv[1] == 'addcardwithlabel':
  149. # ------- ADD CARD WITH LABEL START -----------
  150. authorid = sys.argv[2]
  151. boardid = sys.argv[3]
  152. swimlaneid = sys.argv[4]
  153. listid = sys.argv[5]
  154. cardtitle = sys.argv[6]
  155. carddescription = sys.argv[7]
  156. labelIds = sys.argv[8] # Aggiunto labelIds
  157. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  158. # Add card
  159. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  160. post_data = {
  161. 'authorId': '{}'.format(authorid),
  162. 'title': '{}'.format(cardtitle),
  163. 'description': '{}'.format(carddescription),
  164. 'swimlaneId': '{}'.format(swimlaneid),
  165. 'labelIds': labelIds
  166. }
  167. body = requests.post(cardtolist, data=post_data, headers=headers)
  168. print(body.text)
  169. # If ok id card
  170. if body.status_code == 200:
  171. card_data = body.json()
  172. new_card_id = card_data.get('_id')
  173. # Updating card
  174. if new_card_id:
  175. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + new_card_id
  176. put_data = {'labelIds': labelIds}
  177. body = requests.put(edcard, data=put_data, headers=headers)
  178. print("=== EDIT CARD ===\n")
  179. body = requests.get(edcard, headers=headers)
  180. data2 = body.text.replace('}', "}\n")
  181. print(data2)
  182. else:
  183. print("Error obraining ID.")
  184. else:
  185. print("Error adding card.")
  186. # ------- ADD CARD WITH LABEL END -----------
  187. if arguments == 7:
  188. if sys.argv[1] == 'addcard':
  189. # ------- ADD CARD START -----------
  190. authorid = sys.argv[2]
  191. boardid = sys.argv[3]
  192. swimlaneid = sys.argv[4]
  193. listid = sys.argv[5]
  194. cardtitle = sys.argv[6]
  195. carddescription = sys.argv[7]
  196. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  197. # Add card
  198. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  199. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  200. body = requests.post(cardtolist, data=post_data, headers=headers)
  201. print(body.text)
  202. # ------- ADD CARD END -----------
  203. if arguments == 6:
  204. if sys.argv[1] == 'editcard':
  205. # ------- EDIT CARD START -----------
  206. boardid = sys.argv[2]
  207. listid = sys.argv[3]
  208. cardid = sys.argv[4]
  209. newcardtitle = sys.argv[5]
  210. newcarddescription = sys.argv[6]
  211. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  212. print(edcard)
  213. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  214. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  215. body = requests.put(edcard, data=put_data, headers=headers)
  216. print("=== EDIT CARD ===\n")
  217. body = requests.get(edcard, headers=headers)
  218. data2 = body.text.replace('}',"}\n")
  219. print(data2)
  220. # ------- EDIT CARD END -----------
  221. if sys.argv[1] == 'editcustomfield':
  222. # ------- EDIT CUSTOMFIELD START -----------
  223. boardid = sys.argv[2]
  224. listid = sys.argv[3]
  225. cardid = sys.argv[4]
  226. customfieldid = sys.argv[5]
  227. newcustomfieldvalue = sys.argv[6]
  228. edfield = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid + s + 'customFields' + s + customfieldid
  229. #print(edfield)
  230. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  231. post_data = {'_id': '{}'.format(customfieldid), 'value': '{}'.format(newcustomfieldvalue)}
  232. #print(post_data)
  233. body = requests.post(edfield, data=post_data, headers=headers)
  234. print("=== EDIT CUSTOMFIELD ===\n")
  235. data2 = body.text.replace('}',"}\n")
  236. print(data2)
  237. # ------- EDIT CUSTOMFIELD END -----------
  238. if arguments == 5:
  239. if sys.argv[1] == 'addlabel':
  240. # ------- EDIT CARD ADD LABEL START -----------
  241. boardid = sys.argv[2]
  242. listid = sys.argv[3]
  243. cardid = sys.argv[4]
  244. labelIds = sys.argv[5]
  245. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  246. print(edcard)
  247. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  248. put_data = {'labelIds': labelIds}
  249. body = requests.put(edcard, data=put_data, headers=headers)
  250. print("=== ADD LABEL ===\n")
  251. body = requests.get(edcard, headers=headers)
  252. data2 = body.text.replace('}',"}\n")
  253. print(data2)
  254. # ------- EDIT CARD ADD LABEL END -----------
  255. if sys.argv[1] == 'editcardcolor':
  256. # ------- EDIT CARD COLOR START -----------
  257. boardid = sys.argv[2]
  258. listid = sys.argv[3]
  259. cardid = sys.argv[4]
  260. newcolor = sys.argv[5]
  261. valid_colors = ['white', 'green', 'yellow', 'orange', 'red', 'purple', 'blue', 'sky', 'lime', 'pink', 'black',
  262. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen', 'slateblue', 'magenta', 'gold', 'navy',
  263. 'gray', 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo']
  264. if newcolor not in valid_colors:
  265. print("Invalid color. Choose a color from the list.")
  266. sys.exit(1)
  267. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  268. print(edcard)
  269. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  270. put_data = {'color': '{}'.format(newcolor)}
  271. body = requests.put(edcard, data=put_data, headers=headers)
  272. print("=== EDIT CARD COLOR ===\n")
  273. body = requests.get(edcard, headers=headers)
  274. data2 = body.text.replace('}', "}\n")
  275. print(data2)
  276. # ------- EDIT CARD COLOR END -----------
  277. if arguments >= 4:
  278. if sys.argv[1] == 'newuser':
  279. # ------- CREATE NEW USER START -----------
  280. username = sys.argv[2]
  281. email = sys.argv[3]
  282. password = sys.argv[4]
  283. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  284. post_data = {'username': '{}'.format(username),'email': '{}'.format(email),'password': '{}'.format(password)}
  285. body = requests.post(users, data=post_data, headers=headers)
  286. print("=== CREATE NEW USER ===\n")
  287. print(body.text)
  288. # ------- CREATE NEW USER END -----------
  289. if sys.argv[1] == 'getcard':
  290. # ------- LIST OF CARD START -----------
  291. boardid = sys.argv[2]
  292. listid = sys.argv[3]
  293. cardid = sys.argv[4]
  294. listone = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  295. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  296. print("=== INFO OF ONE LIST ===\n")
  297. print("URL:", listone) # Stampa l'URL per debug
  298. try:
  299. response = requests.get(listone, headers=headers)
  300. print("=== RESPONSE ===\n")
  301. print("Status Code:", response.status_code) # Stampa il codice di stato per debug
  302. if response.status_code == 200:
  303. data2 = response.text.replace('}', "}\n")
  304. print(data2)
  305. else:
  306. print(f"Error: {response.status_code}")
  307. print(f"Response: {response.text}")
  308. except Exception as e:
  309. print(f"Error in the GET request: {e}")
  310. # ------- LISTS OF CARD END -----------
  311. if sys.argv[1] == 'createlabel':
  312. # ------- CREATE LABEL START -----------
  313. boardid = sys.argv[2]
  314. labelcolor = sys.argv[3]
  315. labelname = sys.argv[4]
  316. label_url = wekanurl + apiboards + boardid + s + 'labels'
  317. print(label_url)
  318. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  319. # Object to send
  320. put_data = {'label': {'color': labelcolor, 'name': labelname}}
  321. print("URL:", label_url)
  322. print("Headers:", headers)
  323. print("Data:", put_data)
  324. try:
  325. response = requests.put(label_url, json=put_data, headers=headers)
  326. print("=== CREATE LABELS ===\n")
  327. print("Response Status Code:", response.status_code)
  328. print("Response Text:", response.text)
  329. except Exception as e:
  330. print("Error:", e)
  331. # ------- CREATE LABEL END -----------
  332. if sys.argv[1] == 'addchecklist':
  333. # ------- ADD CHECKLIST START -----------
  334. board_id = sys.argv[2]
  335. card_id = sys.argv[3]
  336. checklist_title = sys.argv[4]
  337. # Aggiungi la checklist
  338. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists'
  339. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  340. data = {'title': checklist_title}
  341. response = requests.post(checklist_url, data=data, headers=headers)
  342. response.raise_for_status()
  343. result = json.loads(response.text)
  344. checklist_id = result.get('_id')
  345. print(f"Checklist '{checklist_title}' created. ID: {checklist_id}")
  346. # Aggiungi gli items alla checklist
  347. items_to_add = sys.argv[5:]
  348. for item_title in items_to_add:
  349. checklist_item_url = wekanurl + apiboards + board_id + s + cs + s + card_id + s + 'checklists' + s + checklist_id + '/items'
  350. item_data = {'title': item_title}
  351. item_response = requests.post(checklist_item_url, data=item_data, headers=headers)
  352. item_response.raise_for_status()
  353. item_result = json.loads(item_response.text)
  354. checklist_item_id = item_result.get('_id')
  355. print(f"Item '{item_title}' added. ID: {checklist_item_id}")
  356. if sys.argv[1] == 'checklistinfo':
  357. # ------- ADD CHECKLIST START -----------
  358. board_id = sys.argv[2]
  359. card_id = sys.argv[3]
  360. checklist_id = sys.argv[4]
  361. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists' + s + checklist_id
  362. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  363. response = requests.get(checklist_url, headers=headers)
  364. response.raise_for_status()
  365. checklist_info = response.json()
  366. print("Checklist Info:")
  367. print(checklist_info)
  368. if arguments == 3:
  369. if sys.argv[1] == 'editboardtitle':
  370. # ------- EDIT BOARD TITLE START -----------
  371. boardid = sys.argv[2]
  372. boardtitle = sys.argv[3]
  373. edboardtitle = wekanurl + apiboards + boardid + s + 'title'
  374. print(edboardtitle)
  375. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  376. post_data = {'title': boardtitle}
  377. body = requests.put(edboardtitle, json=post_data, headers=headers)
  378. print("=== EDIT BOARD TITLE ===\n")
  379. #body = requests.get(edboardtitle, headers=headers)
  380. data2 = body.text.replace('}',"}\n")
  381. print(data2)
  382. if body.status_code == 200:
  383. print("Succesfull!")
  384. else:
  385. print(f"Error: {body.status_code}")
  386. print(body.text)
  387. # ------- EDIT BOARD TITLE END -----------
  388. if sys.argv[1] == 'copyboard':
  389. # ------- COPY BOARD START -----------
  390. boardid = sys.argv[2]
  391. boardtitle = sys.argv[3]
  392. edboardcopy = wekanurl + apiboards + boardid + s + 'copy'
  393. print(edboardcopy)
  394. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  395. post_data = {'title': boardtitle}
  396. body = requests.post(edboardcopy, json=post_data, headers=headers)
  397. print("=== COPY BOARD ===\n")
  398. #body = requests.get(edboardcopy, headers=headers)
  399. data2 = body.text.replace('}',"}\n")
  400. print(data2)
  401. if body.status_code == 200:
  402. print("Succesfull!")
  403. else:
  404. print(f"Error: {body.status_code}")
  405. print(body.text)
  406. # ------- COPY BOARD END -----------
  407. if sys.argv[1] == 'createlist':
  408. # ------- CREATE LIST START -----------
  409. boardid = sys.argv[2]
  410. listtitle = sys.argv[3]
  411. list = wekanurl + apiboards + boardid + s + l
  412. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  413. post_data = {'title': '{}'.format(listtitle)}
  414. body = requests.post(list, data=post_data, headers=headers)
  415. print("=== CREATE LIST ===\n")
  416. print(body.text)
  417. # ------- CREATE LIST END -----------
  418. if sys.argv[1] == 'list':
  419. # ------- LIST OF BOARD START -----------
  420. boardid = sys.argv[2]
  421. listid = sys.argv[3]
  422. listone = wekanurl + apiboards + boardid + s + l + s + listid
  423. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  424. print("=== INFO OF ONE LIST ===\n")
  425. body = requests.get(listone, headers=headers)
  426. data2 = body.text.replace('}',"}\n")
  427. print(data2)
  428. # ------- LISTS OF BOARD END -----------
  429. if sys.argv[1] == 'customfield':
  430. # ------- INFO OF CUSTOM FIELD START -----------
  431. boardid = sys.argv[2]
  432. customfieldid = sys.argv[3]
  433. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  434. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  435. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  436. body = requests.get(customfieldone, headers=headers)
  437. data2 = body.text.replace('}',"}\n")
  438. print(data2)
  439. # ------- INFO OF CUSTOM FIELD END -----------
  440. if sys.argv[1] == 'cardsbyswimlane':
  441. # ------- RETRIEVE CARDS BY SWIMLANE ID START -----------
  442. boardid = sys.argv[2]
  443. swimlaneid = sys.argv[3]
  444. cardsbyswimlane = wekanurl + apiboards + boardid + s + sws + s + swimlaneid + s + cs
  445. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  446. print("=== CARDS BY SWIMLANE ID ===\n")
  447. print("URL:", cardsbyswimlane) # Debug
  448. try:
  449. body = requests.get(cardsbyswimlane, headers=headers)
  450. print("Status Code:", body.status_code) # Debug
  451. data = body.text.replace('}', "}\n")
  452. print("Data:", data)
  453. except Exception as e:
  454. print("Error GET:", e)
  455. # ------- RETRIEVE CARDS BY SWIMLANE ID END -----------
  456. if sys.argv[1] == 'deleteallcards':
  457. boardid = sys.argv[2]
  458. swimlaneid = sys.argv[3]
  459. # ------- GET SWIMLANE CARDS START -----------
  460. get_swimlane_cards_url = wekanurl + apiboards + boardid + s + "swimlanes" + s + swimlaneid + s + "cards"
  461. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  462. try:
  463. response = requests.get(get_swimlane_cards_url, headers=headers)
  464. response.raise_for_status()
  465. cards_data = response.json()
  466. # Print the details of each card
  467. for card in cards_data:
  468. # ------- DELETE CARD START -----------
  469. delete_card_url = wekanurl + apiboards + boardid + s + "lists" + s + card['listId'] + s + "cards" + s + card['_id']
  470. try:
  471. response = requests.delete(delete_card_url, headers=headers)
  472. if response.status_code == 404:
  473. print(f"Card not found: {card['_id']}")
  474. else:
  475. response.raise_for_status()
  476. deleted_card_data = response.json()
  477. print(f"Card Deleted Successfully. Card ID: {deleted_card_data['_id']}")
  478. except requests.exceptions.RequestException as e:
  479. print(f"Error deleting card: {e}")
  480. # ------- DELETE CARD END -----------
  481. except requests.exceptions.RequestException as e:
  482. print(f"Error getting swimlane cards: {e}")
  483. sys.exit(1)
  484. # ------- GET SWIMLANE CARDS END -----------
  485. if sys.argv[1] == 'get_list_cards_count':
  486. # ------- GET LIST CARDS COUNT START -----------
  487. boardid = sys.argv[2]
  488. listid = sys.argv[3]
  489. get_list_cards_count_url = wekanurl + apiboards + boardid + s + l + s + listid + s + "cards_count"
  490. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  491. try:
  492. response = requests.get(get_list_cards_count_url, headers=headers)
  493. response.raise_for_status()
  494. data = response.json()
  495. print(f"List Cards Count: {data['list_cards_count']}")
  496. except requests.exceptions.RequestException as e:
  497. print(f"Error: {e}")
  498. # ------- GET LIST CARDS COUNT END -----------
  499. if sys.argv[1] == 'checklistid':
  500. # ------- ADD CHECKLIST START -----------
  501. board_id = sys.argv[2]
  502. card_id = sys.argv[3]
  503. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists'
  504. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  505. response = requests.get(checklist_url, headers=headers)
  506. response.raise_for_status()
  507. checklists = response.json()
  508. print("Checklists:")
  509. for checklist in checklists:
  510. print(checklist)
  511. if arguments == 2:
  512. # ------- BOARDS LIST START -----------
  513. userid = sys.argv[2]
  514. boards = users + s + userid + s + bs
  515. if sys.argv[1] == 'boards':
  516. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  517. #post_data = {'userId': '{}'.format(userid)}
  518. body = requests.get(boards, headers=headers)
  519. print("=== BOARDS ===\n")
  520. data2 = body.text.replace('}',"}\n")
  521. print(data2)
  522. # ------- BOARDS LIST END -----------
  523. if sys.argv[1] == 'board':
  524. # ------- BOARD INFO START -----------
  525. boardid = sys.argv[2]
  526. board = wekanurl + apiboards + boardid
  527. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  528. body = requests.get(board, headers=headers)
  529. print("=== BOARD ===\n")
  530. data2 = body.text.replace('}',"}\n")
  531. print(data2)
  532. # ------- BOARD INFO END -----------
  533. if sys.argv[1] == 'customfields':
  534. # ------- CUSTOM FIELDS OF BOARD START -----------
  535. boardid = sys.argv[2]
  536. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  537. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  538. body = requests.get(boardcustomfields, headers=headers)
  539. print("=== CUSTOM FIELDS OF BOARD ===\n")
  540. data2 = body.text.replace('}',"}\n")
  541. print(data2)
  542. # ------- CUSTOM FIELDS OF BOARD END -----------
  543. if sys.argv[1] == 'swimlanes':
  544. boardid = sys.argv[2]
  545. swimlanes = wekanurl + apiboards + boardid + s + sws
  546. # ------- SWIMLANES OF BOARD START -----------
  547. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  548. print("=== SWIMLANES ===\n")
  549. body = requests.get(swimlanes, headers=headers)
  550. data2 = body.text.replace('}',"}\n")
  551. print(data2)
  552. # ------- SWIMLANES OF BOARD END -----------
  553. if sys.argv[1] == 'lists':
  554. # ------- LISTS OF BOARD START -----------
  555. boardid = sys.argv[2]
  556. lists = wekanurl + apiboards + boardid + s + l
  557. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  558. print("=== LISTS ===\n")
  559. body = requests.get(lists, headers=headers)
  560. data2 = body.text.replace('}',"}\n")
  561. print(data2)
  562. # ------- LISTS OF BOARD END -----------
  563. if sys.argv[1] == 'listattachments':
  564. # ------- LISTS OF ATTACHMENTS START -----------
  565. boardid = sys.argv[2]
  566. listattachments = wekanurl + apiboards + boardid + s + ats
  567. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  568. print("=== LIST OF ATTACHMENTS ===\n")
  569. body = requests.get(listattachments, headers=headers)
  570. data2 = body.text.replace('}',"}\n")
  571. print(data2)
  572. # ------- LISTS OF ATTACHMENTS END -----------
  573. if sys.argv[1] == 'get_board_cards_count':
  574. # ------- GET BOARD CARDS COUNT START -----------
  575. boardid = sys.argv[2]
  576. get_board_cards_count_url = wekanurl + apiboards + boardid + s + "cards_count"
  577. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  578. try:
  579. response = requests.get(get_board_cards_count_url, headers=headers)
  580. response.raise_for_status()
  581. data = response.json()
  582. print(f"Board Cards Count: {data['board_cards_count']}")
  583. except requests.exceptions.RequestException as e:
  584. print(f"Error: {e}")
  585. # ------- GET BOARD CARDS COUNT END -----------
  586. if arguments == 1:
  587. if sys.argv[1] == 'users':
  588. # ------- LIST OF USERS START -----------
  589. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  590. print(users)
  591. print("=== USERS ===\n")
  592. body = requests.get(users, headers=headers)
  593. data2 = body.text.replace('}',"}\n")
  594. print(data2)
  595. # ------- LIST OF USERS END -----------
  596. if sys.argv[1] == 'user':
  597. # ------- LIST OF ALL USERS START -----------
  598. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  599. print(user)
  600. print("=== USER ===\n")
  601. body = requests.get(user, headers=headers)
  602. data2 = body.text.replace('}',"}\n")
  603. print(data2)
  604. # ------- LIST OF ALL USERS END -----------
  605. if sys.argv[1] == 'boards':
  606. # ------- LIST OF PUBLIC BOARDS START -----------
  607. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  608. print("=== PUBLIC BOARDS ===\n")
  609. listpublicboards = wekanurl + apiboards
  610. body = requests.get(listpublicboards, headers=headers)
  611. data2 = body.text.replace('}',"}\n")
  612. print(data2)
  613. # ------- LIST OF PUBLIC BOARDS END -----------