aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/serializers.py
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2021-12-28 16:24:37 +0100
committerGravatar GitHub <[email protected]>2021-12-28 16:24:37 +0100
commit4ab79729b1c4ed579bf26349abee7d42a9fdcf2f (patch)
treec7d7c51ed6ab9598ab9e00e7aca253bea1077588 /pydis_site/apps/api/serializers.py
parentMerge pull request #636 from python-discord/revert-metricity-change (diff)
parentMerge branch 'main' into otn_softdel (diff)
Merge pull request #508 from RohanJnr/otn_softdel
offtopicnames active attribute and support for PUT and PATCH request
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):