Forum Discussion

FrankG's avatar
3 years ago

Confusing requirements and output get_admin_list using the SDK - Guidance requested

Hello all, I'm trying to gather a list of users (role agnostic) and include only a specific set of fields in my output, filtered by a last_login_on timeframe.  When I attempt to do this throug...
  • Anonymous's avatar
    Anonymous
    3 years ago

    Here you go. This uses my lmwrapper helper script, so if you're not using that, you'd need to setup the api object manually. Just set the month, day, and year variables to the date you'd want. 

    from lm import lm
    from datetime import datetime
    
    #datetime before which to consider users inactive
    month = 10
    day = 1
    year = 2021
    inactivedate = datetime(year,month,day,0,0,0)
    
    #these next few variables are for use in pagination
    size = 1000
    offset = 0
    results = []
    endfound = False
    while not endfound:
        users = lm.get_admin_list(size=size, offset=offset).items #fetch a page
        results.extend(users) #add the fetched data to the tally
        offset += size #figure out the next page range
        endfound = len(users) < size #check to see if we fetched an incomplete page (e.g. we're done)
    
    for user in results:
        lastlogin = datetime.utcfromtimestamp(user.last_login_on) #convert the string to a datetime object so we can compare it to our inactivedate
        if lastlogin < inactivedate and user.status != 'suspended':
            print(f"Disabling {user.username} (id={user.id}), last login time: {lastlogin.strftime('%Y-%m-%d')}")
            response = lm.patch_admin_by_id(user.id, body={'status':'suspended'})
            print(f"{user.username} (id={user.id}) is now {response.status}")