aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/serializers.py
diff options
context:
space:
mode:
authorGravatar RohanJnr <[email protected]>2021-05-31 15:27:54 +0530
committerGravatar RohanJnr <[email protected]>2021-06-04 21:45:08 +0530
commit69f4dc119af4a8d46295abc2551bad24bee96066 (patch)
tree9c4fcb6ec568dadd51165ae71e29e7827bab7c22 /pydis_site/apps/api/serializers.py
parentUse ModelViewSet to add support for PUT and PATCH request. (diff)
Return a list of ot-names only for GET request only to bot/off-topic-channel-names
Diffstat (limited to 'pydis_site/apps/api/serializers.py')
-rw-r--r--pydis_site/apps/api/serializers.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py
index 8f61073e..1acb4fb8 100644
--- a/pydis_site/apps/api/serializers.py
+++ b/pydis_site/apps/api/serializers.py
@@ -1,4 +1,6 @@
"""Converters from Django models to data interchange formats and back."""
+from typing import List
+
from django.db.models.query import QuerySet
from django.db.utils import IntegrityError
from rest_framework.exceptions import NotFound
@@ -200,14 +202,29 @@ class ExpandedInfractionSerializer(InfractionSerializer):
return ret
+class OffTopicChannelNameListSerializer(ListSerializer):
+ def update(self, instance, validated_data):
+ pass
+
+ def to_representation(self, objects: List[OffTopicChannelName]) -> List[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 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', 'active')
+ fields = ('name', 'used', 'active')
class ReminderSerializer(ModelSerializer):