sarumont / py-trello

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

help with copying card from one board to another #289

Open mals14 opened 5 years ago

mals14 commented 5 years ago

I am not a Python expert and trying to learn and trying to use py-trello. Thank you for writing the library!

Would like to copy a card from one board to a list in a different board. Have selected the source_board, target_board and target_list using the functions available in py-trello. Unable to copy the card though.

Now py-trello already has code for moving a card.

    def change_board(self, board_id, list_id=None):
        args = {'value': board_id}
        if list_id is not None:
            args['idList'] = list_id
        self.client.fetch_json(
            '/cards/' + self.id + '/idBoard',
            http_method='PUT',
            post_args=args)

I can tell that the answer lies on this Trello API page in terms of how the url is to be formed that needs to go to the Trello API, but honestly am not clear as to what the parameters should be if say I want to copy everything that can be copied in a card - attachments, checklists, labels, comments and description.

Also, even if the url format were to be clear to me, I am not sure how to create a function to copy the card by modifying the change_board function above. Not sure of how to augment the class in my code :(

Thank you for your help.

mals14 commented 5 years ago

And I figured that this is the call that needs to be made by something that augments the code from the outside.

Copying this below from trelloclient.py

def fetch_json(
            self,
            uri_path,
            http_method='GET',
            headers=None,
            query_params=None,
            post_args=None,
            files=None):
        """ Fetch some JSON from Trello """

        # explicit values here to avoid mutable default values
        if headers is None:
            headers = {}
        if query_params is None:
            query_params = {}
        if post_args is None:
            post_args = {}

        # if files specified, we don't want any data
        data = None
        if files is None:
            data = json.dumps(post_args)

        # set content type and accept headers to handle JSON
        if http_method in ("POST", "PUT", "DELETE") and not files:
            headers['Content-Type'] = 'application/json; charset=utf-8'

        headers['Accept'] = 'application/json'

        # construct the full URL without query parameters
        if uri_path[0] == '/':
            uri_path = uri_path[1:]
        url = 'https://api.trello.com/1/%s' % uri_path

        if self.oauth is None:
            query_params['key'] = self.api_key
            query_params['token'] = self.api_secret

        # perform the HTTP requests, if possible uses OAuth authentication
        response = self.http_service.request(http_method, url, params=query_params,
                                             headers=headers, data=data,
                                             auth=self.oauth, files=files,
                                             proxies=self.proxies)

        if response.status_code == 401:
            raise Unauthorized("%s at %s" % (response.text, url), response)
        if response.status_code != 200:
            raise ResourceUnavailable("%s at %s" % (response.text, url), response)

        return response.json()

And this needs to be used with the answer on this page - https://stackoverflow.com/questions/29774835/trello-api-copying-cards-keepfromsource

I just don't know how to do it and will appreciate any help. thanks.

sahandevs commented 5 years ago

hi @mals14, it's better to ask this kind of questions in stackoverflow. but you can use this method i wrote for copying cards https://github.com/SahandAkbarzadeh/trello-chicbot/blob/master/lib_trello/trello_extensions.py

mcarlock12 commented 5 years ago

Having some issues and this looks like the closest Issue ticket. we need more documentation for this page, but anyone knows how I can get the card.description?

client = TrelloClient( api_key='', api_secret='' )

all_boards = client.list_boards() all_boards.desc() ??

mals14 commented 5 years ago

here is how you can print all the descriptions.

for board in all_boards:
  print(board.description)
mals14 commented 4 years ago

hi @mals14, it's better to ask this kind of questions in stackoverflow. but you can use this method i wrote for copying cards https://github.com/SahandAkbarzadeh/trello-chicbot/blob/master/lib_trello/trello_extensions.py

@SahandAkbarzadeh I was thinking that this relates to completing the wrapper to include one end point that was missed as opposed to simply seeking help with solving a problem? Sound right - otherwise do share how you are thinking about what goes here and what goes on stack overflow - will help in the future. Thanks.

mals14 commented 4 years ago

hi @mals14, it's better to ask this kind of questions in stackoverflow. but you can use this method i wrote for copying cards https://github.com/SahandAkbarzadeh/trello-chicbot/blob/master/lib_trello/trello_extensions.py

And thank you for sharing your method. It worked beautifully, and it has given me pointers as to how to use code from within a Python library.