def sync_ldap_groups(self, ldap_groups):
        """Synchronize LDAP groups with local group model."""
        groupname_field = 'name'
        self.stats_group_total = len(ldap_groups)

        for cname, ldap_attributes in ldap_groups:
            defaults = {}
            try:
                for name, attribute in ldap_attributes.items():
                    defaults[self.conf_LDAP_SYNC_GROUP_ATTRIBUTES[name]] = attribute[0].decode('utf-8')
            except AttributeError:
                # In some cases attrs is a list instead of a dict; skip these invalid groups
                continue

            try:
                groupname = defaults[groupname_field]
            except KeyError:
                logger.warning("Group is missing a required attribute '%s'" % groupname_field)
                self.stats_group_errors += 1
                continue

            kwargs = {
                groupname_field + '__iexact': groupname,
                'defaults': defaults,
            }
            try:
                group, created = Group.objects.get_or_create(**kwargs)
            except (IntegrityError, DataError) as e:
                logger.error("Error creating group %s: %s" % (groupname, e))
                self.stats_group_errors += 1
            else:
                if created:
                    self.stats_group_added += 1
                    logger.debug("Created group %s" % groupname)

        logger.info("Groups are synchronized")
def sync_ldap_groups ( self , ldap_groups ) : groupname_field = 'name' self . stats_group_total = len ( ldap_groups ) for cname , ldap_attributes in ldap_groups : defaults = { } try : for name , attribute in ldap_attributes . items ( ) : defaults [ self . conf_LDAP_SYNC_GROUP_ATTRIBUTES [ name ] ] = attribute [ 0 ] . decode ( 'utf-8' ) except AttributeError : continue try : groupname = defaults [ groupname_field ] except KeyError : logger . warning ( "Group is missing a required attribute '%s'" % groupname_field ) self . stats_group_errors += 1 continue kwargs = { groupname_field + '__iexact' : groupname , 'defaults' : defaults , } try : group , created = Group . objects . get_or_create ( ** kwargs ) except ( IntegrityError , DataError ) as e : logger . error ( "Error creating group %s: %s" % ( groupname , e ) ) self . stats_group_errors += 1 else : if created : self . stats_group_added += 1 logger . debug ( "Created group %s" % groupname ) logger . info ( "Groups are synchronized" )
Synchronize LDAP groups with local group model.