Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Sort checkbox items alphabetically

Luyin I'm New Here Sep 10, 2020

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

3 answers

@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"}

1 vote
Iain Dooley Community Leader Sep 10, 2020

@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:

https://app.benkobot.com/

You can look at the implementation details/API calls in the Trellinator code:

http://docs.trellinator.com/

https://github.com/iaindooley/trellinator-libs

@SGi Admin 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. 

Like Iain Dooley likes this

@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

 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Trello

📹 What is Trello?

Hello Community!  My name is Brittany Joiner and I am a Trello enthusiast and Atlassian Community Leader. I'll be sharing with you a series of videos with my top tips on how to best use Trello...

817 views 13 30
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you