diff options
author | 2022-01-01 14:28:53 +0200 | |
---|---|---|
committer | 2022-01-01 14:28:53 +0200 | |
commit | 1798edc10ff5e5adaffe9879f2a9ebb8a7b5c926 (patch) | |
tree | 984fb8393fe0a8f17499cb89cc306de455841453 /pydis_site/apps/api/serializers.py | |
parent | Merged infraction and notification settings in JSON (diff) | |
parent | Merge pull request #637 from python-discord/mbaruh-patch-1 (diff) |
Merge branch 'main' into new-filter-schema
Diffstat (limited to 'pydis_site/apps/api/serializers.py')
-rw-r--r-- | pydis_site/apps/api/serializers.py | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 91aac822..88b6e2bd 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -392,25 +392,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): |