def _get_features_for_projects(all_projects, user): # Arrange to call features.has_for_batch rather than features.has # for performance's sake projects_by_org = defaultdict(list) for project in all_projects: projects_by_org[project.organization].append(project) features_by_project = defaultdict(list) for feature_name in features.all(feature_type=ProjectFeature).keys(): if not feature_name.startswith(_PROJECT_SCOPE_PREFIX): continue abbreviated_feature = feature_name[len(_PROJECT_SCOPE_PREFIX):] for (organization, projects) in projects_by_org.items(): result = features.has_for_batch(feature_name, organization, projects, user) for (project, flag) in result.items(): if flag: features_by_project[project].append( abbreviated_feature) for project in all_projects: if project.flags.has_releases: features_by_project[project].append("releases") return features_by_project
def get_features_for_projects( all_projects: Sequence[Project], user: User) -> MutableMapping[Project, List[str]]: # Arrange to call features.has_for_batch rather than features.has # for performance's sake projects_by_org = defaultdict(list) for project in all_projects: projects_by_org[project.organization].append(project) features_by_project = defaultdict(list) project_features = [ feature for feature in features.all(feature_type=ProjectFeature).keys() if feature.startswith(_PROJECT_SCOPE_PREFIX) ] batch_checked = set() for (organization, projects) in projects_by_org.items(): batch_features = features.batch_has(project_features, actor=user, projects=projects, organization=organization) # batch_has has found some features if batch_features: for project in projects: for feature_name, active in batch_features.get( f"project:{project.id}", {}).items(): if active: features_by_project[project].append( feature_name[len(_PROJECT_SCOPE_PREFIX):]) batch_checked.add(feature_name) for feature_name in project_features: if feature_name in batch_checked: continue abbreviated_feature = feature_name[len(_PROJECT_SCOPE_PREFIX):] for (organization, projects) in projects_by_org.items(): result = features.has_for_batch(feature_name, organization, projects, user) for (project, flag) in result.items(): if flag: features_by_project[project].append(abbreviated_feature) for project in all_projects: if project.flags.has_releases: features_by_project[project].append("releases") return features_by_project