Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

few add responders patches #3220

Merged
merged 10 commits into from
Oct 31, 2023
27 changes: 21 additions & 6 deletions engine/apps/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,27 @@ def schedules_with_oncall_users(self):
"""
return get_oncall_users_for_multiple_schedules(self.request.user.organization.oncall_schedules.all())

def _get_is_currently_oncall_query_param(self) -> str:
return self.request.query_params.get("is_currently_oncall", "").lower()

def _is_currently_oncall_request(self) -> bool:
return self._get_is_currently_oncall_query_param() in ["true", "false"]

def _is_short_oncall_request(self) -> bool:
return self.request.query_params.get("short", "true").lower() == "false"
joeyorlando marked this conversation as resolved.
Show resolved Hide resolved

def _is_currently_oncall_or_short_request(self) -> bool:
return self._is_currently_oncall_request() or self._is_short_oncall_request()

def get_serializer_context(self):
context = super().get_serializer_context()
context.update({"schedules_with_oncall_users": self.schedules_with_oncall_users})
context.update(
{
"schedules_with_oncall_users": self.schedules_with_oncall_users
if self._is_currently_oncall_or_short_request()
else {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main change, only set context["schedules_with_oncall_users"] for certain requests. Previously we were always setting it even if the serializer didn't need it (UserLongSerializer is the only serializer that needs this here).

}
)
return context

def get_serializer_class(self):
Expand All @@ -247,12 +265,10 @@ def get_serializer_class(self):

is_list_request = self.action in ["list"]
is_filters_request = query_params.get("filters", "false") == "true"
is_short_request = query_params.get("short", "true") == "false"
is_currently_oncall_request = query_params.get("is_currently_oncall", "").lower() in ["true", "false"]

if is_list_request and is_filters_request:
return self.get_filter_serializer_class()
elif is_list_request and (is_short_request or is_currently_oncall_request):
elif is_list_request and self._is_currently_oncall_or_short_request():
return UserLongSerializer

is_users_own_data = kwargs.get("pk") is not None and kwargs.get("pk") == user.public_primary_key
Expand All @@ -277,11 +293,10 @@ def get_queryset(self):
def list(self, request, *args, **kwargs) -> Response:
queryset = self.filter_queryset(self.get_queryset())

is_currently_oncall_query_param = request.query_params.get("is_currently_oncall", "").lower()

def _get_oncall_user_ids():
return {user.pk for _, users in self.schedules_with_oncall_users.items() for user in users}

is_currently_oncall_query_param = self._get_is_currently_oncall_query_param()
if is_currently_oncall_query_param == "true":
# client explicitly wants to filter out users that are on-call
queryset = queryset.filter(pk__in=_get_oncall_user_ids())
Expand Down