I would like to build a function (via API or Power-Up; I would slightly prefer to use the API, because it lets me exercise coding) that lets me sort items in a checklist on a card alphabetically. So my idea is that I have two functions:
f(checklist) -> sorted(checklist_items)
g(sorted_checklist_items, board) -> PUT (update checklist item order on board)
The first one I got to work with this:
for c in cards_with_checklists: if c['shortLink'] in TO_BE_SORTED: print(c['name'], c['shortLink']) print('shortLink is correct, entering checklist sorting') checklists = (get_checklist(id) for id in c['idChecklists']) for cl in checklists: counter = 0 for item in sorted( cl['checkItems'], key=lambda x: x['name'].lower() ): item['pos'] = counter counter += 1 print('updated checklist in order:') print(cl['name']) print(cl['id']) print(cl) # for debugging print() break
(I pasted this from a jupyter notebook, so it's not 100 % refactored)
For the second one I used this:
def update_checklist(id) -> None or requests.exceptions.HTTPError: response = requests.put(f"{trello_api}/checklists/{id}", params=q) return response.raise_for_status()
The code runs through, with a 200 OK code, but the items on the card didn't get updated on the board (not even after refreshing the page, clearing the cache and similar movements. That makes me wonder whether I misunderstood the logic of the 'pos' attribute on checklist items.
Is there anyone who has built something similar and has succeeded?
BR
Luyin
@Luyin here's my implementation in Trellinator:
https://gist.github.com/iaindooley/4f138401f475a56fccf8e4295ec3c90c
You can run that in BenkoBot here, just update the card link and the checklist name:
You can look at the implementation details/API calls in the Trellinator code:
https://github.com/iaindooley/trellinator-libs
@milynnus you can set position on Checklist items:
https://github.com/iaindooley/trellinator-libs/blob/master/CheckItem.js#L88
@Iain Dooley Thanks. Will check py-Trello library. I know that there are some things I can do when coding custom Power Up using JS I cannot do with py-Trello especially the part on user-variables.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Luyin as far as I know there is no 'set' for 'pos' on a checklist item. So you need to clear and add the items back
Here's what I did using py-trello
for cl in trello_card.checklists:
if cl.name == 'Checklist':
cl_dict = {}
for item in cl.items:
cl_dict[item['name']] = item['checked']
cl_dict_ordered = OrderedDict(sorted(cl_dict.items()))
print(cl_dict)
print(cl_dict_ordered)
cl.clear()
for k in cl_dict_ordered.keys():
cl.add_checklist_item(name=k, checked=cl_dict_ordered[k])
# for verification only
test_dict = {}
for item in cl.items:
test_dict[item['name']]= item['checked']
print(test_dict)
Output:
{'C': True, 'S': False, 'F': True, 'A': False, 'G': False} OrderedDict([('A', False), ('C', True), ('F', True), ('G', False), ('S', False)]) {'A': False, 'C': True, 'F': True, 'G': False, 'S': False}
I will probably convert this to a module that can be called via Butler HTTP Request to my Superhero Power endpoint with payload as {"card_id" : "{cardidlong}", "checklist_name" : "name of checklist"}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Luyin this should be better. It preserves the checklist item "as is" . Important when advanced checklist assignment and due dates were set on the items.
for cl in trello_card.checklists:
if cl.name == 'Checklist':
sorted_cl_items = sorted(cl.items, key=lambda dict_items : dict_items.get('name'))
ix = 0
for i in sorted_cl_items:
# set pos to 6000++ and using Code block from py-trello def set_checklist_item(self, name, checked)
json_obj = client.fetch_json(
'/cards/' + trello_card.id +
'/checklist/' + cl.id +
'/checkItem/' + i['id'],
http_method='PUT',
post_args={'pos' : 6000 + ix})
cl.items[ix] = json_obj
ix = ix + 1
break
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.