api.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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
  35. python3 api.py listattachments BOARDID # List attachments
  36. python3 api.py cardsbyswimlane SWIMLANEID LISTID
  37. python3 api.py getcard BOARDID LISTID CARDID
  38. python3 api.py addlabel BOARDID LISTID CARDID LABELID
  39. python3 api.py addcardwithlabel AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION LABELIDS
  40. python3 api.py editboardtitle BOARDID NEWBOARDTITLE
  41. 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`)
  42. 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`)
  43. 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.)
  44. python3 api.py deleteallcards BOARDID SWIMLANEID ( * Be careful will delete ALL CARDS INSIDE the swimlanes automatically in every list * )
  45. Admin API:
  46. python3 api.py users # All users
  47. python3 api.py boards # All Public Boards
  48. python3 api.py newuser USERNAME EMAIL PASSWORD
  49. """
  50. if arguments == 0:
  51. print(syntax)
  52. exit
  53. # TODO:
  54. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  55. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  56. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  57. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  58. # ------- SETTINGS START -------------
  59. # Username is your Wekan username or email address.
  60. # OIDC/OAuth2 etc uses email address as username.
  61. username = 'testtest'
  62. password = 'testtest'
  63. wekanurl = 'http://localhost:4000/'
  64. # ------- SETTINGS END -------------
  65. """
  66. === ADD CUSTOM FIELD TO BOARD ===
  67. Type: text, number, date, dropdown, checkbox, currency, stringtemplate.
  68. python3 api.py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 "SomeField" "date" "" true true true true
  69. === USERS ===
  70. python3 api.py users
  71. => abcd1234
  72. === BOARDS ===
  73. python3 api.py boards abcd1234
  74. === SWIMLANES ===
  75. python3 api.py swimlanes dYZ
  76. [{"_id":"Jiv","title":"Default"}
  77. ]
  78. === LISTS ===
  79. python3 api.py lists dYZ
  80. []
  81. There is no lists, so create a list:
  82. === CREATE LIST ===
  83. python3 api.py createlist dYZ 'Test'
  84. {"_id":"7Kp"}
  85. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  86. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  87. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  88. python3 api.py listattachments BOARDID
  89. """
  90. # ------- API URL GENERATION START -----------
  91. loginurl = 'users/login'
  92. wekanloginurl = wekanurl + loginurl
  93. apiboards = 'api/boards/'
  94. apiattachments = 'api/attachments/'
  95. apiusers = 'api/users'
  96. apiuser = 'api/user'
  97. apiallusers = 'api/allusers'
  98. e = 'export'
  99. s = '/'
  100. l = 'lists'
  101. sw = 'swimlane'
  102. sws = 'swimlanes'
  103. cs = 'cards'
  104. cf = 'custom-fields'
  105. bs = 'boards'
  106. apbs = 'allpublicboards'
  107. atl = 'attachmentslist'
  108. at = 'attachment'
  109. ats = 'attachments'
  110. users = wekanurl + apiusers
  111. user = wekanurl + apiuser
  112. allusers = wekanurl + apiallusers
  113. # ------- API URL GENERATION END -----------
  114. # ------- LOGIN TOKEN START -----------
  115. data = {"username": username, "password": password}
  116. body = requests.post(wekanloginurl, json=data)
  117. d = body.json()
  118. apikey = d['token']
  119. # ------- LOGIN TOKEN END -----------
  120. if arguments == 10:
  121. if sys.argv[1] == 'addcustomfieldtoboard':
  122. # ------- ADD CUSTOM FIELD TO BOARD START -----------
  123. authorid = sys.argv[2]
  124. boardid = sys.argv[3]
  125. name = sys.argv[4]
  126. type1 = sys.argv[5]
  127. settings = str(json.loads(sys.argv[6]))
  128. # There is error: Settings must be object. So this does not work yet.
  129. #settings = {'currencyCode': 'EUR'}
  130. print(type(settings))
  131. showoncard = sys.argv[7]
  132. automaticallyoncard = sys.argv[8]
  133. showlabelonminicard = sys.argv[9]
  134. showsumattopoflist = sys.argv[10]
  135. customfieldtoboard = wekanurl + apiboards + boardid + s + cf
  136. # Add Custom Field to Board
  137. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  138. 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)}
  139. body = requests.post(customfieldtoboard, data=post_data, headers=headers)
  140. print(body.text)
  141. # ------- ADD CUSTOM FIELD TO BOARD END -----------
  142. if arguments == 8:
  143. if sys.argv[1] == 'addcardwithlabel':
  144. # ------- ADD CARD WITH LABEL START -----------
  145. authorid = sys.argv[2]
  146. boardid = sys.argv[3]
  147. swimlaneid = sys.argv[4]
  148. listid = sys.argv[5]
  149. cardtitle = sys.argv[6]
  150. carddescription = sys.argv[7]
  151. labelIds = sys.argv[8] # Aggiunto labelIds
  152. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  153. # Add card
  154. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  155. post_data = {
  156. 'authorId': '{}'.format(authorid),
  157. 'title': '{}'.format(cardtitle),
  158. 'description': '{}'.format(carddescription),
  159. 'swimlaneId': '{}'.format(swimlaneid),
  160. 'labelIds': labelIds
  161. }
  162. body = requests.post(cardtolist, data=post_data, headers=headers)
  163. print(body.text)
  164. # If ok id card
  165. if body.status_code == 200:
  166. card_data = body.json()
  167. new_card_id = card_data.get('_id')
  168. # Updating card
  169. if new_card_id:
  170. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + new_card_id
  171. put_data = {'labelIds': labelIds}
  172. body = requests.put(edcard, data=put_data, headers=headers)
  173. print("=== EDIT CARD ===\n")
  174. body = requests.get(edcard, headers=headers)
  175. data2 = body.text.replace('}', "}\n")
  176. print(data2)
  177. else:
  178. print("Error obraining ID.")
  179. else:
  180. print("Error adding card.")
  181. # ------- ADD CARD WITH LABEL END -----------
  182. if arguments == 7:
  183. if sys.argv[1] == 'addcard':
  184. # ------- ADD CARD START -----------
  185. authorid = sys.argv[2]
  186. boardid = sys.argv[3]
  187. swimlaneid = sys.argv[4]
  188. listid = sys.argv[5]
  189. cardtitle = sys.argv[6]
  190. carddescription = sys.argv[7]
  191. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  192. # Add card
  193. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  194. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  195. body = requests.post(cardtolist, data=post_data, headers=headers)
  196. print(body.text)
  197. # ------- ADD CARD END -----------
  198. if arguments == 6:
  199. if sys.argv[1] == 'editcard':
  200. # ------- EDIT CARD START -----------
  201. boardid = sys.argv[2]
  202. listid = sys.argv[3]
  203. cardid = sys.argv[4]
  204. newcardtitle = sys.argv[5]
  205. newcarddescription = sys.argv[6]
  206. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  207. print(edcard)
  208. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  209. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  210. body = requests.put(edcard, data=put_data, headers=headers)
  211. print("=== EDIT CARD ===\n")
  212. body = requests.get(edcard, headers=headers)
  213. data2 = body.text.replace('}',"}\n")
  214. print(data2)
  215. # ------- EDIT CARD END -----------
  216. if sys.argv[1] == 'editcustomfield':
  217. # ------- EDIT CUSTOMFIELD START -----------
  218. boardid = sys.argv[2]
  219. listid = sys.argv[3]
  220. cardid = sys.argv[4]
  221. customfieldid = sys.argv[5]
  222. newcustomfieldvalue = sys.argv[6]
  223. edfield = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid + s + 'customFields' + s + customfieldid
  224. #print(edfield)
  225. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  226. post_data = {'_id': '{}'.format(customfieldid), 'value': '{}'.format(newcustomfieldvalue)}
  227. #print(post_data)
  228. body = requests.post(edfield, data=post_data, headers=headers)
  229. print("=== EDIT CUSTOMFIELD ===\n")
  230. data2 = body.text.replace('}',"}\n")
  231. print(data2)
  232. # ------- EDIT CUSTOMFIELD END -----------
  233. if arguments == 5:
  234. if sys.argv[1] == 'addlabel':
  235. # ------- EDIT CARD ADD LABEL START -----------
  236. boardid = sys.argv[2]
  237. listid = sys.argv[3]
  238. cardid = sys.argv[4]
  239. labelIds = sys.argv[5]
  240. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  241. print(edcard)
  242. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  243. put_data = {'labelIds': labelIds}
  244. body = requests.put(edcard, data=put_data, headers=headers)
  245. print("=== ADD LABEL ===\n")
  246. body = requests.get(edcard, headers=headers)
  247. data2 = body.text.replace('}',"}\n")
  248. print(data2)
  249. # ------- EDIT CARD ADD LABEL END -----------
  250. if sys.argv[1] == 'editcardcolor':
  251. # ------- EDIT CARD COLOR START -----------
  252. boardid = sys.argv[2]
  253. listid = sys.argv[3]
  254. cardid = sys.argv[4]
  255. newcolor = sys.argv[5]
  256. valid_colors = ['white', 'green', 'yellow', 'orange', 'red', 'purple', 'blue', 'sky', 'lime', 'pink', 'black',
  257. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen', 'slateblue', 'magenta', 'gold', 'navy',
  258. 'gray', 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo']
  259. if newcolor not in valid_colors:
  260. print("Invalid color. Choose a color from the list.")
  261. sys.exit(1)
  262. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  263. print(edcard)
  264. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  265. put_data = {'color': '{}'.format(newcolor)}
  266. body = requests.put(edcard, data=put_data, headers=headers)
  267. print("=== EDIT CARD COLOR ===\n")
  268. body = requests.get(edcard, headers=headers)
  269. data2 = body.text.replace('}', "}\n")
  270. print(data2)
  271. # ------- EDIT CARD COLOR END -----------
  272. if arguments >= 4:
  273. if sys.argv[1] == 'newuser':
  274. # ------- CREATE NEW USER START -----------
  275. username = sys.argv[2]
  276. email = sys.argv[3]
  277. password = sys.argv[4]
  278. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  279. post_data = {'username': '{}'.format(username),'email': '{}'.format(email),'password': '{}'.format(password)}
  280. body = requests.post(users, data=post_data, headers=headers)
  281. print("=== CREATE NEW USER ===\n")
  282. print(body.text)
  283. # ------- CREATE NEW USER END -----------
  284. if sys.argv[1] == 'getcard':
  285. # ------- LIST OF CARD START -----------
  286. boardid = sys.argv[2]
  287. listid = sys.argv[3]
  288. cardid = sys.argv[4]
  289. listone = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  290. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  291. print("=== INFO OF ONE LIST ===\n")
  292. print("URL:", listone) # Stampa l'URL per debug
  293. try:
  294. response = requests.get(listone, headers=headers)
  295. print("=== RESPONSE ===\n")
  296. print("Status Code:", response.status_code) # Stampa il codice di stato per debug
  297. if response.status_code == 200:
  298. data2 = response.text.replace('}', "}\n")
  299. print(data2)
  300. else:
  301. print(f"Error: {response.status_code}")
  302. print(f"Response: {response.text}")
  303. except Exception as e:
  304. print(f"Error in the GET request: {e}")
  305. # ------- LISTS OF CARD END -----------
  306. if sys.argv[1] == 'createlabel':
  307. # ------- CREATE LABEL START -----------
  308. boardid = sys.argv[2]
  309. labelcolor = sys.argv[3]
  310. labelname = sys.argv[4]
  311. label_url = wekanurl + apiboards + boardid + s + 'labels'
  312. print(label_url)
  313. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  314. # Object to send
  315. put_data = {'label': {'color': labelcolor, 'name': labelname}}
  316. print("URL:", label_url)
  317. print("Headers:", headers)
  318. print("Data:", put_data)
  319. try:
  320. response = requests.put(label_url, json=put_data, headers=headers)
  321. print("=== CREATE LABELS ===\n")
  322. print("Response Status Code:", response.status_code)
  323. print("Response Text:", response.text)
  324. except Exception as e:
  325. print("Error:", e)
  326. # ------- CREATE LABEL END -----------
  327. if sys.argv[1] == 'addchecklist':
  328. # ------- ADD CHECKLIST START -----------
  329. board_id = sys.argv[2]
  330. card_id = sys.argv[3]
  331. checklist_title = sys.argv[4]
  332. # Aggiungi la checklist
  333. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists'
  334. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  335. data = {'title': checklist_title}
  336. response = requests.post(checklist_url, data=data, headers=headers)
  337. response.raise_for_status()
  338. result = json.loads(response.text)
  339. checklist_id = result.get('_id')
  340. print(f"Checklist '{checklist_title}' created. ID: {checklist_id}")
  341. # Aggiungi gli items alla checklist
  342. items_to_add = sys.argv[5:] # Gli argomenti successivi sono considerati come titoli degli items
  343. for item_title in items_to_add:
  344. checklist_item_url = wekanurl + apiboards + board_id + s + cs + s + card_id + s + 'checklists' + s + checklist_id + '/items'
  345. item_data = {'title': item_title}
  346. item_response = requests.post(checklist_item_url, data=item_data, headers=headers)
  347. item_response.raise_for_status()
  348. item_result = json.loads(item_response.text)
  349. checklist_item_id = item_result.get('_id')
  350. print(f"Item '{item_title}' added. ID: {checklist_item_id}")
  351. if arguments == 3:
  352. if sys.argv[1] == 'editboardtitle':
  353. # ------- EDIT BOARD TITLE START -----------
  354. boardid = sys.argv[2]
  355. boardtitle = sys.argv[3]
  356. edboardtitle = wekanurl + apiboards + boardid + s + 'title'
  357. print(edboardtitle)
  358. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  359. post_data = {'title': boardtitle}
  360. body = requests.put(edboardtitle, json=post_data, headers=headers)
  361. print("=== EDIT BOARD TITLE ===\n")
  362. #body = requests.get(edboardtitle, headers=headers)
  363. data2 = body.text.replace('}',"}\n")
  364. print(data2)
  365. if body.status_code == 200:
  366. print("Succesfull!")
  367. else:
  368. print(f"Error: {body.status_code}")
  369. print(body.text)
  370. # ------- EDIT BOARD TITLE END -----------
  371. if sys.argv[1] == 'createlist':
  372. # ------- CREATE LIST START -----------
  373. boardid = sys.argv[2]
  374. listtitle = sys.argv[3]
  375. list = wekanurl + apiboards + boardid + s + l
  376. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  377. post_data = {'title': '{}'.format(listtitle)}
  378. body = requests.post(list, data=post_data, headers=headers)
  379. print("=== CREATE LIST ===\n")
  380. print(body.text)
  381. # ------- CREATE LIST END -----------
  382. if sys.argv[1] == 'list':
  383. # ------- LIST OF BOARD START -----------
  384. boardid = sys.argv[2]
  385. listid = sys.argv[3]
  386. listone = wekanurl + apiboards + boardid + s + l + s + listid
  387. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  388. print("=== INFO OF ONE LIST ===\n")
  389. body = requests.get(listone, headers=headers)
  390. data2 = body.text.replace('}',"}\n")
  391. print(data2)
  392. # ------- LISTS OF BOARD END -----------
  393. if sys.argv[1] == 'customfield':
  394. # ------- INFO OF CUSTOM FIELD START -----------
  395. boardid = sys.argv[2]
  396. customfieldid = sys.argv[3]
  397. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  398. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  399. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  400. body = requests.get(customfieldone, headers=headers)
  401. data2 = body.text.replace('}',"}\n")
  402. print(data2)
  403. # ------- INFO OF CUSTOM FIELD END -----------
  404. if sys.argv[1] == 'cardsbyswimlane':
  405. # ------- RETRIEVE CARDS BY SWIMLANE ID START -----------
  406. boardid = sys.argv[2]
  407. swimlaneid = sys.argv[3]
  408. cardsbyswimlane = wekanurl + apiboards + boardid + s + sws + s + swimlaneid + s + cs
  409. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  410. print("=== CARDS BY SWIMLANE ID ===\n")
  411. print("URL:", cardsbyswimlane) # Debug
  412. try:
  413. body = requests.get(cardsbyswimlane, headers=headers)
  414. print("Status Code:", body.status_code) # Debug
  415. data = body.text.replace('}', "}\n")
  416. print("Data:", data)
  417. except Exception as e:
  418. print("Error GET:", e)
  419. # ------- RETRIEVE CARDS BY SWIMLANE ID END -----------
  420. if sys.argv[1] == 'deleteallcards':
  421. boardid = sys.argv[2]
  422. swimlaneid = sys.argv[3]
  423. # ------- GET SWIMLANE CARDS START -----------
  424. get_swimlane_cards_url = wekanurl + apiboards + boardid + s + "swimlanes" + s + swimlaneid + s + "cards"
  425. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  426. try:
  427. response = requests.get(get_swimlane_cards_url, headers=headers)
  428. response.raise_for_status()
  429. cards_data = response.json()
  430. # Print the details of each card
  431. for card in cards_data:
  432. # ------- DELETE CARD START -----------
  433. delete_card_url = wekanurl + apiboards + boardid + s + "lists" + s + card['listId'] + s + "cards" + s + card['_id']
  434. try:
  435. response = requests.delete(delete_card_url, headers=headers)
  436. response.raise_for_status()
  437. deleted_card_data = response.json()
  438. print(f"Card Deleted Successfully. Card ID: {deleted_card_data['_id']}")
  439. except requests.exceptions.RequestException as e:
  440. print(f"Error deleting card: {e}")
  441. # ------- DELETE CARD END -----------
  442. except requests.exceptions.RequestException as e:
  443. print(f"Error getting swimlane cards: {e}")
  444. sys.exit(1)
  445. # ------- GET SWIMLANE CARDS END -----------
  446. if arguments == 2:
  447. # ------- BOARDS LIST START -----------
  448. userid = sys.argv[2]
  449. boards = users + s + userid + s + bs
  450. if sys.argv[1] == 'boards':
  451. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  452. #post_data = {'userId': '{}'.format(userid)}
  453. body = requests.get(boards, headers=headers)
  454. print("=== BOARDS ===\n")
  455. data2 = body.text.replace('}',"}\n")
  456. print(data2)
  457. # ------- BOARDS LIST END -----------
  458. if sys.argv[1] == 'board':
  459. # ------- BOARD INFO START -----------
  460. boardid = sys.argv[2]
  461. board = wekanurl + apiboards + boardid
  462. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  463. body = requests.get(board, headers=headers)
  464. print("=== BOARD ===\n")
  465. data2 = body.text.replace('}',"}\n")
  466. print(data2)
  467. # ------- BOARD INFO END -----------
  468. if sys.argv[1] == 'customfields':
  469. # ------- CUSTOM FIELDS OF BOARD START -----------
  470. boardid = sys.argv[2]
  471. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  472. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  473. body = requests.get(boardcustomfields, headers=headers)
  474. print("=== CUSTOM FIELDS OF BOARD ===\n")
  475. data2 = body.text.replace('}',"}\n")
  476. print(data2)
  477. # ------- CUSTOM FIELDS OF BOARD END -----------
  478. if sys.argv[1] == 'swimlanes':
  479. boardid = sys.argv[2]
  480. swimlanes = wekanurl + apiboards + boardid + s + sws
  481. # ------- SWIMLANES OF BOARD START -----------
  482. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  483. print("=== SWIMLANES ===\n")
  484. body = requests.get(swimlanes, headers=headers)
  485. data2 = body.text.replace('}',"}\n")
  486. print(data2)
  487. # ------- SWIMLANES OF BOARD END -----------
  488. if sys.argv[1] == 'lists':
  489. # ------- LISTS OF BOARD START -----------
  490. boardid = sys.argv[2]
  491. lists = wekanurl + apiboards + boardid + s + l
  492. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  493. print("=== LISTS ===\n")
  494. body = requests.get(lists, headers=headers)
  495. data2 = body.text.replace('}',"}\n")
  496. print(data2)
  497. # ------- LISTS OF BOARD END -----------
  498. if sys.argv[1] == 'listattachments':
  499. # ------- LISTS OF ATTACHMENTS START -----------
  500. boardid = sys.argv[2]
  501. listattachments = wekanurl + apiboards + boardid + s + ats
  502. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  503. print("=== LIST OF ATTACHMENTS ===\n")
  504. body = requests.get(listattachments, headers=headers)
  505. data2 = body.text.replace('}',"}\n")
  506. print(data2)
  507. # ------- LISTS OF ATTACHMENTS END -----------
  508. if arguments == 1:
  509. if sys.argv[1] == 'users':
  510. # ------- LIST OF USERS START -----------
  511. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  512. print(users)
  513. print("=== USERS ===\n")
  514. body = requests.get(users, headers=headers)
  515. data2 = body.text.replace('}',"}\n")
  516. print(data2)
  517. # ------- LIST OF USERS END -----------
  518. if sys.argv[1] == 'user':
  519. # ------- LIST OF ALL USERS START -----------
  520. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  521. print(user)
  522. print("=== USER ===\n")
  523. body = requests.get(user, headers=headers)
  524. data2 = body.text.replace('}',"}\n")
  525. print(data2)
  526. # ------- LIST OF ALL USERS END -----------
  527. if sys.argv[1] == 'boards':
  528. # ------- LIST OF PUBLIC BOARDS START -----------
  529. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  530. print("=== PUBLIC BOARDS ===\n")
  531. listpublicboards = wekanurl + apiboards
  532. body = requests.get(listpublicboards, headers=headers)
  533. data2 = body.text.replace('}',"}\n")
  534. print(data2)
  535. # ------- LIST OF PUBLIC BOARDS END -----------