aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/serializers.py
diff options
context:
space:
mode:
Diffstat (limited to 'pydis_site/apps/api/serializers.py')
-rw-r--r--pydis_site/apps/api/serializers.py29
1 files changed, 17 insertions, 12 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py
index de2fccff..ac05ebd4 100644
--- a/pydis_site/apps/api/serializers.py
+++ b/pydis_site/apps/api/serializers.py
@@ -209,25 +209,30 @@ class ExpandedInfractionSerializer(InfractionSerializer):
return ret
+class OffTopicChannelNameListSerializer(ListSerializer):
+ """Custom ListSerializer to override to_representation() when list views are triggered."""
+
+ def to_representation(self, objects: list[OffTopicChannelName]) -> list[str]:
+ """
+ Return a list with all `OffTopicChannelName`s in the database.
+
+ This returns the list of off topic channel names. We want to only return
+ the name attribute, hence it is unnecessary to create a nested dictionary.
+ Additionally, this allows off topic channel name routes to simply return an
+ array of names instead of objects, saving on bandwidth.
+ """
+ return [obj.name for obj in objects]
+
+
class OffTopicChannelNameSerializer(ModelSerializer):
"""A class providing (de-)serialization of `OffTopicChannelName` instances."""
class Meta:
"""Metadata defined for the Django REST Framework."""
+ list_serializer_class = OffTopicChannelNameListSerializer
model = OffTopicChannelName
- fields = ('name',)
-
- def to_representation(self, obj: OffTopicChannelName) -> str:
- """
- Return the representation of this `OffTopicChannelName`.
-
- This only returns the name of the off topic channel name. As the model
- only has a single attribute, it is unnecessary to create a nested dictionary.
- Additionally, this allows off topic channel name routes to simply return an
- array of names instead of objects, saving on bandwidth.
- """
- return obj.name
+ fields = ('name', 'used', 'active')
class ReminderSerializer(ModelSerializer):