From 8232b1115bdc308c7ca12f477c704957ec3e3ed1 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Wed, 19 May 2021 13:15:53 +0530 Subject: Serialize name and active attribute. --- pydis_site/apps/api/serializers.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index f47bedca..8f61073e 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -207,18 +207,7 @@ class OffTopicChannelNameSerializer(ModelSerializer): """Metadata defined for the Django REST Framework.""" 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', 'active') class ReminderSerializer(ModelSerializer): -- cgit v1.2.3 From 69f4dc119af4a8d46295abc2551bad24bee96066 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 31 May 2021 15:27:54 +0530 Subject: Return a list of ot-names only for GET request only to bot/off-topic-channel-names --- pydis_site/apps/api/serializers.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') 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): -- cgit v1.2.3 From da1056f36f77d98783b8fb53152fd4ebbc19c019 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 10 Jun 2021 02:08:54 +0530 Subject: Lint file and remove update() method declaration from OffTopicChannelNameListSerializer. --- pydis_site/apps/api/serializers.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 1acb4fb8..0d505675 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -203,12 +203,12 @@ class ExpandedInfractionSerializer(InfractionSerializer): class OffTopicChannelNameListSerializer(ListSerializer): - def update(self, instance, validated_data): - pass + """Custom ListSerializer to override to_representation() when list views are triggered.""" 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 @@ -222,6 +222,7 @@ class OffTopicChannelNameSerializer(ModelSerializer): class Meta: """Metadata defined for the Django REST Framework.""" + list_serializer_class = OffTopicChannelNameListSerializer model = OffTopicChannelName fields = ('name', 'used', 'active') -- cgit v1.2.3 From e7b4da7955c100d42536e298ffc9966dd9f95c48 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 22 Jul 2021 13:51:28 +0530 Subject: Update docstring. --- pydis_site/apps/api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 0d505675..957c85f3 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -207,7 +207,7 @@ class OffTopicChannelNameListSerializer(ListSerializer): def to_representation(self, objects: List[OffTopicChannelName]) -> List[str]: """ - Return the representation of this `OffTopicChannelName`. + Return a list representing a list of `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. -- cgit v1.2.3 From 41ec1fd7e3d5bd7b0c16a32b5977d46ce5b3e89e Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 5 Nov 2021 10:56:01 +0530 Subject: Eliminate usage of typing module and update docstrings. --- pydis_site/apps/api/serializers.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 957c85f3..9b351be2 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -1,6 +1,4 @@ """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 @@ -205,12 +203,12 @@ class ExpandedInfractionSerializer(InfractionSerializer): class OffTopicChannelNameListSerializer(ListSerializer): """Custom ListSerializer to override to_representation() when list views are triggered.""" - def to_representation(self, objects: List[OffTopicChannelName]) -> List[str]: + def to_representation(self, objects: list[OffTopicChannelName]) -> list[str]: """ - Return a list representing a list of `OffTopicChannelName`. + Return a list with all `OffTopicChannelName`s in the database. - 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. + 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. """ -- cgit v1.2.3