I'm trying to figure out how to compile a list of all active users in the form of (first name, last name, email address) out of our active directory in Ignition. I have the following code, but I can't figure out how to get it to match all 3 values perfectly together -- it is getting jumbled up and matching names to the incorrect email addresses. I definitely need to combine the name and email acquisition sections into one singular loop somehow (instead of relying on the zip function at the end) but I don't know how to do this.
emails = []
lastnames = []
firstnames = []
users = system.user.getUsers("")
for user in users:
contacts = user.getContactInfo()
for contact in contacts:
if contact.contactType == "email":
email = contact.value
if email is not None:
emails.append(email)
if user.get(user.FirstName) is not None and user.get(user.LastName) is not None:
lastName = user.get(user.FirstName)
lastnames.append(lastName)
firstName = user.get(user.LastName)
firstnames.append(firstName)
names = zip(firstnames,lastnames,emails)
print names
For example, this is returning entries like the below. Names look good, but wrong email.
[(u'John', u'Smith', u'jane.doe@company.com')]
Maybe something like this:
list = []
users = system.user.getUsers("")
for user in users:
contacts = user.getContactInfo()
for contact in contacts:
if contact.contactType == "email":
email = contact.value
first = user.FirstName
last = user.LastName
if email is not None and first is not None and last is not None:
last.append((first, last, email))
Definitely too magic for anyone not well familiarized with Python, but I couldn't resist:
filter(
all,
(
(
user.get(user.FirstName),
user.get(user.LastName),
next((ci.value for ci in user.contactInfo if ci.contactType == "email"), None)
)
for user in system.user.getUsers("default")
)
)
1 Like
Thank you for the response! Now the list entries are coming in like so:
[(firstname, lastname, u'john.smith@company.com')]
Each entry is coming in with "firstname" and "lastname" instead of the user's name. Side note, my company enters names like "Smith, John" for "Firstname Lastname", causing these 2 values to get mixed up, which is why I have them backwards in the original post. Not super relevant for this problem (because it's easy to account for) but forgot to mention that originally.
This seems cool but I definitely don't understand it! I'll try playing around with it.
Looks like my quick copy just forgets to call user.get()
.
Oh yes you're right! It is working perfectly now, I really appreciate it. The final form is:
list = []
users = system.user.getUsers("")
for user in users:
contacts = user.getContactInfo()
for contact in contacts:
if contact.contactType == "email":
email = contact.value
first = user.get(user.FirstName)
last = user.get(user.LastName)
if email is not None and first is not None and last is not None:
list.append((first, last, email))
print list
3 Likes
One minor thing I'll point out that differs between the imperative solution and the comprehension I posted - if a user has more than one email, they'll end up duplicated in the output with the current imperative solution. Whether that's actually going to matter is going to depend on your input data and your output requirements, but worth consideration.
1 Like