Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mwheeler-ep committed Dec 19, 2024
1 parent 1cdccef commit 01ffcd5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
4 changes: 3 additions & 1 deletion engine/apps/api/serializers/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ def get_teams(self, obj: "AlertGroup"):
Handle AlertGroups that haven't been assigned a team yet
"""

if obj.teams:
if obj.teams.exists():
teams = obj.teams
elif obj.channel.team:
teams = [obj.channel.team]
else:
teams = []
return FastTeamSerializer(teams, context=self.context, many=True).data

def get_render_for_web(self, obj: "AlertGroup") -> RenderForWeb | EmptyRenderForWeb:
Expand Down
32 changes: 28 additions & 4 deletions engine/apps/api/views/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,6 @@ class AlertGroupView(
filter_backends = [AlertGroupSearchFilter, filters.DjangoFilterBackend]
filterset_class = AlertGroupFilter

TEAM_LOOKUP="teams"

def get_serializer_class(self):
if self.action == "list":
return AlertGroupListSerializer
Expand All @@ -304,10 +302,36 @@ def get_serializer_class(self):
def get_queryset(self, ignore_filtering_by_available_teams=False):
# no select_related or prefetch_related is used at this point, it will be done on paginate_queryset.

alert_receive_channels_qs = AlertReceiveChannel.objects_with_deleted.filter(
organization_id=self.request.auth.organization.id
)
if not ignore_filtering_by_available_teams:
alert_receive_channels_qs = alert_receive_channels_qs.filter(*self.available_teams_lookup_args_with_field(field="team"))

# Filter by team(s). Since we really filter teams from integrations, this is not an AlertGroup model filter.
# This is based on the common.api_helpers.ByTeamModelFieldFilterMixin implementation

team_values = self.request.query_params.getlist("team", [])

if team_values:
null_team_lookup = Q(team__isnull=True) if NO_TEAM_VALUE in team_values else None
alert_receive_channels_teams_lookup = Q(team__public_primary_key__in=[ppk for ppk in team_values if ppk != NO_TEAM_VALUE])
if null_team_lookup:
alert_receive_channels_teams_lookup = alert_receive_channels_teams_lookup | null_team_lookup
alert_receive_channels_qs = alert_receive_channels_qs.filter(alert_receive_channels_teams_lookup)

alert_receive_channels_ids = list(alert_receive_channels_qs.values_list("id", flat=True))
print(alert_receive_channels_ids)


if team_values:
null_team_lookup = (Q(teams__isnull=True) | Q(teams=None)) if NO_TEAM_VALUE in team_values else None
teams_lookup = Q(teams__public_primary_key__in=[ppk for ppk in team_values if ppk != NO_TEAM_VALUE])
# need to also filter null team by null in in the receive channels so that we don't provide alertgroups where the team is assoicated to the channel
null_team_lookup = ((Q(teams__isnull=True) | Q(teams=None)) & Q(channel__in=alert_receive_channels_ids)) if NO_TEAM_VALUE in team_values else None
teams_lookup = (
Q(teams__public_primary_key__in=[ppk for ppk in team_values if ppk != NO_TEAM_VALUE]) | # handle alertgroups with teams property
((Q(teams__isnull=True) | Q(teams=None)) & Q(channel__in=alert_receive_channels_ids)) # handle alertgroups without a teams property set
)

if null_team_lookup:
teams_lookup = teams_lookup | null_team_lookup

Expand Down
16 changes: 10 additions & 6 deletions engine/common/api_helpers/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,25 @@ class TeamFilteringMixin:

TEAM_LOOKUP = "team"

@property
def available_teams_lookup_args(self):

def available_teams_lookup_args_with_field(self, field=TEAM_LOOKUP):
"""
This property returns a list of Q objects that are used to filter instances by teams available to the user.
NOTE: use .distinct() after filtering by available teams as it may return duplicate instances.
"""
available_teams_lookup_args = []
if not self.request.user.is_admin:
available_teams_lookup_args = [
Q(**{f"{self.TEAM_LOOKUP}__users": self.request.user})
| Q(**{f"{self.TEAM_LOOKUP}__is_sharing_resources_to_all": True})
| Q(**{f"{self.TEAM_LOOKUP}__isnull": True})
Q(**{f"{field}__users": self.request.user})
| Q(**{f"{field}__is_sharing_resources_to_all": True})
| Q(**{f"{field}__isnull": True})
]
return available_teams_lookup_args


@property
def available_teams_lookup_args(self):
return self.available_teams_lookup_args_with_field(field=self.TEAM_LOOKUP)

def retrieve(self, request, *args, **kwargs):
try:
return super().retrieve(request, *args, **kwargs)
Expand Down

0 comments on commit 01ffcd5

Please sign in to comment.