2020-12-03 06:54:25 +02:00
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vi:ts=4:et
# Wekan API Python CLI, originally from here, where is more details:
# https://github.com/wekan/wekan/wiki/New-card-with-Python3-and-REST-API
2021-10-16 19:07:36 +03:00
# TODO:
2021-10-19 08:27:28 +03:00
# addcustomfieldtoboard: There is error: Settings must be object. So adding does not work yet.
2021-10-16 19:07:36 +03:00
2020-12-03 06:54:25 +02:00
try :
# python 3
from urllib . parse import urlencode
except ImportError :
# python 2
from urllib import urlencode
import json
import requests
import sys
arguments = len ( sys . argv ) - 1
2023-01-13 21:50:39 +02:00
syntax = """ === Wekan API Python CLI: Shows IDs for addcard ===
# AUTHORID is USERID that writes card or custom field.
If * nix : chmod + x api . py = > . / api . py users
Syntax :
User API :
python3 api . py user # Current user and list of current user boards
python3 api . py boards USERID # Boards of USERID
python3 api . py swimlanes BOARDID # Swimlanes of BOARDID
python3 api . py lists BOARDID # Lists of BOARDID
python3 api . py list BOARDID LISTID # Info of LISTID
python3 api . py createlist BOARDID LISTTITLE # Create list
python3 api . py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
python3 api . py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION
python3 api . py customfields BOARDID # Custom Fields of BOARDID
python3 api . py customfield BOARDID CUSTOMFIELDID # Info of CUSTOMFIELDID
python3 api . py addcustomfieldtoboard AUTHORID BOARDID NAME TYPE SETTINGS SHOWONCARD AUTOMATICALLYONCARD SHOWLABELONMINICARD SHOWSUMATTOPOFLIST # Add Custom Field to Board
2023-05-18 09:27:59 -04:00
python3 api . py editcustomfield BOARDID LISTID CARDID CUSTOMFIELDID NEWCUSTOMFIELDVALUE
2023-01-13 21:50:39 +02:00
python3 api . py listattachments BOARDID # List attachments
2024-02-07 14:38:49 +01:00
python3 api . py cardsbyswimlane BOARDID LISTID
python3 api . py getcard BOARDID LISTID CARDID
2023-01-13 21:50:39 +02:00
Admin API :
python3 api . py users # All users
python3 api . py boards # All Public Boards
python3 api . py newuser USERNAME EMAIL PASSWORD
"""
2020-12-03 06:54:25 +02:00
if arguments == 0 :
2023-01-13 21:50:39 +02:00
print ( syntax )
exit
2020-12-03 06:54:25 +02:00
# TODO:
2021-06-01 12:41:54 +03:00
# print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
# print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
# print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
# print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
2020-12-03 06:54:25 +02:00
# ------- SETTINGS START -------------
# Username is your Wekan username or email address.
# OIDC/OAuth2 etc uses email address as username.
username = ' testtest '
password = ' testtest '
wekanurl = ' http://localhost:4000/ '
# ------- SETTINGS END -------------
"""
2021-10-16 19:07:36 +03:00
== = ADD CUSTOM FIELD TO BOARD == =
Type : text , number , date , dropdown , checkbox , currency , stringtemplate .
python3 api . py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 " SomeField " " date " " " true true true true
2020-12-03 06:54:25 +02:00
== = USERS == =
python3 api . py users
= > abcd1234
== = BOARDS == =
python3 api . py boards abcd1234
== = SWIMLANES == =
python3 api . py swimlanes dYZ
[ { " _id " : " Jiv " , " title " : " Default " }
]
== = LISTS == =
python3 api . py lists dYZ
[ ]
There is no lists , so create a list :
== = CREATE LIST == =
python3 api . py createlist dYZ ' Test '
{ " _id " : " 7Kp " }
# python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
python3 api . py addcard ppg dYZ Jiv 7 Kp ' Test card ' ' Test description '
== = LIST ATTACHMENTS WITH DOWNLOAD URLs == ==
python3 api . py listattachments BOARDID
"""
# ------- API URL GENERATION START -----------
loginurl = ' users/login '
wekanloginurl = wekanurl + loginurl
apiboards = ' api/boards/ '
apiattachments = ' api/attachments/ '
apiusers = ' api/users '
2023-01-13 21:50:39 +02:00
apiuser = ' api/user '
apiallusers = ' api/allusers '
2020-12-03 06:54:25 +02:00
e = ' export '
s = ' / '
l = ' lists '
sw = ' swimlane '
sws = ' swimlanes '
cs = ' cards '
2021-10-16 17:41:18 +03:00
cf = ' custom-fields '
2020-12-03 06:54:25 +02:00
bs = ' boards '
2023-01-13 21:50:39 +02:00
apbs = ' allpublicboards '
2020-12-03 06:54:25 +02:00
atl = ' attachmentslist '
at = ' attachment '
ats = ' attachments '
users = wekanurl + apiusers
2023-01-13 21:50:39 +02:00
user = wekanurl + apiuser
allusers = wekanurl + apiallusers
2020-12-03 06:54:25 +02:00
2020-12-03 07:14:06 +02:00
# ------- API URL GENERATION END -----------
2020-12-03 06:54:25 +02:00
# ------- LOGIN TOKEN START -----------
data = { " username " : username , " password " : password }
2022-09-14 16:02:50 +03:00
body = requests . post ( wekanloginurl , json = data )
2020-12-03 06:54:25 +02:00
d = body . json ( )
apikey = d [ ' token ' ]
# ------- LOGIN TOKEN END -----------
2021-10-16 19:07:36 +03:00
if arguments == 10 :
if sys . argv [ 1 ] == ' addcustomfieldtoboard ' :
# ------- ADD CUSTOM FIELD TO BOARD START -----------
authorid = sys . argv [ 2 ]
boardid = sys . argv [ 3 ]
name = sys . argv [ 4 ]
type1 = sys . argv [ 5 ]
2021-10-19 08:27:28 +03:00
settings = str ( json . loads ( sys . argv [ 6 ] ) )
2021-10-16 19:07:36 +03:00
# There is error: Settings must be object. So this does not work yet.
#settings = {'currencyCode': 'EUR'}
print ( type ( settings ) )
showoncard = sys . argv [ 7 ]
automaticallyoncard = sys . argv [ 8 ]
showlabelonminicard = sys . argv [ 9 ]
showsumattopoflist = sys . argv [ 10 ]
customfieldtoboard = wekanurl + apiboards + boardid + s + cf
# Add Custom Field to Board
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
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 ) }
body = requests . post ( customfieldtoboard , data = post_data , headers = headers )
print ( body . text )
# ------- ADD CUSTOM FIELD TO BOARD END -----------
2020-12-03 06:54:25 +02:00
if arguments == 7 :
if sys . argv [ 1 ] == ' addcard ' :
2021-10-16 19:07:36 +03:00
# ------- ADD CARD START -----------
2020-12-03 06:54:25 +02:00
authorid = sys . argv [ 2 ]
boardid = sys . argv [ 3 ]
swimlaneid = sys . argv [ 4 ]
listid = sys . argv [ 5 ]
cardtitle = sys . argv [ 6 ]
carddescription = sys . argv [ 7 ]
cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
2021-10-16 19:07:36 +03:00
# Add card
2020-12-03 06:54:25 +02:00
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
post_data = { ' authorId ' : ' {} ' . format ( authorid ) , ' title ' : ' {} ' . format ( cardtitle ) , ' description ' : ' {} ' . format ( carddescription ) , ' swimlaneId ' : ' {} ' . format ( swimlaneid ) }
body = requests . post ( cardtolist , data = post_data , headers = headers )
print ( body . text )
2021-10-16 19:07:36 +03:00
# ------- ADD CARD END -----------
2020-12-03 06:54:25 +02:00
2021-06-01 12:41:54 +03:00
if arguments == 6 :
if sys . argv [ 1 ] == ' editcard ' :
2021-10-16 19:07:36 +03:00
# ------- EDIT CARD START -----------
2021-06-01 12:41:54 +03:00
boardid = sys . argv [ 2 ]
listid = sys . argv [ 3 ]
cardid = sys . argv [ 4 ]
newcardtitle = sys . argv [ 5 ]
newcarddescription = sys . argv [ 6 ]
edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
print ( edcard )
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
put_data = { ' title ' : ' {} ' . format ( newcardtitle ) , ' description ' : ' {} ' . format ( newcarddescription ) }
body = requests . put ( edcard , data = put_data , headers = headers )
print ( " === EDIT CARD === \n " )
body = requests . get ( edcard , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
2021-10-16 19:07:36 +03:00
# ------- EDIT CARD END -----------
2021-06-01 12:41:54 +03:00
2023-05-18 09:27:59 -04:00
if sys . argv [ 1 ] == ' editcustomfield ' :
# ------- EDIT CUSTOMFIELD START -----------
boardid = sys . argv [ 2 ]
listid = sys . argv [ 3 ]
cardid = sys . argv [ 4 ]
customfieldid = sys . argv [ 5 ]
newcustomfieldvalue = sys . argv [ 6 ]
edfield = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid + s + ' customFields ' + s + customfieldid
#print(edfield)
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
post_data = { ' _id ' : ' {} ' . format ( customfieldid ) , ' value ' : ' {} ' . format ( newcustomfieldvalue ) }
#print(post_data)
body = requests . post ( edfield , data = post_data , headers = headers )
print ( " === EDIT CUSTOMFIELD === \n " )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- EDIT CUSTOMFIELD END -----------
2022-12-14 09:51:31 +02:00
if arguments == 4 :
if sys . argv [ 1 ] == ' newuser ' :
# ------- CREATE NEW USER START -----------
username = sys . argv [ 2 ]
email = sys . argv [ 3 ]
password = sys . argv [ 4 ]
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
post_data = { ' username ' : ' {} ' . format ( username ) , ' email ' : ' {} ' . format ( email ) , ' password ' : ' {} ' . format ( password ) }
body = requests . post ( users , data = post_data , headers = headers )
print ( " === CREATE NEW USER === \n " )
print ( body . text )
# ------- CREATE NEW USER END -----------
2024-02-07 14:38:49 +01:00
if sys . argv [ 1 ] == ' getcard ' :
# ------- LIST OF CARD START -----------
boardid = sys . argv [ 2 ]
listid = sys . argv [ 3 ]
cardid = sys . argv [ 4 ]
listone = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === INFO OF ONE LIST === \n " )
print ( " URL: " , listone ) # Stampa l'URL per debug
try :
response = requests . get ( listone , headers = headers )
print ( " === RESPONSE === \n " )
print ( " Status Code: " , response . status_code ) # Stampa il codice di stato per debug
if response . status_code == 200 :
data2 = response . text . replace ( ' } ' , " } \n " )
print ( data2 )
else :
print ( f " Error: { response . status_code } " )
print ( f " Response: { response . text } " )
except Exception as e :
print ( f " Error in the GET request: { e } " )
# ------- LISTS OF CARD END -----------
2022-12-14 09:51:31 +02:00
2020-12-03 06:54:25 +02:00
if arguments == 3 :
if sys . argv [ 1 ] == ' createlist ' :
# ------- CREATE LIST START -----------
boardid = sys . argv [ 2 ]
listtitle = sys . argv [ 3 ]
list = wekanurl + apiboards + boardid + s + l
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
post_data = { ' title ' : ' {} ' . format ( listtitle ) }
body = requests . post ( list , data = post_data , headers = headers )
print ( " === CREATE LIST === \n " )
print ( body . text )
# ------- CREATE LIST END -----------
2021-06-01 12:41:54 +03:00
if sys . argv [ 1 ] == ' list ' :
# ------- LIST OF BOARD START -----------
boardid = sys . argv [ 2 ]
listid = sys . argv [ 3 ]
listone = wekanurl + apiboards + boardid + s + l + s + listid
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === INFO OF ONE LIST === \n " )
body = requests . get ( listone , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- LISTS OF BOARD END -----------
2021-10-16 17:51:59 +03:00
if sys . argv [ 1 ] == ' customfield ' :
2021-10-16 19:07:36 +03:00
# ------- INFO OF CUSTOM FIELD START -----------
2021-10-16 17:51:59 +03:00
boardid = sys . argv [ 2 ]
customfieldid = sys . argv [ 3 ]
customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === INFO OF ONE CUSTOM FIELD === \n " )
body = requests . get ( customfieldone , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
2021-10-16 19:07:36 +03:00
# ------- INFO OF CUSTOM FIELD END -----------
2021-10-16 17:51:59 +03:00
2024-02-07 14:38:49 +01:00
if sys . argv [ 1 ] == ' cardsbyswimlane ' :
# ------- RETRIEVE CARDS BY SWIMLANE ID START -----------
boardid = sys . argv [ 2 ]
swimlaneid = sys . argv [ 3 ]
cardsbyswimlane = wekanurl + apiboards + boardid + s + sws + s + swimlaneid + s + cs
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === CARDS BY SWIMLANE ID === \n " )
print ( " URL: " , cardsbyswimlane ) # Debug
try :
body = requests . get ( cardsbyswimlane , headers = headers )
print ( " Status Code: " , body . status_code ) # Debug
data = body . text . replace ( ' } ' , " } \n " )
print ( " Data: " , data )
except Exception as e :
print ( " Error GET: " , e )
# ------- RETRIEVE CARDS BY SWIMLANE ID END -----------
2020-12-03 06:54:25 +02:00
if arguments == 2 :
# ------- BOARDS LIST START -----------
userid = sys . argv [ 2 ]
boards = users + s + userid + s + bs
if sys . argv [ 1 ] == ' boards ' :
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
#post_data = {'userId': '{}'.format(userid)}
body = requests . get ( boards , headers = headers )
print ( " === BOARDS === \n " )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- BOARDS LIST END -----------
2021-10-16 17:41:18 +03:00
if sys . argv [ 1 ] == ' board ' :
2020-12-03 06:54:25 +02:00
# ------- BOARD INFO START -----------
boardid = sys . argv [ 2 ]
board = wekanurl + apiboards + boardid
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
body = requests . get ( board , headers = headers )
print ( " === BOARD === \n " )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- BOARD INFO END -----------
2021-10-16 17:41:18 +03:00
if sys . argv [ 1 ] == ' customfields ' :
2021-10-16 19:07:36 +03:00
# ------- CUSTOM FIELDS OF BOARD START -----------
2021-10-16 17:41:18 +03:00
boardid = sys . argv [ 2 ]
boardcustomfields = wekanurl + apiboards + boardid + s + cf
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
body = requests . get ( boardcustomfields , headers = headers )
print ( " === CUSTOM FIELDS OF BOARD === \n " )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
2021-10-16 19:07:36 +03:00
# ------- CUSTOM FIELDS OF BOARD END -----------
2021-10-16 17:41:18 +03:00
2020-12-03 06:54:25 +02:00
if sys . argv [ 1 ] == ' swimlanes ' :
boardid = sys . argv [ 2 ]
swimlanes = wekanurl + apiboards + boardid + s + sws
# ------- SWIMLANES OF BOARD START -----------
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === SWIMLANES === \n " )
body = requests . get ( swimlanes , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- SWIMLANES OF BOARD END -----------
if sys . argv [ 1 ] == ' lists ' :
# ------- LISTS OF BOARD START -----------
boardid = sys . argv [ 2 ]
2021-06-01 12:41:54 +03:00
lists = wekanurl + apiboards + boardid + s + l
2020-12-03 06:54:25 +02:00
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === LISTS === \n " )
body = requests . get ( lists , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- LISTS OF BOARD END -----------
if sys . argv [ 1 ] == ' listattachments ' :
# ------- LISTS OF ATTACHMENTS START -----------
boardid = sys . argv [ 2 ]
listattachments = wekanurl + apiboards + boardid + s + ats
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === LIST OF ATTACHMENTS === \n " )
body = requests . get ( listattachments , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- LISTS OF ATTACHMENTS END -----------
if arguments == 1 :
if sys . argv [ 1 ] == ' users ' :
# ------- LIST OF USERS START -----------
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( users )
print ( " === USERS === \n " )
body = requests . get ( users , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- LIST OF USERS END -----------
2021-10-16 16:50:47 +03:00
2023-01-13 21:50:39 +02:00
if sys . argv [ 1 ] == ' user ' :
# ------- LIST OF ALL USERS START -----------
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( user )
print ( " === USER === \n " )
body = requests . get ( user , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- LIST OF ALL USERS END -----------
2021-10-16 16:50:47 +03:00
if sys . argv [ 1 ] == ' boards ' :
# ------- LIST OF PUBLIC BOARDS START -----------
headers = { ' Accept ' : ' application/json ' , ' Authorization ' : ' Bearer {} ' . format ( apikey ) }
print ( " === PUBLIC BOARDS === \n " )
listpublicboards = wekanurl + apiboards
body = requests . get ( listpublicboards , headers = headers )
data2 = body . text . replace ( ' } ' , " } \n " )
print ( data2 )
# ------- LIST OF PUBLIC BOARDS END -----------