(self, roles, dry_run=False)
Add each role if it does not already exist, otherwise skip it.
Add each role if it does not already exist, otherwise skip it.
Add each role if it does not already exist otherwise skip it .
def ensure_roles(self, roles, dry_run=False):
        """
        Add each role if it does not already exist, otherwise skip it.
        """
        dry_run_tag = "[DRY RUN] " if dry_run else ""
        roles_to_save = []
        for role in roles:
            if role.slug not in self.roles_by_slug:
                if self.verbose or dry_run:
                    logger.info('%sCreating role: %s', dry_run_tag, role.name)
                if not dry_run:
                    roles_to_save.append(role)
            else:
                logger.info('Role already exists: %s', role.name)
        if roles_to_save:
            roles = Role.objects.bulk_create(roles_to_save)
            self.roles_by_slug.update((role.slug, role) for role in roles)
def ensure_roles ( self , roles , dry_run = False ) : dry_run_tag = "[DRY RUN] " if dry_run else "" roles_to_save = [ ] for role in roles : if role . slug not in self . roles_by_slug : if self . verbose or dry_run : logger . info ( '%sCreating role: %s' , dry_run_tag , role . name ) if not dry_run : roles_to_save . append ( role ) else : logger . info ( 'Role already exists: %s' , role . name ) if roles_to_save : roles = Role . objects . bulk_create ( roles_to_save ) self . roles_by_slug . update ( ( role . slug , role ) for role in roles )