Asana / python-asana

Official Python client library for the Asana API v1
MIT License
303 stars 105 forks source link

Iterating through portfolios using Python #110

Closed jamois closed 4 years ago

jamois commented 4 years ago

Working with portfolios and I would like to do two things:

  1. Pull only the portfolios associated with my "team" (find_by_team) and
  2. Figure out how to iterate through the portfolios once I have them.

As a test, I can do the following:

ports = self.client.portfolios.find_all()

Something is returned in ports but none of the typical iteration techniques work, e.g.

1. for item in ports:
2. option_lst = list(ports)
 for p in enumerate(option_lst):
3. for p in ports.items

I obviously do not understand the iterators in this API since, according to the very few examples provided, I can iterate through projects using option #2 but when iterating through tasks I must use option #1. Since none of the above work for portfolios and I can't find code examples, can someone help by answering the two questions above? Thanks!

rossgrambo-zz commented 4 years ago

Hey @jamois ,

Sadly, you can't directly get all the portfolios for a team. Portfolios in Asana live in a workspace, not under a team. Additionally, with their current security model, you can only get Portfolios for the Authenticated user. Meaning if you used your PAT to get all portfolios, it would have different results then if you used your co-worker's PAT. See the required owner field here: https://developers.asana.com/docs/get-multiple-portfolios

As for iterating, that endpoint returns a generator. So you'll need to call .next(). Details are in the ReadMe here.

Let me know if you have any other questions, I'm going to close this issue for now.

jamois commented 4 years ago

Thanks for the response. I guess I need to figure out what the definition of a workspace is. Our projects are part of a team and I can easily find all of the projects associated with my team using the follow:

asanaProjects =client.projects.find_by_team(designSearchGID)
option_lst = list(asanaProjects)
for i, project in enumerate(option_lst):

But as you can see, I used code from one of the "examples" to iterate through the list of projects.

Regarding the iterator for portfolios, this did not work:

ports =client.portfolios.find_all()
if ports:
    aport = ports.next()

produces this error:

AttributeError: 'generator' object has no attribute 'next'
rossgrambo-zz commented 4 years ago

Alright I found the issue. In python2 if I run:

ports = client.portfolios.find_all({'owner': 'my_gid', 'workspace': 'my_workspace_gid'})
if ports:
    aport = ports.next()

It works as expected.

And if I run:

ports = client.portfolios.find_all()
if ports:
    aport = ports.next()

However in python 3, I hit the error you're hitting. Perhaps you can help me understand, because it's not immediately obvious to me.

The code running is at https://github.com/Asana/python-asana/blob/master/asana/page_iterator.py, where we define next and next.

Your code will work if you change the code from .next() to .next(). I'm not sure if python3 is using next is a different way?

jamois commented 4 years ago

Thanks for the response. I am definitely using python3 and have not tried this on python 2.x. However, I'm not sure I understand your comment:

Your code will work if you change the code from .next() to .next(). I'm not sure if python3 is using next is a different way?

You state next() and next(). Can you clarify? Thx

rossgrambo-zz commented 4 years ago

The code running is at https://github.com/Asana/python-asana/blob/master/asana/page_iterator.py, where we define next and __next__.

Your code will work if you change the code from .next() to .__next__(). I'm not sure if python3 is using next is a different way?

Sorry github auto-formatted it to bold