Trapping Email using Python API on list_user_activity.

Solved!
jrmathieu63
Level 3
Trapping Email using Python API on list_user_activity.

Have the following python code to trap specifics of the our user community user profiles and to identify their access activity (last_login_time). 

Also will require the email, which is just indicating a keyerror.

What is the email address in list_user_activity since it appears to be different than in list_user?
If possible, is there details on this somewhere online?

activity = client.list_users_activity()
list_user = client.list_users()

dss_activity = []
for user in activity:
login = user.activity['login']
last_login_time = user.activity['lastSuccessfulLogin']
converted_last_login = datetime.fromtimestamp(last_login_time/1e3)
user = client.get_user(login)
settings = user.get_settings()

displayName = settings.get_raw()["displayName"]
email = settings.get_raw()["email"]

 

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-95-c8dbd776efcb> in <module>
17 displayName = settings.get_raw()["displayName"]
---> 18 email = settings.get_raw()["email"]

KeyError: 'email'


Operating system used: AL2


Operating system used: AL2

0 Kudos
1 Solution
jrmathieu63
Level 3
Author

Discovery the issue - the default Administrator and viewer accounts did not have emails assigned.
Once they were, emails were able to be retrieved.
Apparently you cannot have missing emails or you will receive an key error.
Good to know for future issues.

View solution in original post

0 Kudos
3 Replies
JordanB
Dataiker

Hi @jrmathieu63,

What is the email address in list_user_activity since it appears to be different than in list_user?

Email is not set in list_user_activity, it will only include login and session details. 

Can you let us know the output you get for the following script? If none of your users have emails added, you should see an empty list. 

import dataiku
client = dataiku.api_client()
dss_users = client.list_users()
for user in dss_users:
    user = client.get_user(user['login'])
    settings = user.get_settings()
    email = settings.get_raw()["email"]
    print(email)

 

Thanks!

Jordan

 

0 Kudos
jrmathieu63
Level 3
Author

JordonB,

Thanks for the reply.

All of our user accounts have emails assigned as part of their profile since they are LDAP/SSO defined and they are defined automatically thru first login.

Received same error as before with the below scripts.

When I run the following I do see an email assignment available for an account but just cannot select it without the key error.

activity = client.list_users_activity()
for user in activity:
     login = user.activity['login']
     user = client.get_user(login)
     settings = user.get_settings()
     chkall = settings.get_raw()
print(chkall)

 

0 Kudos
jrmathieu63
Level 3
Author

Discovery the issue - the default Administrator and viewer accounts did not have emails assigned.
Once they were, emails were able to be retrieved.
Apparently you cannot have missing emails or you will receive an key error.
Good to know for future issues.

0 Kudos