From d061ab1c138c7cecf8d138226990bdcf0761a5db Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Wed, 19 May 2021 13:13:57 +0530 Subject: Add active field to OffTopicChannelName model. --- .../apps/api/migrations/0070_auto_20210519_0545.py | 23 ++++++++++++++++++++++ .../apps/api/models/bot/off_topic_channel_name.py | 7 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 pydis_site/apps/api/migrations/0070_auto_20210519_0545.py (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/migrations/0070_auto_20210519_0545.py b/pydis_site/apps/api/migrations/0070_auto_20210519_0545.py new file mode 100644 index 00000000..dbd7ac91 --- /dev/null +++ b/pydis_site/apps/api/migrations/0070_auto_20210519_0545.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.14 on 2021-05-19 05:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0069_documentationlink_validators'), + ] + + operations = [ + migrations.AddField( + model_name='offtopicchannelname', + name='active', + field=models.BooleanField(default=True, help_text='Whether or not this name should be considered for naming channels.'), + ), + migrations.AlterField( + model_name='offtopicchannelname', + name='used', + field=models.BooleanField(default=False, help_text='Whether or not this name has already been used during this rotation.'), + ), + ] diff --git a/pydis_site/apps/api/models/bot/off_topic_channel_name.py b/pydis_site/apps/api/models/bot/off_topic_channel_name.py index 403c7465..582c069e 100644 --- a/pydis_site/apps/api/models/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/models/bot/off_topic_channel_name.py @@ -18,7 +18,12 @@ class OffTopicChannelName(ModelReprMixin, models.Model): used = models.BooleanField( default=False, - help_text="Whether or not this name has already been used during this rotation", + help_text="Whether or not this name has already been used during this rotation.", + ) + + active = models.BooleanField( + default=True, + help_text="Whether or not this name should be considered for naming channels." ) def __str__(self): -- cgit v1.2.3 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') 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 b2819a4cfe04b893d5cd638407e48ff6bd501a20 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Wed, 19 May 2021 13:16:21 +0530 Subject: Use ModelViewSet to add support for PUT and PATCH request. --- pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py index 826ad25e..194e96d2 100644 --- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py @@ -3,16 +3,15 @@ from django.db.models.query import QuerySet from django.http.request import HttpRequest from django.shortcuts import get_object_or_404 from rest_framework.exceptions import ParseError -from rest_framework.mixins import DestroyModelMixin from rest_framework.response import Response from rest_framework.status import HTTP_201_CREATED -from rest_framework.viewsets import ViewSet +from rest_framework.viewsets import ModelViewSet from pydis_site.apps.api.models.bot.off_topic_channel_name import OffTopicChannelName from pydis_site.apps.api.serializers import OffTopicChannelNameSerializer -class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): +class OffTopicChannelNameViewSet(ModelViewSet): """ View of off-topic channel names used by the bot to rotate our off-topic names on a daily basis. @@ -73,7 +72,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): """Returns a queryset that covers the entire OffTopicChannelName table.""" return OffTopicChannelName.objects.all() - def create(self, request: HttpRequest) -> Response: + def create(self, request: HttpRequest, *args, **kwargs) -> Response: """ DRF method for creating a new OffTopicChannelName. @@ -91,7 +90,7 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): 'name': ["This query parameter is required."] }) - def list(self, request: HttpRequest) -> Response: + def list(self, request: HttpRequest, *args, **kwargs) -> Response: """ DRF method for listing OffTopicChannelName entries. @@ -133,6 +132,4 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet): serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) - queryset = self.get_queryset() - serialized = self.serializer_class(queryset, many=True) - return Response(serialized.data) + return super().list(self, request) -- 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') 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 87e25f77cbc900024e86d439abd9fde71181800d Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 4 Jun 2021 21:46:54 +0530 Subject: Query off topic names based on Active attribute. --- .../apps/api/viewsets/bot/off_topic_channel_name.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py index 194e96d2..0caf8088 100644 --- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py @@ -1,9 +1,11 @@ +import json + from django.db.models import Case, Value, When from django.db.models.query import QuerySet -from django.http.request import HttpRequest from django.shortcuts import get_object_or_404 from rest_framework.exceptions import ParseError from rest_framework.response import Response +from rest_framework.request import Request from rest_framework.status import HTTP_201_CREATED from rest_framework.viewsets import ModelViewSet @@ -68,11 +70,11 @@ class OffTopicChannelNameViewSet(ModelViewSet): name = self.kwargs[self.lookup_field] return get_object_or_404(queryset, name=name) - def get_queryset(self) -> QuerySet: + def get_queryset(self, active=True) -> QuerySet: """Returns a queryset that covers the entire OffTopicChannelName table.""" - return OffTopicChannelName.objects.all() + return OffTopicChannelName.objects.filter(active=True) - def create(self, request: HttpRequest, *args, **kwargs) -> Response: + def create(self, request: Request, *args, **kwargs) -> Response: """ DRF method for creating a new OffTopicChannelName. @@ -90,7 +92,7 @@ class OffTopicChannelNameViewSet(ModelViewSet): 'name': ["This query parameter is required."] }) - def list(self, request: HttpRequest, *args, **kwargs) -> Response: + def list(self, request: Request, *args, **kwargs) -> Response: """ DRF method for listing OffTopicChannelName entries. @@ -132,4 +134,6 @@ class OffTopicChannelNameViewSet(ModelViewSet): serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) - return super().list(self, request) + queryset = self.get_queryset(active=bool(request.query_params.get("active", True))) + serialized = self.serializer_class(queryset, many=True) + return Response(serialized.data) -- 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') 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 f0534b8e1ed55c18416372f23bb5a1f2e0862e38 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 10 Jun 2021 02:09:22 +0530 Subject: Fix bug: Do not force active param. --- .../apps/api/viewsets/bot/off_topic_channel_name.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py index 0caf8088..18ee84ea 100644 --- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py @@ -1,11 +1,9 @@ -import json - from django.db.models import Case, Value, When from django.db.models.query import QuerySet from django.shortcuts import get_object_or_404 from rest_framework.exceptions import ParseError -from rest_framework.response import Response from rest_framework.request import Request +from rest_framework.response import Response from rest_framework.status import HTTP_201_CREATED from rest_framework.viewsets import ModelViewSet @@ -70,9 +68,9 @@ class OffTopicChannelNameViewSet(ModelViewSet): name = self.kwargs[self.lookup_field] return get_object_or_404(queryset, name=name) - def get_queryset(self, active=True) -> QuerySet: + def get_queryset(self, **kwargs) -> QuerySet: """Returns a queryset that covers the entire OffTopicChannelName table.""" - return OffTopicChannelName.objects.filter(active=True) + return OffTopicChannelName.objects.filter(**kwargs) def create(self, request: Request, *args, **kwargs) -> Response: """ @@ -134,6 +132,11 @@ class OffTopicChannelNameViewSet(ModelViewSet): serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) - queryset = self.get_queryset(active=bool(request.query_params.get("active", True))) + params = {} + + if active_param := request.query_params.get("active"): + params["active"] = active_param.lower() == "true" + + queryset = self.get_queryset(**params) serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) -- cgit v1.2.3 From 832880cfac4206aaba0e7de8f005c6425da7a8f3 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 10 Jun 2021 02:11:40 +0530 Subject: Add tests for active params. --- .../apps/api/tests/test_off_topic_channel_names.py | 38 +++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index 3ab8b22d..a407654c 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -65,8 +65,15 @@ class EmptyDatabaseTests(APISubdomainTestCase): class ListTests(APISubdomainTestCase): @classmethod def setUpTestData(cls): - cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand', used=False) - cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk', used=True) + cls.test_name = OffTopicChannelName.objects.create( + name='lemons-lemonade-stand', used=False, active=True + ) + cls.test_name_2 = OffTopicChannelName.objects.create( + name='bbq-with-bisk', used=True, active=True + ) + cls.test_name_3 = OffTopicChannelName.objects.create( + name="frozen-with-iceman", used=True, active=False + ) def test_returns_name_in_list(self): """Return all off-topic channel names.""" @@ -78,7 +85,8 @@ class ListTests(APISubdomainTestCase): response.json(), [ self.test_name.name, - self.test_name_2.name + self.test_name_2.name, + self.test_name_3.name ] ) @@ -97,7 +105,29 @@ class ListTests(APISubdomainTestCase): response = self.client.get(f'{url}?random_items=2') self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), [self.test_name.name, self.test_name_2.name]) + self.assertEqual(response.json(), [self.test_name.name, self.test_name_3.name]) + + def test_returns_inactive_ot_names(self): + """Return inactive off topic names.""" + url = reverse('bot:offtopicchannelname-list', host="api") + response = self.client.get(f"{url}?active=false") + + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.json(), + [self.test_name_3.name] + ) + + def test_returns_active_ot_names(self): + """Return active off topic names.""" + url = reverse('bot:offtopicchannelname-list', host="api") + response = self.client.get(f"{url}?active=true") + + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.json(), + [self.test_name.name, self.test_name_2.name] + ) class CreationTests(APISubdomainTestCase): -- cgit v1.2.3 From 3795b6d6de005f0ed00c37cf042eaca01d0a4769 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 10 Jun 2021 02:32:17 +0530 Subject: Use assertListEqual where applicable instead of assertEqual. --- .../apps/api/tests/test_off_topic_channel_names.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index a407654c..ebb1224a 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -69,7 +69,7 @@ class ListTests(APISubdomainTestCase): name='lemons-lemonade-stand', used=False, active=True ) cls.test_name_2 = OffTopicChannelName.objects.create( - name='bbq-with-bisk', used=True, active=True + name='bbq-with-bisk', used=False, active=True ) cls.test_name_3 = OffTopicChannelName.objects.create( name="frozen-with-iceman", used=True, active=False @@ -81,7 +81,7 @@ class ListTests(APISubdomainTestCase): response = self.client.get(url) self.assertEqual(response.status_code, 200) - self.assertEqual( + self.assertListEqual( response.json(), [ self.test_name.name, @@ -90,22 +90,24 @@ class ListTests(APISubdomainTestCase): ] ) - def test_returns_single_item_with_random_items_param_set_to_1(self): + def test_returns_two_items_with_random_items_param_set_to_2(self): """Return not-used name instead used.""" url = reverse('bot:offtopicchannelname-list', host='api') - response = self.client.get(f'{url}?random_items=1') + response = self.client.get(f'{url}?random_items=2') self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.json()), 1) - self.assertEqual(response.json(), [self.test_name.name]) + self.assertEqual(len(response.json()), 2) + self.assertEqual(response.json(), [self.test_name.name, self.test_name_2.name]) def test_running_out_of_names_with_random_parameter(self): """Reset names `used` parameter to `False` when running out of names.""" url = reverse('bot:offtopicchannelname-list', host='api') - response = self.client.get(f'{url}?random_items=2') + response = self.client.get(f'{url}?random_items=3') self.assertEqual(response.status_code, 200) - self.assertEqual(response.json(), [self.test_name.name, self.test_name_3.name]) + self.assertListEqual( + response.json(), [self.test_name.name, self.test_name_2.name, self.test_name_3.name] + ) def test_returns_inactive_ot_names(self): """Return inactive off topic names.""" -- cgit v1.2.3 From 8fdabfb4c08931b1e2352e98b307b3bfa3a121f1 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Thu, 10 Jun 2021 02:45:55 +0530 Subject: Use sets to compare 2 un-ordered lists. --- .../apps/api/tests/test_off_topic_channel_names.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index ebb1224a..34dde7c6 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -81,13 +81,13 @@ class ListTests(APISubdomainTestCase): response = self.client.get(url) self.assertEqual(response.status_code, 200) - self.assertListEqual( - response.json(), - [ + self.assertEqual( + set(response.json()), + { self.test_name.name, self.test_name_2.name, self.test_name_3.name - ] + } ) def test_returns_two_items_with_random_items_param_set_to_2(self): @@ -97,7 +97,7 @@ class ListTests(APISubdomainTestCase): self.assertEqual(response.status_code, 200) self.assertEqual(len(response.json()), 2) - self.assertEqual(response.json(), [self.test_name.name, self.test_name_2.name]) + self.assertEqual(set(response.json()), {self.test_name.name, self.test_name_2.name}) def test_running_out_of_names_with_random_parameter(self): """Reset names `used` parameter to `False` when running out of names.""" @@ -105,8 +105,9 @@ class ListTests(APISubdomainTestCase): response = self.client.get(f'{url}?random_items=3') self.assertEqual(response.status_code, 200) - self.assertListEqual( - response.json(), [self.test_name.name, self.test_name_2.name, self.test_name_3.name] + self.assertEqual( + set(response.json()), + {self.test_name.name, self.test_name_2.name, self.test_name_3.name} ) def test_returns_inactive_ot_names(self): @@ -127,8 +128,8 @@ class ListTests(APISubdomainTestCase): self.assertEqual(response.status_code, 200) self.assertEqual( - response.json(), - [self.test_name.name, self.test_name_2.name] + set(response.json()), + {self.test_name.name, self.test_name_2.name} ) -- 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') 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 d52f21c1fefbb396762dd74609b9957ae029dfc3 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Sat, 24 Jul 2021 19:26:36 +0530 Subject: fix CommandError: Conflicting migrations detected. --- pydis_site/apps/api/migrations/0072_merge_20210724_1354.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 pydis_site/apps/api/migrations/0072_merge_20210724_1354.py (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/migrations/0072_merge_20210724_1354.py b/pydis_site/apps/api/migrations/0072_merge_20210724_1354.py new file mode 100644 index 00000000..f12efab5 --- /dev/null +++ b/pydis_site/apps/api/migrations/0072_merge_20210724_1354.py @@ -0,0 +1,14 @@ +# Generated by Django 3.0.14 on 2021-07-24 13:54 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0071_increase_message_content_4000'), + ('api', '0070_auto_20210519_0545'), + ] + + operations = [ + ] -- cgit v1.2.3 From 2b95db672e649d9fc1159b3e2211dcb72560d8f6 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 5 Nov 2021 10:49:30 +0530 Subject: Fix: Conflicting migrations. --- pydis_site/apps/api/migrations/0074_merge_20211105_0518.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 pydis_site/apps/api/migrations/0074_merge_20211105_0518.py (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/migrations/0074_merge_20211105_0518.py b/pydis_site/apps/api/migrations/0074_merge_20211105_0518.py new file mode 100644 index 00000000..ebf5ae15 --- /dev/null +++ b/pydis_site/apps/api/migrations/0074_merge_20211105_0518.py @@ -0,0 +1,14 @@ +# Generated by Django 3.0.14 on 2021-11-05 05:18 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0072_merge_20210724_1354'), + ('api', '0073_otn_allow_GT_and_LT'), + ] + + operations = [ + ] -- cgit v1.2.3 From 876daaf6eb34e620710473c0311e23f36fd4e7eb Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 5 Nov 2021 10:55:09 +0530 Subject: Update test cases to adhere to recent changes made that removed hosts. --- pydis_site/apps/api/tests/test_off_topic_channel_names.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/tests/test_off_topic_channel_names.py b/pydis_site/apps/api/tests/test_off_topic_channel_names.py index 354cda9c..2d273756 100644 --- a/pydis_site/apps/api/tests/test_off_topic_channel_names.py +++ b/pydis_site/apps/api/tests/test_off_topic_channel_names.py @@ -112,7 +112,7 @@ class ListTests(AuthenticatedAPITestCase): def test_returns_inactive_ot_names(self): """Return inactive off topic names.""" - url = reverse('bot:offtopicchannelname-list') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(f"{url}?active=false") self.assertEqual(response.status_code, 200) @@ -123,7 +123,7 @@ class ListTests(AuthenticatedAPITestCase): def test_returns_active_ot_names(self): """Return active off topic names.""" - url = reverse('bot:offtopicchannelname-list') + url = reverse('api:bot:offtopicchannelname-list') response = self.client.get(f"{url}?active=true") self.assertEqual(response.status_code, 200) -- 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') 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 From 6e1d38d6ceb97f287f0d347e974bc795dda38ffe Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Fri, 5 Nov 2021 10:56:34 +0530 Subject: Use framework defination of method. --- pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py index 7151d29b..27eabec9 100644 --- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py @@ -70,7 +70,7 @@ class OffTopicChannelNameViewSet(ModelViewSet): def get_queryset(self, **kwargs) -> QuerySet: """Returns a queryset that covers the entire OffTopicChannelName table.""" - return OffTopicChannelName.objects.filter(**kwargs) + return OffTopicChannelName.objects.all() def create(self, request: Request, *args, **kwargs) -> Response: """ @@ -137,6 +137,6 @@ class OffTopicChannelNameViewSet(ModelViewSet): if active_param := request.query_params.get("active"): params["active"] = active_param.lower() == "true" - queryset = self.get_queryset(**params) + queryset = self.get_queryset().filter(**params) serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) -- cgit v1.2.3 From 4377c6d960e3fbf3a416230d1ad7de972019898e Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Sat, 6 Nov 2021 14:43:17 +0530 Subject: Remove get_queryset() and add new class variable . --- pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py index 27eabec9..78f8c340 100644 --- a/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py +++ b/pydis_site/apps/api/viewsets/bot/off_topic_channel_name.py @@ -57,6 +57,7 @@ class OffTopicChannelNameViewSet(ModelViewSet): lookup_field = 'name' serializer_class = OffTopicChannelNameSerializer + queryset = OffTopicChannelName.objects.all() def get_object(self) -> OffTopicChannelName: """ @@ -64,11 +65,10 @@ class OffTopicChannelNameViewSet(ModelViewSet): If it doesn't, a HTTP 404 is returned by way of throwing an exception. """ - queryset = self.get_queryset() name = self.kwargs[self.lookup_field] - return get_object_or_404(queryset, name=name) + return get_object_or_404(self.queryset, name=name) - def get_queryset(self, **kwargs) -> QuerySet: + def get_queryset(self) -> QuerySet: """Returns a queryset that covers the entire OffTopicChannelName table.""" return OffTopicChannelName.objects.all() @@ -108,13 +108,13 @@ class OffTopicChannelNameViewSet(ModelViewSet): 'random_items': ["Must be a positive integer."] }) - queryset = self.get_queryset().order_by('used', '?')[:random_count] + queryset = self.queryset.order_by('used', '?')[:random_count] # When any name is used in our listing then this means we reached end of round # and we need to reset all other names `used` to False if any(offtopic_name.used for offtopic_name in queryset): # These names that we just got have to be excluded from updating used to False - self.get_queryset().update( + self.queryset.update( used=Case( When( name__in=(offtopic_name.name for offtopic_name in queryset), @@ -125,7 +125,7 @@ class OffTopicChannelNameViewSet(ModelViewSet): ) else: # Otherwise mark selected names `used` to True - self.get_queryset().filter( + self.queryset.filter( name__in=(offtopic_name.name for offtopic_name in queryset) ).update(used=True) @@ -137,6 +137,6 @@ class OffTopicChannelNameViewSet(ModelViewSet): if active_param := request.query_params.get("active"): params["active"] = active_param.lower() == "true" - queryset = self.get_queryset().filter(**params) + queryset = self.queryset.filter(**params) serialized = self.serializer_class(queryset, many=True) return Response(serialized.data) -- cgit v1.2.3 From 1261efd75c10bf2031a2019475d4b32c5859d807 Mon Sep 17 00:00:00 2001 From: RohanJnr Date: Mon, 13 Dec 2021 11:25:27 +0530 Subject: Merge migrations to avoid conflicts. --- pydis_site/apps/api/migrations/0078_merge_20211213_0552.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 pydis_site/apps/api/migrations/0078_merge_20211213_0552.py (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/api/migrations/0078_merge_20211213_0552.py b/pydis_site/apps/api/migrations/0078_merge_20211213_0552.py new file mode 100644 index 00000000..5ce0e871 --- /dev/null +++ b/pydis_site/apps/api/migrations/0078_merge_20211213_0552.py @@ -0,0 +1,14 @@ +# Generated by Django 3.1.14 on 2021-12-13 05:52 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0077_use_generic_jsonfield'), + ('api', '0074_merge_20211105_0518'), + ] + + operations = [ + ] -- cgit v1.2.3