def validate_has_feature(user: User) -> None:
    if not any([
            features.has("organizations:notification-platform",
                         organization,
                         actor=user) for organization in user.get_orgs()
    ]):
        raise ResourceDoesNotExist
    def get_attrs(
        self,
        item_list: Iterable[Union["Team", "User"]],
        user: User,
        **kwargs: Any,
    ) -> Mapping[Union["Team", "User"], Mapping[str, Iterable[Any]]]:
        """
        This takes a list of recipients (which are either Users or Teams,
        because both can have Notification Settings). The function
        returns a mapping of targets to flat lists of object to be passed to the
        `serialize` function.

        :param item_list: Either a Set of User or Team objects whose
            notification settings should be serialized.
        :param user: The user who will be viewing the notification settings.
        :param kwargs: Dict of optional filter options:
            - type: NotificationSettingTypes enum value. e.g. WORKFLOW, DEPLOY.
        """
        type_option: Optional[NotificationSettingTypes] = kwargs.get("type")
        actor_mapping = {
            recipient.actor_id: recipient
            for recipient in item_list
        }

        notifications_settings = NotificationSetting.objects._filter(
            type=type_option,
            target_ids=actor_mapping.keys(),
        )

        results: MutableMapping[Union["Team", "User"], MutableMapping[
            str, Set[Any]]] = defaultdict(lambda: defaultdict(set))

        for notifications_setting in notifications_settings:
            target = actor_mapping.get(notifications_setting.target_id)
            results[target]["settings"].add(notifications_setting)

        for recipient in item_list:
            # This works because both User and Team models implement `get_projects`.
            results[recipient]["projects"] = recipient.get_projects()

            if type(recipient) == Team:
                results[recipient]["organizations"] = {recipient.organization}

            if type(recipient) == User:
                results[recipient]["organizations"] = user.get_orgs()

        return results
Example #3
0
def get_identity(
    user: User, organization_id: int, integration_id: int
) -> Tuple[Organization, Integration, IdentityProvider]:
    try:
        organization = Organization.objects.get(id__in=user.get_orgs(), id=organization_id)
    except Organization.DoesNotExist:
        raise Http404

    try:
        integration = Integration.objects.get(id=integration_id, organizations=organization)
    except Integration.DoesNotExist:
        raise Http404

    try:
        idp = IdentityProvider.objects.get(external_id=integration.external_id, type="slack")
    except IdentityProvider.DoesNotExist:
        raise Http404

    return organization, integration, idp