Get Lab Manager Email?

def get_emails(self):
    """Returns the emails from all registered lab managers
           """
    query = {"portal_type": ["Contact", "LabContact"]}
    contacts = map(api.get_object, api.search(query, "portal_catalog"))

    lab_manager_emails = []
    for contact in contacts:
        roles = contact.getRoles()
        if "LabManager" in roles:
            email = contact.getEmailAddress()
            if email:
                lab_manager_emails.append(email)

    print(lab_manager_emails)
    return list(OrderedDict.fromkeys(lab_manager_emails))

Is this code Correct for getting Lab manager Email From Lab Contacts?

Two things:

  • Objects from type LabContact are catalogued in senaite_catalog_contact. Use this specific catalog instead of portal_catalog

  • The roles are bound to the user, not to the contact. Something like this:

    user = api.get_user(contact)
    roles = user.getRoles() if user else []
    
1 Like

As a result of digging through Senaite. Core docs, I’ve found some get methods to solve my problem

def get_emails(self):
“”“Returns the emails from all registered lab managers
“””
managers = api.get_users_by_roles(“LabManager”)
usernames = [manager.getUserName() for manager in managers]

    labmanager_mails = []
    for labmanager in usernames:  
        properties = api.get_user_properties(labmanager)
        email_property = properties.get('email', '')
        labmanager_mails.append(email_property)
        print(email_property)
    return True

Is this decent method or any mistakes?