4

Using python-redmine module, is there a way to list all users assigned to a project through the Redmine API?

I've found that I'm able to request information about individual users, and that may API Key user is only assigned to a specific project and it can only see other users on the project. So I can do the following but it is hugely inefficient:

from redmine import Redmine
redmine = Redmine("https://myserver.net/", key="blahblah")

my_users = []
for x in range(1, 301):
    try:
        user = redmine.user.get(str(x))
        my_users.append(user.id)
    except:
        print "",

The above code gets me what I want, a list of the users on a project... but takes way too long to be practical. Plus there may be user IDs higher than any range I pick.

Update: Here is what I got to work, thnx @njzk2

my_users = []
project = redmine.project.get('myproject')
for membership in project.memberships:
    my_users.append(membership.user.id)
oz10
  • 153,307
  • 27
  • 93
  • 128

2 Answers2

5

The answer by njzk2 is good but it still makes 2 separate API calls:

  1. redmine.project.get('my_project')
  2. redmine.project.get('my_project').memberships

So if you need to get all available details of the project first and all the members of the project second, then it's the best available option. But if you only need to get the members, you can do it in one API call via:

redmine.project_membership.filter(project_id='my_project') (docs)

You're probably wondering why there are so many ways to do one thing, well, because there can be different use cases and python-redmine tries it's best to provide different ways to achieve things. So you'll have to look through documentation thoroughly and find the best available way that works for your exact case.

Max Tepkeev
  • 2,636
  • 1
  • 14
  • 12
4

Probably something like:

for membership in redmine.project.get('my_project').memberships:
    print membership.user_id

see http://python-redmine.readthedocs.org/resources/project_membership.html and http://python-redmine.readthedocs.org/resources/project.html

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • FWIW, after seeing your suggested example I realized just how non-sensicle the python-redmine documentation was to me. I'm probably at a disadvantage because I'm not familiar with ORM style interface. :( – oz10 Oct 16 '14 at 21:28
  • I find that there could be more details on the properties available on each object, too. – njzk2 Oct 17 '14 at 13:05
  • Probably at first it's hard, but when you get used to it, it's really intuitive and easy to use. But if you guys have an idea about how documentation can be improved, just open an issue at github and describe the improvements you believe would make documentation more easier to read and understand. – Max Tepkeev Oct 18 '14 at 07:29