api.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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 uploadattachment BOARDID SWIMLANEID LISTID CARDID FILEPATH [STORAGE_BACKEND] # Upload attachment to card
  37. python3 api.py downloadattachment ATTACHMENTID OUTPUTPATH # Download attachment to local file
  38. python3 api.py attachmentinfo ATTACHMENTID # Get attachment information
  39. python3 api.py listcardattachments BOARDID SWIMLANEID LISTID CARDID # List attachments for specific card
  40. python3 api.py copymoveattachment ATTACHMENTID TARGETBOARDID TARGETSWIMLANEID TARGETLISTID TARGETCARDID [copy|move] # Copy or move attachment
  41. python3 api.py deleteattachment ATTACHMENTID # Delete attachment
  42. python3 api.py cardsbyswimlane SWIMLANEID LISTID # Retrieve cards list on a swimlane
  43. python3 api.py getcard BOARDID LISTID CARDID # Get card info
  44. python3 api.py addlabel BOARDID LISTID CARDID LABELID # Add label to a card
  45. python3 api.py addcardwithlabel AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION LABELIDS # Add a card and a label
  46. python3 api.py editboardtitle BOARDID NEWBOARDTITLE # Edit board title
  47. python3 api.py copyboard BOARDID NEWBOARDTITLE # Copy a board
  48. 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
  49. 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
  50. 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
  51. 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
  52. python3 api.py checklistid BOARDID CARDID # Retrieve Checklist ID attached to a card
  53. python3 api.py checklistinfo BOARDID CARDID CHECKLISTID # Get checklist info
  54. python3 api.py get_list_cards_count BOARDID LISTID # Retrieve how many cards in a list
  55. python3 api.py get_board_cards_count BOARDID # Retrieve how many cards in a board
  56. Admin API:
  57. python3 api.py users # All users
  58. python3 api.py boards # All Public Boards
  59. python3 api.py newuser USERNAME EMAIL PASSWORD
  60. """
  61. if arguments == 0:
  62. print(syntax)
  63. exit
  64. # TODO:
  65. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  66. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  67. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  68. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  69. # ------- SETTINGS START -------------
  70. # Username is your Wekan username or email address.
  71. # OIDC/OAuth2 etc uses email address as username.
  72. username = 'testtest'
  73. password = 'testtest'
  74. wekanurl = 'http://localhost:4000/'
  75. # ------- SETTINGS END -------------
  76. """
  77. === ADD CUSTOM FIELD TO BOARD ===
  78. Type: text, number, date, dropdown, checkbox, currency, stringtemplate.
  79. python3 api.py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 "SomeField" "date" "" true true true true
  80. === USERS ===
  81. python3 api.py users
  82. => abcd1234
  83. === BOARDS ===
  84. python3 api.py boards abcd1234
  85. === SWIMLANES ===
  86. python3 api.py swimlanes dYZ
  87. [{"_id":"Jiv","title":"Default"}
  88. ]
  89. === LISTS ===
  90. python3 api.py lists dYZ
  91. []
  92. There is no lists, so create a list:
  93. === CREATE LIST ===
  94. python3 api.py createlist dYZ 'Test'
  95. {"_id":"7Kp"}
  96. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  97. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  98. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  99. python3 api.py listattachments BOARDID
  100. """
  101. # ------- API URL GENERATION START -----------
  102. loginurl = 'users/login'
  103. wekanloginurl = wekanurl + loginurl
  104. apiboards = 'api/boards/'
  105. apiattachments = 'api/attachments/'
  106. apiusers = 'api/users'
  107. apiuser = 'api/user'
  108. apiallusers = 'api/allusers'
  109. e = 'export'
  110. s = '/'
  111. l = 'lists'
  112. sw = 'swimlane'
  113. sws = 'swimlanes'
  114. cs = 'cards'
  115. cf = 'custom-fields'
  116. bs = 'boards'
  117. apbs = 'allpublicboards'
  118. atl = 'attachmentslist'
  119. at = 'attachment'
  120. ats = 'attachments'
  121. users = wekanurl + apiusers
  122. user = wekanurl + apiuser
  123. allusers = wekanurl + apiallusers
  124. # ------- API URL GENERATION END -----------
  125. # ------- LOGIN TOKEN START -----------
  126. data = {"username": username, "password": password}
  127. body = requests.post(wekanloginurl, json=data)
  128. d = body.json()
  129. apikey = d['token']
  130. # ------- LOGIN TOKEN END -----------
  131. if arguments == 10:
  132. if sys.argv[1] == 'addcustomfieldtoboard':
  133. # ------- ADD CUSTOM FIELD TO BOARD START -----------
  134. authorid = sys.argv[2]
  135. boardid = sys.argv[3]
  136. name = sys.argv[4]
  137. type1 = sys.argv[5]
  138. settings = str(json.loads(sys.argv[6]))
  139. # There is error: Settings must be object. So this does not work yet.
  140. #settings = {'currencyCode': 'EUR'}
  141. print(type(settings))
  142. showoncard = sys.argv[7]
  143. automaticallyoncard = sys.argv[8]
  144. showlabelonminicard = sys.argv[9]
  145. showsumattopoflist = sys.argv[10]
  146. customfieldtoboard = wekanurl + apiboards + boardid + s + cf
  147. # Add Custom Field to Board
  148. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  149. 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)}
  150. body = requests.post(customfieldtoboard, data=post_data, headers=headers)
  151. print(body.text)
  152. # ------- ADD CUSTOM FIELD TO BOARD END -----------
  153. if arguments == 8:
  154. if sys.argv[1] == 'addcardwithlabel':
  155. # ------- ADD CARD WITH LABEL START -----------
  156. authorid = sys.argv[2]
  157. boardid = sys.argv[3]
  158. swimlaneid = sys.argv[4]
  159. listid = sys.argv[5]
  160. cardtitle = sys.argv[6]
  161. carddescription = sys.argv[7]
  162. labelIds = sys.argv[8] # Aggiunto labelIds
  163. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  164. # Add card
  165. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  166. post_data = {
  167. 'authorId': '{}'.format(authorid),
  168. 'title': '{}'.format(cardtitle),
  169. 'description': '{}'.format(carddescription),
  170. 'swimlaneId': '{}'.format(swimlaneid),
  171. 'labelIds': labelIds
  172. }
  173. body = requests.post(cardtolist, data=post_data, headers=headers)
  174. print(body.text)
  175. # If ok id card
  176. if body.status_code == 200:
  177. card_data = body.json()
  178. new_card_id = card_data.get('_id')
  179. # Updating card
  180. if new_card_id:
  181. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + new_card_id
  182. put_data = {'labelIds': labelIds}
  183. body = requests.put(edcard, data=put_data, headers=headers)
  184. print("=== EDIT CARD ===\n")
  185. body = requests.get(edcard, headers=headers)
  186. data2 = body.text.replace('}', "}\n")
  187. print(data2)
  188. else:
  189. print("Error obraining ID.")
  190. else:
  191. print("Error adding card.")
  192. # ------- ADD CARD WITH LABEL END -----------
  193. if arguments == 7:
  194. if sys.argv[1] == 'addcard':
  195. # ------- ADD CARD START -----------
  196. authorid = sys.argv[2]
  197. boardid = sys.argv[3]
  198. swimlaneid = sys.argv[4]
  199. listid = sys.argv[5]
  200. cardtitle = sys.argv[6]
  201. carddescription = sys.argv[7]
  202. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  203. # Add card
  204. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  205. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  206. body = requests.post(cardtolist, data=post_data, headers=headers)
  207. print(body.text)
  208. # ------- ADD CARD END -----------
  209. if arguments == 6:
  210. if sys.argv[1] == 'editcard':
  211. # ------- EDIT CARD START -----------
  212. boardid = sys.argv[2]
  213. listid = sys.argv[3]
  214. cardid = sys.argv[4]
  215. newcardtitle = sys.argv[5]
  216. newcarddescription = sys.argv[6]
  217. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  218. print(edcard)
  219. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  220. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  221. body = requests.put(edcard, data=put_data, headers=headers)
  222. print("=== EDIT CARD ===\n")
  223. body = requests.get(edcard, headers=headers)
  224. data2 = body.text.replace('}',"}\n")
  225. print(data2)
  226. # ------- EDIT CARD END -----------
  227. if sys.argv[1] == 'editcustomfield':
  228. # ------- EDIT CUSTOMFIELD START -----------
  229. boardid = sys.argv[2]
  230. listid = sys.argv[3]
  231. cardid = sys.argv[4]
  232. customfieldid = sys.argv[5]
  233. newcustomfieldvalue = sys.argv[6]
  234. edfield = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid + s + 'customFields' + s + customfieldid
  235. #print(edfield)
  236. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  237. post_data = {'_id': '{}'.format(customfieldid), 'value': '{}'.format(newcustomfieldvalue)}
  238. #print(post_data)
  239. body = requests.post(edfield, data=post_data, headers=headers)
  240. print("=== EDIT CUSTOMFIELD ===\n")
  241. data2 = body.text.replace('}',"}\n")
  242. print(data2)
  243. # ------- EDIT CUSTOMFIELD END -----------
  244. if arguments == 5:
  245. if sys.argv[1] == 'addlabel':
  246. # ------- EDIT CARD ADD LABEL START -----------
  247. boardid = sys.argv[2]
  248. listid = sys.argv[3]
  249. cardid = sys.argv[4]
  250. labelIds = sys.argv[5]
  251. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  252. print(edcard)
  253. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  254. put_data = {'labelIds': labelIds}
  255. body = requests.put(edcard, data=put_data, headers=headers)
  256. print("=== ADD LABEL ===\n")
  257. body = requests.get(edcard, headers=headers)
  258. data2 = body.text.replace('}',"}\n")
  259. print(data2)
  260. # ------- EDIT CARD ADD LABEL END -----------
  261. if sys.argv[1] == 'editcardcolor':
  262. # ------- EDIT CARD COLOR START -----------
  263. boardid = sys.argv[2]
  264. listid = sys.argv[3]
  265. cardid = sys.argv[4]
  266. newcolor = sys.argv[5]
  267. valid_colors = ['white', 'green', 'yellow', 'orange', 'red', 'purple', 'blue', 'sky', 'lime', 'pink', 'black',
  268. 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen', 'slateblue', 'magenta', 'gold', 'navy',
  269. 'gray', 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo']
  270. if newcolor not in valid_colors:
  271. print("Invalid color. Choose a color from the list.")
  272. sys.exit(1)
  273. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  274. print(edcard)
  275. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  276. put_data = {'color': '{}'.format(newcolor)}
  277. body = requests.put(edcard, data=put_data, headers=headers)
  278. print("=== EDIT CARD COLOR ===\n")
  279. body = requests.get(edcard, headers=headers)
  280. data2 = body.text.replace('}', "}\n")
  281. print(data2)
  282. # ------- EDIT CARD COLOR END -----------
  283. if arguments >= 4:
  284. if sys.argv[1] == 'newuser':
  285. # ------- CREATE NEW USER START -----------
  286. username = sys.argv[2]
  287. email = sys.argv[3]
  288. password = sys.argv[4]
  289. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  290. post_data = {'username': '{}'.format(username),'email': '{}'.format(email),'password': '{}'.format(password)}
  291. body = requests.post(users, data=post_data, headers=headers)
  292. print("=== CREATE NEW USER ===\n")
  293. print(body.text)
  294. # ------- CREATE NEW USER END -----------
  295. if sys.argv[1] == 'getcard':
  296. # ------- LIST OF CARD START -----------
  297. boardid = sys.argv[2]
  298. listid = sys.argv[3]
  299. cardid = sys.argv[4]
  300. listone = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  301. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  302. print("=== INFO OF ONE LIST ===\n")
  303. print("URL:", listone) # Stampa l'URL per debug
  304. try:
  305. response = requests.get(listone, headers=headers)
  306. print("=== RESPONSE ===\n")
  307. print("Status Code:", response.status_code) # Stampa il codice di stato per debug
  308. if response.status_code == 200:
  309. data2 = response.text.replace('}', "}\n")
  310. print(data2)
  311. else:
  312. print(f"Error: {response.status_code}")
  313. print(f"Response: {response.text}")
  314. except Exception as e:
  315. print(f"Error in the GET request: {e}")
  316. # ------- LISTS OF CARD END -----------
  317. if sys.argv[1] == 'createlabel':
  318. # ------- CREATE LABEL START -----------
  319. boardid = sys.argv[2]
  320. labelcolor = sys.argv[3]
  321. labelname = sys.argv[4]
  322. label_url = wekanurl + apiboards + boardid + s + 'labels'
  323. print(label_url)
  324. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  325. # Object to send
  326. put_data = {'label': {'color': labelcolor, 'name': labelname}}
  327. print("URL:", label_url)
  328. print("Headers:", headers)
  329. print("Data:", put_data)
  330. try:
  331. response = requests.put(label_url, json=put_data, headers=headers)
  332. print("=== CREATE LABELS ===\n")
  333. print("Response Status Code:", response.status_code)
  334. print("Response Text:", response.text)
  335. except Exception as e:
  336. print("Error:", e)
  337. # ------- CREATE LABEL END -----------
  338. if sys.argv[1] == 'addchecklist':
  339. # ------- ADD CHECKLIST START -----------
  340. board_id = sys.argv[2]
  341. card_id = sys.argv[3]
  342. checklist_title = sys.argv[4]
  343. # Aggiungi la checklist
  344. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists'
  345. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  346. data = {'title': checklist_title}
  347. response = requests.post(checklist_url, data=data, headers=headers)
  348. response.raise_for_status()
  349. result = json.loads(response.text)
  350. checklist_id = result.get('_id')
  351. print(f"Checklist '{checklist_title}' created. ID: {checklist_id}")
  352. # Aggiungi gli items alla checklist
  353. items_to_add = sys.argv[5:]
  354. for item_title in items_to_add:
  355. checklist_item_url = wekanurl + apiboards + board_id + s + cs + s + card_id + s + 'checklists' + s + checklist_id + '/items'
  356. item_data = {'title': item_title}
  357. item_response = requests.post(checklist_item_url, data=item_data, headers=headers)
  358. item_response.raise_for_status()
  359. item_result = json.loads(item_response.text)
  360. checklist_item_id = item_result.get('_id')
  361. print(f"Item '{item_title}' added. ID: {checklist_item_id}")
  362. if sys.argv[1] == 'checklistinfo':
  363. # ------- ADD CHECKLIST START -----------
  364. board_id = sys.argv[2]
  365. card_id = sys.argv[3]
  366. checklist_id = sys.argv[4]
  367. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists' + s + checklist_id
  368. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  369. response = requests.get(checklist_url, headers=headers)
  370. response.raise_for_status()
  371. checklist_info = response.json()
  372. print("Checklist Info:")
  373. print(checklist_info)
  374. if arguments == 3:
  375. if sys.argv[1] == 'editboardtitle':
  376. # ------- EDIT BOARD TITLE START -----------
  377. boardid = sys.argv[2]
  378. boardtitle = sys.argv[3]
  379. edboardtitle = wekanurl + apiboards + boardid + s + 'title'
  380. print(edboardtitle)
  381. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  382. post_data = {'title': boardtitle}
  383. body = requests.put(edboardtitle, json=post_data, headers=headers)
  384. print("=== EDIT BOARD TITLE ===\n")
  385. #body = requests.get(edboardtitle, headers=headers)
  386. data2 = body.text.replace('}',"}\n")
  387. print(data2)
  388. if body.status_code == 200:
  389. print("Succesfull!")
  390. else:
  391. print(f"Error: {body.status_code}")
  392. print(body.text)
  393. # ------- EDIT BOARD TITLE END -----------
  394. if sys.argv[1] == 'copyboard':
  395. # ------- COPY BOARD START -----------
  396. boardid = sys.argv[2]
  397. boardtitle = sys.argv[3]
  398. edboardcopy = wekanurl + apiboards + boardid + s + 'copy'
  399. print(edboardcopy)
  400. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  401. post_data = {'title': boardtitle}
  402. body = requests.post(edboardcopy, json=post_data, headers=headers)
  403. print("=== COPY BOARD ===\n")
  404. #body = requests.get(edboardcopy, headers=headers)
  405. data2 = body.text.replace('}',"}\n")
  406. print(data2)
  407. if body.status_code == 200:
  408. print("Succesfull!")
  409. else:
  410. print(f"Error: {body.status_code}")
  411. print(body.text)
  412. # ------- COPY BOARD END -----------
  413. if sys.argv[1] == 'createlist':
  414. # ------- CREATE LIST START -----------
  415. boardid = sys.argv[2]
  416. listtitle = sys.argv[3]
  417. list = wekanurl + apiboards + boardid + s + l
  418. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  419. post_data = {'title': '{}'.format(listtitle)}
  420. body = requests.post(list, data=post_data, headers=headers)
  421. print("=== CREATE LIST ===\n")
  422. print(body.text)
  423. # ------- CREATE LIST END -----------
  424. if sys.argv[1] == 'list':
  425. # ------- LIST OF BOARD START -----------
  426. boardid = sys.argv[2]
  427. listid = sys.argv[3]
  428. listone = wekanurl + apiboards + boardid + s + l + s + listid
  429. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  430. print("=== INFO OF ONE LIST ===\n")
  431. body = requests.get(listone, headers=headers)
  432. data2 = body.text.replace('}',"}\n")
  433. print(data2)
  434. # ------- LISTS OF BOARD END -----------
  435. if sys.argv[1] == 'customfield':
  436. # ------- INFO OF CUSTOM FIELD START -----------
  437. boardid = sys.argv[2]
  438. customfieldid = sys.argv[3]
  439. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  440. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  441. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  442. body = requests.get(customfieldone, headers=headers)
  443. data2 = body.text.replace('}',"}\n")
  444. print(data2)
  445. # ------- INFO OF CUSTOM FIELD END -----------
  446. if sys.argv[1] == 'cardsbyswimlane':
  447. # ------- RETRIEVE CARDS BY SWIMLANE ID START -----------
  448. boardid = sys.argv[2]
  449. swimlaneid = sys.argv[3]
  450. cardsbyswimlane = wekanurl + apiboards + boardid + s + sws + s + swimlaneid + s + cs
  451. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  452. print("=== CARDS BY SWIMLANE ID ===\n")
  453. print("URL:", cardsbyswimlane) # Debug
  454. try:
  455. body = requests.get(cardsbyswimlane, headers=headers)
  456. print("Status Code:", body.status_code) # Debug
  457. data = body.text.replace('}', "}\n")
  458. print("Data:", data)
  459. except Exception as e:
  460. print("Error GET:", e)
  461. # ------- RETRIEVE CARDS BY SWIMLANE ID END -----------
  462. if sys.argv[1] == 'deleteallcards':
  463. boardid = sys.argv[2]
  464. swimlaneid = sys.argv[3]
  465. # ------- GET SWIMLANE CARDS START -----------
  466. get_swimlane_cards_url = wekanurl + apiboards + boardid + s + "swimlanes" + s + swimlaneid + s + "cards"
  467. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  468. try:
  469. response = requests.get(get_swimlane_cards_url, headers=headers)
  470. response.raise_for_status()
  471. cards_data = response.json()
  472. # Print the details of each card
  473. for card in cards_data:
  474. # ------- DELETE CARD START -----------
  475. delete_card_url = wekanurl + apiboards + boardid + s + "lists" + s + card['listId'] + s + "cards" + s + card['_id']
  476. try:
  477. response = requests.delete(delete_card_url, headers=headers)
  478. if response.status_code == 404:
  479. print(f"Card not found: {card['_id']}")
  480. else:
  481. response.raise_for_status()
  482. deleted_card_data = response.json()
  483. print(f"Card Deleted Successfully. Card ID: {deleted_card_data['_id']}")
  484. except requests.exceptions.RequestException as e:
  485. print(f"Error deleting card: {e}")
  486. # ------- DELETE CARD END -----------
  487. except requests.exceptions.RequestException as e:
  488. print(f"Error getting swimlane cards: {e}")
  489. sys.exit(1)
  490. # ------- GET SWIMLANE CARDS END -----------
  491. if sys.argv[1] == 'get_list_cards_count':
  492. # ------- GET LIST CARDS COUNT START -----------
  493. boardid = sys.argv[2]
  494. listid = sys.argv[3]
  495. get_list_cards_count_url = wekanurl + apiboards + boardid + s + l + s + listid + s + "cards_count"
  496. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  497. try:
  498. response = requests.get(get_list_cards_count_url, headers=headers)
  499. response.raise_for_status()
  500. data = response.json()
  501. print(f"List Cards Count: {data['list_cards_count']}")
  502. except requests.exceptions.RequestException as e:
  503. print(f"Error: {e}")
  504. # ------- GET LIST CARDS COUNT END -----------
  505. if sys.argv[1] == 'checklistid':
  506. # ------- ADD CHECKLIST START -----------
  507. board_id = sys.argv[2]
  508. card_id = sys.argv[3]
  509. checklist_url = wekanurl + apiboards + board_id + s + cs + s + card_id + '/checklists'
  510. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  511. response = requests.get(checklist_url, headers=headers)
  512. response.raise_for_status()
  513. checklists = response.json()
  514. print("Checklists:")
  515. for checklist in checklists:
  516. print(checklist)
  517. if arguments == 2:
  518. # ------- BOARDS LIST START -----------
  519. userid = sys.argv[2]
  520. boards = users + s + userid + s + bs
  521. if sys.argv[1] == 'boards':
  522. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  523. #post_data = {'userId': '{}'.format(userid)}
  524. body = requests.get(boards, headers=headers)
  525. print("=== BOARDS ===\n")
  526. data2 = body.text.replace('}',"}\n")
  527. print(data2)
  528. # ------- BOARDS LIST END -----------
  529. if sys.argv[1] == 'board':
  530. # ------- BOARD INFO START -----------
  531. boardid = sys.argv[2]
  532. board = wekanurl + apiboards + boardid
  533. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  534. body = requests.get(board, headers=headers)
  535. print("=== BOARD ===\n")
  536. data2 = body.text.replace('}',"}\n")
  537. print(data2)
  538. # ------- BOARD INFO END -----------
  539. if sys.argv[1] == 'customfields':
  540. # ------- CUSTOM FIELDS OF BOARD START -----------
  541. boardid = sys.argv[2]
  542. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  543. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  544. body = requests.get(boardcustomfields, headers=headers)
  545. print("=== CUSTOM FIELDS OF BOARD ===\n")
  546. data2 = body.text.replace('}',"}\n")
  547. print(data2)
  548. # ------- CUSTOM FIELDS OF BOARD END -----------
  549. if sys.argv[1] == 'swimlanes':
  550. boardid = sys.argv[2]
  551. swimlanes = wekanurl + apiboards + boardid + s + sws
  552. # ------- SWIMLANES OF BOARD START -----------
  553. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  554. print("=== SWIMLANES ===\n")
  555. body = requests.get(swimlanes, headers=headers)
  556. data2 = body.text.replace('}',"}\n")
  557. print(data2)
  558. # ------- SWIMLANES OF BOARD END -----------
  559. if sys.argv[1] == 'lists':
  560. # ------- LISTS OF BOARD START -----------
  561. boardid = sys.argv[2]
  562. lists = wekanurl + apiboards + boardid + s + l
  563. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  564. print("=== LISTS ===\n")
  565. body = requests.get(lists, headers=headers)
  566. data2 = body.text.replace('}',"}\n")
  567. print(data2)
  568. # ------- LISTS OF BOARD END -----------
  569. if sys.argv[1] == 'listattachments':
  570. # ------- LISTS OF ATTACHMENTS START -----------
  571. boardid = sys.argv[2]
  572. listattachments = wekanurl + apiboards + boardid + s + ats
  573. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  574. print("=== LIST OF ATTACHMENTS ===\n")
  575. body = requests.get(listattachments, headers=headers)
  576. data2 = body.text.replace('}',"}\n")
  577. print(data2)
  578. # ------- LISTS OF ATTACHMENTS END -----------
  579. if sys.argv[1] == 'get_board_cards_count':
  580. # ------- GET BOARD CARDS COUNT START -----------
  581. boardid = sys.argv[2]
  582. get_board_cards_count_url = wekanurl + apiboards + boardid + s + "cards_count"
  583. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  584. try:
  585. response = requests.get(get_board_cards_count_url, headers=headers)
  586. response.raise_for_status()
  587. data = response.json()
  588. print(f"Board Cards Count: {data['board_cards_count']}")
  589. except requests.exceptions.RequestException as e:
  590. print(f"Error: {e}")
  591. # ------- GET BOARD CARDS COUNT END -----------
  592. if arguments == 1:
  593. if sys.argv[1] == 'users':
  594. # ------- LIST OF USERS START -----------
  595. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  596. print(users)
  597. print("=== USERS ===\n")
  598. body = requests.get(users, headers=headers)
  599. data2 = body.text.replace('}',"}\n")
  600. print(data2)
  601. # ------- LIST OF USERS END -----------
  602. if sys.argv[1] == 'user':
  603. # ------- LIST OF ALL USERS START -----------
  604. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  605. print(user)
  606. print("=== USER ===\n")
  607. body = requests.get(user, headers=headers)
  608. data2 = body.text.replace('}',"}\n")
  609. print(data2)
  610. # ------- LIST OF ALL USERS END -----------
  611. if sys.argv[1] == 'boards':
  612. # ------- LIST OF PUBLIC BOARDS START -----------
  613. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  614. print("=== PUBLIC BOARDS ===\n")
  615. listpublicboards = wekanurl + apiboards
  616. body = requests.get(listpublicboards, headers=headers)
  617. data2 = body.text.replace('}',"}\n")
  618. print(data2)
  619. # ------- LIST OF PUBLIC BOARDS END -----------
  620. # ------- NEW ATTACHMENT API ENDPOINTS START -----------
  621. if sys.argv[1] == 'uploadattachment':
  622. # ------- UPLOAD ATTACHMENT START -----------
  623. if arguments < 5:
  624. print("Usage: python3 api.py uploadattachment BOARDID SWIMLANEID LISTID CARDID FILEPATH [STORAGE_BACKEND]")
  625. print("Storage backends: fs, gridfs, s3")
  626. exit(1)
  627. boardid = sys.argv[2]
  628. swimlaneid = sys.argv[3]
  629. listid = sys.argv[4]
  630. cardid = sys.argv[5]
  631. filepath = sys.argv[6]
  632. storage_backend = sys.argv[7] if arguments > 6 else None
  633. # Read file and convert to base64
  634. try:
  635. with open(filepath, 'rb') as f:
  636. file_data = f.read()
  637. import base64
  638. base64_data = base64.b64encode(file_data).decode('utf-8')
  639. except FileNotFoundError:
  640. print(f"Error: File '{filepath}' not found")
  641. exit(1)
  642. except Exception as e:
  643. print(f"Error reading file: {e}")
  644. exit(1)
  645. # Get file info
  646. import os
  647. filename = os.path.basename(filepath)
  648. import mimetypes
  649. file_type = mimetypes.guess_type(filepath)[0] or 'application/octet-stream'
  650. # Prepare request data
  651. upload_data = {
  652. 'boardId': boardid,
  653. 'swimlaneId': swimlaneid,
  654. 'listId': listid,
  655. 'cardId': cardid,
  656. 'fileData': base64_data,
  657. 'fileName': filename,
  658. 'fileType': file_type
  659. }
  660. if storage_backend:
  661. upload_data['storageBackend'] = storage_backend
  662. # Make API call
  663. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey), 'Content-Type': 'application/json'}
  664. upload_url = wekanurl + 'api/attachment/upload'
  665. try:
  666. response = requests.post(upload_url, headers=headers, json=upload_data)
  667. response.raise_for_status()
  668. result = response.json()
  669. print(f"Upload successful!")
  670. print(f"Attachment ID: {result.get('attachmentId')}")
  671. print(f"File: {result.get('fileName')}")
  672. print(f"Size: {result.get('fileSize')} bytes")
  673. print(f"Storage: {result.get('storageBackend')}")
  674. except requests.exceptions.RequestException as e:
  675. print(f"Upload failed: {e}")
  676. if hasattr(e, 'response') and e.response is not None:
  677. print(f"Response: {e.response.text}")
  678. # ------- UPLOAD ATTACHMENT END -----------
  679. if sys.argv[1] == 'downloadattachment':
  680. # ------- DOWNLOAD ATTACHMENT START -----------
  681. if arguments < 3:
  682. print("Usage: python3 api.py downloadattachment ATTACHMENTID OUTPUTPATH")
  683. exit(1)
  684. attachmentid = sys.argv[2]
  685. outputpath = sys.argv[3]
  686. # Make API call
  687. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  688. download_url = wekanurl + f'api/attachment/download/{attachmentid}'
  689. try:
  690. response = requests.get(download_url, headers=headers)
  691. response.raise_for_status()
  692. result = response.json()
  693. if result.get('success'):
  694. # Decode base64 data and save to file
  695. import base64
  696. file_data = base64.b64decode(result.get('base64Data'))
  697. with open(outputpath, 'wb') as f:
  698. f.write(file_data)
  699. print(f"Download successful!")
  700. print(f"File saved to: {outputpath}")
  701. print(f"Original filename: {result.get('fileName')}")
  702. print(f"Size: {result.get('fileSize')} bytes")
  703. print(f"Storage: {result.get('storageBackend')}")
  704. else:
  705. print(f"Download failed: {result.get('message', 'Unknown error')}")
  706. except requests.exceptions.RequestException as e:
  707. print(f"Download failed: {e}")
  708. if hasattr(e, 'response') and e.response is not None:
  709. print(f"Response: {e.response.text}")
  710. # ------- DOWNLOAD ATTACHMENT END -----------
  711. if sys.argv[1] == 'attachmentinfo':
  712. # ------- ATTACHMENT INFO START -----------
  713. if arguments < 2:
  714. print("Usage: python3 api.py attachmentinfo ATTACHMENTID")
  715. exit(1)
  716. attachmentid = sys.argv[2]
  717. # Make API call
  718. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  719. info_url = wekanurl + f'api/attachment/info/{attachmentid}'
  720. try:
  721. response = requests.get(info_url, headers=headers)
  722. response.raise_for_status()
  723. result = response.json()
  724. if result.get('success'):
  725. print("=== ATTACHMENT INFO ===")
  726. print(f"Attachment ID: {result.get('attachmentId')}")
  727. print(f"File Name: {result.get('fileName')}")
  728. print(f"File Size: {result.get('fileSize')} bytes")
  729. print(f"File Type: {result.get('fileType')}")
  730. print(f"Storage Backend: {result.get('storageBackend')}")
  731. print(f"Board ID: {result.get('boardId')}")
  732. print(f"Swimlane ID: {result.get('swimlaneId')}")
  733. print(f"List ID: {result.get('listId')}")
  734. print(f"Card ID: {result.get('cardId')}")
  735. print(f"Created At: {result.get('createdAt')}")
  736. print(f"Is Image: {result.get('isImage')}")
  737. print(f"Versions: {len(result.get('versions', []))}")
  738. else:
  739. print(f"Failed to get attachment info: {result.get('message', 'Unknown error')}")
  740. except requests.exceptions.RequestException as e:
  741. print(f"Request failed: {e}")
  742. if hasattr(e, 'response') and e.response is not None:
  743. print(f"Response: {e.response.text}")
  744. # ------- ATTACHMENT INFO END -----------
  745. if sys.argv[1] == 'listcardattachments':
  746. # ------- LIST CARD ATTACHMENTS START -----------
  747. if arguments < 5:
  748. print("Usage: python3 api.py listcardattachments BOARDID SWIMLANEID LISTID CARDID")
  749. exit(1)
  750. boardid = sys.argv[2]
  751. swimlaneid = sys.argv[3]
  752. listid = sys.argv[4]
  753. cardid = sys.argv[5]
  754. # Make API call
  755. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  756. list_url = wekanurl + f'api/attachment/list/{boardid}/{swimlaneid}/{listid}/{cardid}'
  757. try:
  758. response = requests.get(list_url, headers=headers)
  759. response.raise_for_status()
  760. result = response.json()
  761. if result.get('success'):
  762. attachments = result.get('attachments', [])
  763. print(f"=== CARD ATTACHMENTS ({len(attachments)}) ===")
  764. for attachment in attachments:
  765. print(f"ID: {attachment.get('attachmentId')}")
  766. print(f"Name: {attachment.get('fileName')}")
  767. print(f"Size: {attachment.get('fileSize')} bytes")
  768. print(f"Type: {attachment.get('fileType')}")
  769. print(f"Storage: {attachment.get('storageBackend')}")
  770. print(f"Created: {attachment.get('createdAt')}")
  771. print("---")
  772. else:
  773. print(f"Failed to list attachments: {result.get('message', 'Unknown error')}")
  774. except requests.exceptions.RequestException as e:
  775. print(f"Request failed: {e}")
  776. if hasattr(e, 'response') and e.response is not None:
  777. print(f"Response: {e.response.text}")
  778. # ------- LIST CARD ATTACHMENTS END -----------
  779. if sys.argv[1] == 'copymoveattachment':
  780. # ------- COPY/MOVE ATTACHMENT START -----------
  781. if arguments < 6:
  782. print("Usage: python3 api.py copymoveattachment ATTACHMENTID TARGETBOARDID TARGETSWIMLANEID TARGETLISTID TARGETCARDID [copy|move]")
  783. exit(1)
  784. attachmentid = sys.argv[2]
  785. targetboardid = sys.argv[3]
  786. targetswimlaneid = sys.argv[4]
  787. targetlistid = sys.argv[5]
  788. targetcardid = sys.argv[6]
  789. operation = sys.argv[7] if arguments > 6 else 'copy'
  790. if operation not in ['copy', 'move']:
  791. print("Operation must be 'copy' or 'move'")
  792. exit(1)
  793. # Prepare request data
  794. request_data = {
  795. 'attachmentId': attachmentid,
  796. 'targetBoardId': targetboardid,
  797. 'targetSwimlaneId': targetswimlaneid,
  798. 'targetListId': targetlistid,
  799. 'targetCardId': targetcardid
  800. }
  801. # Make API call
  802. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey), 'Content-Type': 'application/json'}
  803. api_url = wekanurl + f'api/attachment/{operation}'
  804. try:
  805. response = requests.post(api_url, headers=headers, json=request_data)
  806. response.raise_for_status()
  807. result = response.json()
  808. if result.get('success'):
  809. print(f"{operation.capitalize()} successful!")
  810. if operation == 'copy':
  811. print(f"Source Attachment ID: {result.get('sourceAttachmentId')}")
  812. print(f"New Attachment ID: {result.get('newAttachmentId')}")
  813. else:
  814. print(f"Attachment ID: {result.get('attachmentId')}")
  815. print(f"Source Board: {result.get('sourceBoardId')}")
  816. print(f"Target Board: {result.get('targetBoardId')}")
  817. print(f"File: {result.get('fileName')}")
  818. print(f"Size: {result.get('fileSize')} bytes")
  819. else:
  820. print(f"{operation.capitalize()} failed: {result.get('message', 'Unknown error')}")
  821. except requests.exceptions.RequestException as e:
  822. print(f"{operation.capitalize()} failed: {e}")
  823. if hasattr(e, 'response') and e.response is not None:
  824. print(f"Response: {e.response.text}")
  825. # ------- COPY/MOVE ATTACHMENT END -----------
  826. if sys.argv[1] == 'deleteattachment':
  827. # ------- DELETE ATTACHMENT START -----------
  828. if arguments < 2:
  829. print("Usage: python3 api.py deleteattachment ATTACHMENTID")
  830. exit(1)
  831. attachmentid = sys.argv[2]
  832. # Make API call
  833. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  834. delete_url = wekanurl + f'api/attachment/delete/{attachmentid}'
  835. try:
  836. response = requests.delete(delete_url, headers=headers)
  837. response.raise_for_status()
  838. result = response.json()
  839. if result.get('success'):
  840. print("Delete successful!")
  841. print(f"Attachment ID: {result.get('attachmentId')}")
  842. print(f"File: {result.get('fileName')}")
  843. else:
  844. print(f"Delete failed: {result.get('message', 'Unknown error')}")
  845. except requests.exceptions.RequestException as e:
  846. print(f"Delete failed: {e}")
  847. if hasattr(e, 'response') and e.response is not None:
  848. print(f"Response: {e.response.text}")
  849. # ------- DELETE ATTACHMENT END -----------
  850. # ------- NEW ATTACHMENT API ENDPOINTS END -----------