Beispiel #1
0
def get_access_requests(item_list: Sequence[Team],
                        user: User) -> AbstractSet[Team]:
    if user.is_authenticated():
        return frozenset(
            OrganizationAccessRequest.objects.filter(
                team__in=item_list, member__user=user).values_list("team",
                                                                   flat=True))
    return frozenset()
Beispiel #2
0
def get_team_memberships(team_list: Sequence[Team],
                         user: User) -> Iterable[int]:
    """Get memberships the user has in the provided team list"""
    if not user.is_authenticated():
        return []

    team_ids: Iterable[int] = OrganizationMemberTeam.objects.filter(
        organizationmember__user=user,
        team__in=team_list).values_list("team", flat=True)
    return team_ids
Beispiel #3
0
def get_org_roles(org_ids: Set[int], user: User) -> Mapping[int, str]:
    """Get the role the user has in each org"""
    if not user.is_authenticated():
        return {}

    # map of org id to role
    return {
        om["organization_id"]: om["role"]
        for om in OrganizationMember.objects.filter(
            user=user, organization__in=set(org_ids)).values(
                "role", "organization_id")
    }
Beispiel #4
0
def get_member_totals(team_list: Sequence[Team],
                      user: User) -> Mapping[str, int]:
    """Get the total number of members in each team"""
    if not user.is_authenticated():
        return {}

    query = (Team.objects.filter(
        id__in=[t.pk for t in team_list],
        organizationmember__invite_status=InviteStatus.APPROVED.value,
    ).annotate(member_count=Count("organizationmemberteam")).values(
        "id", "member_count"))
    return {item["id"]: item["member_count"] for item in query}
Beispiel #5
0
    def get_attrs(
            self, item_list: Sequence[Project], user: User, **kwargs: Any
    ) -> MutableMapping[Project, MutableMapping[str, Any]]:
        def measure_span(op_tag):
            span = sentry_sdk.start_span(
                op=f"serialize.get_attrs.project.{op_tag}")
            span.set_data("Object Count", len(item_list))
            return span

        with measure_span("preamble"):
            project_ids = [i.id for i in item_list]
            if user.is_authenticated() and item_list:
                bookmarks = set(
                    ProjectBookmark.objects.filter(
                        user=user,
                        project_id__in=project_ids).values_list("project_id",
                                                                flat=True))

                notification_settings = NotificationSetting.objects.get_for_user_by_projects(
                    NotificationSettingTypes.ISSUE_ALERTS,
                    user,
                    item_list,
                )
                (
                    notification_settings_by_project_id_by_provider,
                    default_subscribe_by_provider,
                ) = transform_to_notification_settings_by_parent_id(
                    notification_settings)
                notification_settings_by_project_id = (
                    notification_settings_by_project_id_by_provider.get(
                        ExternalProviders.EMAIL, {}))
                default_subscribe = default_subscribe_by_provider.get(
                    ExternalProviders.EMAIL)
            else:
                bookmarks = set()
                notification_settings_by_project_id = {}
                default_subscribe = None

        with measure_span("stats"):
            stats = None
            transaction_stats = None
            project_ids = [o.id for o in item_list]
            if self.transaction_stats and self.stats_period:
                stats = self.get_stats(project_ids, "!event.type:transaction")
                transaction_stats = self.get_stats(project_ids,
                                                   "event.type:transaction")
            elif self.stats_period:
                stats = self.get_stats(project_ids, "!event.type:transaction")

        avatars = {
            a.project_id: a
            for a in ProjectAvatar.objects.filter(project__in=item_list)
        }
        project_ids = [i.id for i in item_list]
        platforms = ProjectPlatform.objects.filter(
            project_id__in=project_ids).values_list("project_id", "platform")
        platforms_by_project = defaultdict(list)
        for project_id, platform in platforms:
            platforms_by_project[project_id].append(platform)

        with measure_span("access"):
            result = get_access_by_project(item_list, user)

        with measure_span("features"):
            features_by_project = get_features_for_projects(item_list, user)
            for project, serialized in result.items():
                serialized["features"] = features_by_project[project]

        with measure_span("other"):
            for project, serialized in result.items():
                is_subscribed = (notification_settings_by_project_id.get(
                    project.id, default_subscribe) ==
                                 NotificationSettingOptionValues.ALWAYS)
                serialized.update({
                    "is_bookmarked":
                    project.id in bookmarks,
                    "is_subscribed":
                    is_subscribed,
                    "avatar":
                    avatars.get(project.id),
                    "platforms":
                    platforms_by_project[project.id],
                })
                if stats:
                    serialized["stats"] = stats[project.id]
                if transaction_stats:
                    serialized["transactionStats"] = transaction_stats[
                        project.id]
        return result