sarumont / py-trello

Python API wrapper around Trello's API
BSD 3-Clause "New" or "Revised" License
945 stars 330 forks source link

set_custom_field doesn't set checkbox field type #310

Closed ToyinIku closed 4 years ago

ToyinIku commented 4 years ago

I want to check a custom fields on a card through this API but when I pass the arguments to set the values but it jumps to the else statement because the field_type value is "checkbox" and not "checked". py-trello

def set_custom_field(self, value, custom_field):
         """Update card custom field

         Arguments:
             value {[str, int, date, bool]} -- Value depending on the type of custom_field
             custom_field {custom field object} -- Custom Field Object (board.get_custom_field_definitions()[0])

         """
         if custom_field.field_type in ['text', 'number', 'date', 'checked']: 
             post_args = {'value': {str(custom_field.field_type): value}}
         else: 
             list_field_id = [
                 x for x, y in custom_field.list_options.items() if y == value][0]
             post_args = {'idValue': list_field_id}
         self.client.fetch_json(
            '/card/' + self.id + '/customField/' + custom_field.id + '/item',
            http_method='PUT',
            post_args=post_args)

piece of my code

hasBlog = False                                                             #site does not have shopping cart by default

if (cardFields["info_blog_c"]).upper() == "YES":                        #if field value is yes 
          hasBlog = True                                                              #set to true

cardCreated.set_custom_field(hasBlog, currentCustomField)                  #add the shopping_cart_c field

Error response

Traceback (most recent call last):
 ... line 152, in addCardToListInBoard
    cardCreated.set_custom_field(hasBlog, currentCustomField)        #add the info_blog_c field
  File "/Users/--/.local/share/virtualenvs/Python-5XITnLz4/lib/python3.8/site-packages/trello/card.py", line 573, in set_custom_field
    list_field_id = [
IndexError: list index out of range

I tried this

hasBlog = False                                                             #site does not have blog by default

if (cardFields["info_blog_c"]).upper() == "YES":                        #if field value is yes 
         hasBlog = True                                                              #set to true

currentCustomField.field_type = "checked"
cardCreated.set_custom_field(hasBlog, currentCustomField)        #add the info_blog_c field
currentCustomField.field_type = "checkbox"

Then got this

Traceback (most recent call last):
  File ... line 156, in addCardToListInBoard
    cardCreated.set_custom_field(hasBlog, currentCustomField)        #add the info_blog_c field
  File "/Users/--/.local/share/virtualenvs/Python-5XITnLz4/lib/python3.8/site-packages/trello/card.py", line 577, in set_custom_field
    self.client.fetch_json(
  File "/Users/--/.local/share/virtualenvs/Python-5XITnLz4/lib/python3.8/site-packages/trello/trelloclient.py", line 231, in fetch_json
    raise ResourceUnavailable("%s at %s" % (response.text, url), response)
trello.exceptions.ResourceUnavailable: {"message":"Invalid custom field item value.","error":"ERROR"} at https://api.trello.com/1/card/5ea87ccf329ab97228a72d5c/customField/5e8651cbbb9d488f683b4105/item (HTTP status: 400)
ToyinIku commented 4 years ago

Seems like it doesn't take bool type, needs to be a string

hasBlog = "false"                                                             #site does not have blog by default

if (cardFields["info_blog_c"]).upper() == "YES":                        #if field value is yes 
                hasBlog = "true"                                                              #set to true

currentCustomField.field_type = "checked"
cardCreated.set_custom_field(hasBlog, currentCustomField)        #add the info_blog_c field
currentCustomField.field_type = "checkbox"