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

Prefetch user and actor in expanded infraction view #1186

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions pydis_site/apps/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,8 @@ def to_representation(self, instance: Infraction) -> dict:
"""Return the dictionary representation of this infraction."""
ret = super().to_representation(instance)

user = User.objects.get(id=ret['user'])
user_data = UserSerializer(user).data
ret['user'] = user_data

actor = User.objects.get(id=ret['actor'])
actor_data = UserSerializer(actor).data
ret['actor'] = actor_data
ret['user'] = UserSerializer(instance.user).data
ret['actor'] = UserSerializer(instance.actor).data

return ret

Expand Down
6 changes: 5 additions & 1 deletion pydis_site/apps/api/viewsets/bot/infraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ def get_queryset(self) -> QuerySet:
})
additional_filters['type__in'] = [i.strip() for i in filter_types.split(",")]

return self.queryset.filter(**additional_filters)
qs = self.queryset.filter(**additional_filters)
if self.serializer_class is ExpandedInfractionSerializer:
return qs.prefetch_related('actor', 'user')

return qs

@action(url_path='expanded', detail=False)
def list_expanded(self, *args, **kwargs) -> Response:
Expand Down