def disabled_users_from_project( project: Project) -> Mapping[ExternalProviders, set[User]]: """Get a set of users that have disabled Issue Alert notifications for a given project.""" user_ids = project.member_set.values_list("user", flat=True) users = User.objects.filter(id__in=user_ids) notification_settings = NotificationSetting.objects.get_for_recipient_by_parent( type=NotificationSettingTypes.ISSUE_ALERTS, parent=project, recipients=users, ) notification_settings_by_recipient = transform_to_notification_settings_by_recipient( notification_settings, users) # Although this can be done with dict comprehension, looping for clarity. output = defaultdict(set) for user in users: settings = notification_settings_by_recipient.get(user) if settings: settings_by_provider = get_settings_by_provider(settings) for provider, settings_value_by_scope in settings_by_provider.items( ): project_setting = settings_value_by_scope.get( NotificationScopeType.PROJECT) user_setting = settings_value_by_scope.get( NotificationScopeType.USER) or settings_value_by_scope.get( NotificationScopeType.TEAM) if project_setting == NotificationSettingOptionValues.NEVER or ( not project_setting and user_setting == NotificationSettingOptionValues.NEVER): output[provider].add(user) return output
def test_get_settings_by_provider(self): settings = { NotificationScopeType.USER: { ExternalProviders.EMAIL: NotificationSettingOptionValues.NEVER } } assert get_settings_by_provider(settings) == { ExternalProviders.EMAIL: { NotificationScopeType.USER: NotificationSettingOptionValues.NEVER } }