Skip to content

Commit 3e95570

Browse files
committed
Only return list of ints when retrieving all BumpedThreads
1 parent 0aed5f7 commit 3e95570

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

pydis_site/apps/api/serializers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,29 @@ class Meta:
4242
fields = ('name', 'data')
4343

4444

45+
class ListBumpedThreadSerializer(ListSerializer):
46+
"""Custom ListSerializer to override to_representation() when list views are triggered."""
47+
48+
def to_representation(self, objects: list[BumpedThread]) -> int:
49+
"""
50+
Used by the `ListModelMixin` to return just the list of bumped thread ids.
51+
52+
We want to only return the thread_id attribute, hence it is unnecessary
53+
to create a nested dictionary.
54+
55+
Additionally, this allows bumped thread routes to simply return an
56+
array of thread_id ints instead of objects, saving on bandwidth.
57+
"""
58+
return [obj.thread_id for obj in objects]
59+
60+
4561
class BumpedThreadSerializer(ModelSerializer):
4662
"""A class providing (de-)serialization of `BumpedThread` instances."""
4763

4864
class Meta:
4965
"""Metadata defined for the Django REST Framework."""
5066

67+
list_serializer_class = ListBumpedThreadSerializer
5168
model = BumpedThread
5269
fields = ('thread_id',)
5370

pydis_site/apps/api/viewsets/bot/bumped_thread.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ class BumpedThreadViewSet(
1818
Returns all BumpedThread items in the database.
1919
2020
#### Response format
21-
>>> [
22-
... {
23-
... 'thread_id': "941705627405811793",
24-
... },
25-
... ...
26-
... ]
21+
>>> list[int]
2722
2823
#### Status codes
2924
- 200: returned on success

0 commit comments

Comments
 (0)