Ejemplo n.º 1
0
    def groups_list(self):
        """
        returns all groups in the domain using the adminsdk api.
        """
        key = "admin_sdk_groups_list"
        groups = cache.get(key)
        if not groups:
            groups = []
            page_token = None
            # build our group list
            am = AdminManager()
            service = am.service()
            while True:
                results = service.groups().list(
                    domain=settings.DOMAIN_SUPER_USER_EMAIL.split('@')[1],
                    maxResults=500,
                    pageToken=page_token
                ).execute()

                page_token = results.get('nextPageToken')

                for group in results["groups"]:
                    groups.append(group)
                if not page_token:
                    break
            # set cache to expire after 24 hours
            cache.set(key, groups, 60*60*24)
        return groups
Ejemplo n.º 2
0
    def groups_list(self):
        """
        returns all groups in the domain using the adminsdk api.
        """
        key = "admin_sdk_groups_list"
        groups = cache.get(key)
        if not groups:
            groups = []
            page_token = None
            # build our group list
            am = AdminManager()
            service = am.service()
            while True:
                results = service.groups().list(
                    domain=settings.DOMAIN_SUPER_USER_EMAIL.split('@')[1],
                    maxResults=500,
                    pageToken=page_token).execute()

                page_token = results.get('nextPageToken')

                for group in results["groups"]:
                    groups.append(group)
                if not page_token:
                    break
            # set cache to expire after 24 hours
            cache.set(key, groups, 86400)
        return groups
Ejemplo n.º 3
0
 def group_members(self, email):
     """
     retrieves a group's member list from the admin sdk api.
     """
     key = "group_members_{}".format(email)
     members = cache.get(key)
     if not members:
         am = AdminManager()
         service = am.service()
         while True:
             try:
                 members = service.members().list(groupKey=email,
                                                  alt='json').execute()
             except Exception, e:
                 pass
             else:
                 break
         cache.set(key, members)
Ejemplo n.º 4
0
 def group_members(self, email):
     """
     retrieves a group's member list from the admin sdk api.
     """
     key = "group_members_{}".format(email)
     members = cache.get(key)
     if not members:
         am = AdminManager()
         service = am.service()
         while True:
             try:
                 members = service.members().list(
                     groupKey = email, alt='json'
                 ).execute()
             except Exception, e:
                 pass
             else:
                 break
         cache.set(key, members)
Ejemplo n.º 5
0
def main():
    '''
    Fetch all users from the Google API, go through their
    contacts, and update their contact record for the given
    email for the following fields:

    title
    fullName
    givenName
    familyName
    '''

    am = AdminManager()

    user_list = []
    page_token = None
    while True:
        results = am.service().users().list(
            domain=email.split('@')[1],
            maxResults=500,
            pageToken=page_token,
            orderBy='email', viewType='domain_public'
        ).execute()

        for r in results["users"]:
            user_list.append(r)

        page_token = results.get('nextPageToken')
        if not page_token:
            break

    print "length of user_list: {}".format(len(user_list))

    count = 0
    for user in user_list:

        user_email = user["primaryEmail"]
        cm = ContactsManager(user_email)
        contacts = cm.contacts(settings.CONTACTS_MAX_RESULTS)

        # loop through all contacts in the user's collection
        for entry in contacts.entry:
            # loop through all emails for any given contact
            for e in entry.email:
                if e.address == email:
                    # loop through the various links
                    for l in entry.link:
                        if l.type == 'application/atom+xml' and l.rel == 'edit':
                            if test:
                                print user_email
                                print "\n{}".format(l.href)

                            contact = cm.get_contact(l.href)

                            if test:
                                print contact

                            if contact.name:
                                new_contact = cm.set_name(
                                    contact, names[0].split(',')
                                )
                                if test:
                                    print new_contact
                                cm.save(new_contact)
                                count += 1

    print "number of accounts updated: {}".format(count)