Example #1
0
    def push_to_google(self, api=None):
        """Push to google.

        Reuse api if provided.
        """
        # Check if we have an email to sync to:
        if self.google_email is None or not self.google_email:
            logger.info('No email address for %s', str(self))
            return
        logger.info('Pushing to %s', self.google_email)

        # Reuse api session, and update obj with email address:
        if api is None:
            api = GoogleClient(group=self.google_email)

        api.group = self.google_email

        # Sync:
        if not api.check_group_exists():
            api.create_group()

        emails = utils.clean_emails(
            elvanto_emails=self.elvanto_emails(),
            google_emails=api.fetch_members()
        )
        logger.debug('Emails here: [%s]', ','.join(emails.elvanto))
        logger.debug('Emails google: [%s]', ','.join(emails.google))
        here_not_on_google = set(emails.elvanto) - set(emails.google)
        logger.debug('Here, not on google: [%s]', ','.join(here_not_on_google))
        on_google_not_here = set(emails.google) - set(emails.elvanto)
        logger.debug('On google, not here: [%s]', ','.join(on_google_not_here))

        # update the group, we must call remove first, otherwise we may add
        # a member and then remove them due to domain aliases
        api.remove_members(on_google_not_here)
        api.add_members(here_not_on_google)

        self.last_pushed = timezone.now()
        self.save()

        # check emails match now:
        new_emails = utils.clean_emails(
            elvanto_emails=self.elvanto_emails(),
            google_emails=api.fetch_members()
        )
        self._check_emails_match(new_emails)
Example #2
0
    def push_to_google(self, api=None):
        """Push to google.

        Reuse api if provided.
        """
        # Check if we have an email to sync to:
        if self.google_email is None or not self.google_email:
            logger.info(f'No email address for {str(self)}')
            return
        logger.info(f'Pushing to {self.google_email}')

        # Reuse api session, and update obj with email address:
        if api is None:
            api = GoogleClient(group=self.google_email)

        api.group = self.google_email

        # Sync:
        if not api.check_group_exists():
            api.create_group()

        emails = utils.clean_emails(elvanto_emails=self.elvanto_emails(), google_emails=api.fetch_members())
        logger.debug(f'Emails here: [{",".join(emails.elvanto)}]')
        logger.debug(f'Emails google: [{",".join(emails.google)}]')
        here_not_on_google = utils.compare_emails(emails.elvanto, emails.google)
        logger.debug(f'Here, not on google: [{",".join(here_not_on_google)}]')
        on_google_not_here = utils.compare_emails(emails.google, emails.elvanto)
        logger.debug(f'On google, not here: [{",".join(on_google_not_here)}]')

        # update the group, we must call remove first, otherwise we may add
        # a member and then remove them due to domain aliases
        api.remove_members(on_google_not_here)
        api.add_members(here_not_on_google)

        self.last_pushed = timezone.now()
        self.save()

        # check emails match now:
        new_emails = utils.clean_emails(elvanto_emails=self.elvanto_emails(), google_emails=api.fetch_members())
        self._check_emails_match(new_emails)
def push_emails_to_list(mailing_list, group_pk):
    from elvanto_sync.models import ElvantoGroup
    grp = ElvantoGroup.objects.get(pk=group_pk)
    if not grp.check_google_group_exists():
        grp.create_google_group()

    emails = utils.clean_emails(elvanto_emails=grp.elvanto_emails(),
                                google_emails=grp.google_emails())
    print('Here:')
    print('\t{}'.format(','.join(emails.elvanto)))
    print('Google:')
    print('\t{}'.format(','.join(emails.google)))
    # groups do not match
    here_not_on_google = set(emails.elvanto) - set(emails.google)
    print('Here, not on google:')
    print('\t{}'.format(','.join(here_not_on_google)))
    on_google_not_here = set(emails.google) - set(emails.elvanto)
    print('On google, not here:')
    print('\t{}'.format(','.join(on_google_not_here)))
    # TODO change to a single request
    access_token = fetch_primary_google_token()
    for e in here_not_on_google:
        retry_request(
            'https://www.googleapis.com/admin/directory/v1/groups/{0}/members'.format(mailing_list.replace('@', '%40')),
            'post',
            params={'access_token': access_token},
            data=json.dumps({'email': e}),
            headers={'Content-Type': 'application/json'}
        )

    # TODO change to a single request
    for e in on_google_not_here:
        retry_request(
            'https://www.googleapis.com/admin/directory/v1/groups/{0}/members/{1}'.format(mailing_list.replace('@', '%40'), e.replace('@', '%40')),
            'delete',
            params={'access_token': access_token}
        )

    grp.last_pushed = timezone.now()
    grp.save()
 def test_clean_emails(email_set1, email_set2):
     utils.clean_emails(elvanto_emails=email_set1, google_emails=email_set2)
def test_clean_emails(email_set1, email_set2):
    utils.clean_emails(elvanto_emails=email_set1,
                       google_emails=email_set2)