aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2018-09-01 18:42:06 +0200
committerGravatar Johannes Christ <[email protected]>2018-09-01 18:42:06 +0200
commitf74038af12c577aa5b6ed6f87180f880f0ce8edc (patch)
treeab5e2c21937a0cbc93c7e2e8530c6a566c916ad1 /api
parentIgnore migrations file in `flake8`. (diff)
Add support for deleting off-topic-channel names.
Diffstat (limited to 'api')
-rw-r--r--api/tests/test_off_topic_channel_names.py29
-rw-r--r--api/viewsets.py21
2 files changed, 49 insertions, 1 deletions
diff --git a/api/tests/test_off_topic_channel_names.py b/api/tests/test_off_topic_channel_names.py
index 96cb3b34..1f6e4ee3 100644
--- a/api/tests/test_off_topic_channel_names.py
+++ b/api/tests/test_off_topic_channel_names.py
@@ -112,3 +112,32 @@ class CreationTests(APISubdomainTestCase):
self.assertEqual(response.json(), {
'name': ["Enter a valid value."]
})
+
+
+class DeletionTests(APISubdomainTestCase):
+ @classmethod
+ def setUpTestData(cls):
+ cls.test_name = OffTopicChannelName.objects.create(name='lemons-lemonade-stand')
+ cls.test_name_2 = OffTopicChannelName.objects.create(name='bbq-with-bisk')
+
+ def test_deleting_unknown_name_returns_404(self):
+ url = reverse('bot:offtopicchannelname-detail', args=('unknown-name',), host='api')
+ response = self.client.delete(url)
+
+ self.assertEqual(response.status_code, 404)
+
+ def test_deleting_known_name_returns_204(self):
+ url = reverse('bot:offtopicchannelname-detail', args=(self.test_name.name,), host='api')
+ response = self.client.delete(url)
+
+ self.assertEqual(response.status_code, 204)
+
+ def test_name_gets_deleted(self):
+ url = reverse('bot:offtopicchannelname-detail', args=(self.test_name_2.name,), host='api')
+ response = self.client.delete(url)
+
+ self.assertEqual(response.status_code, 204)
+
+ url = reverse('bot:offtopicchannelname-list', host='api')
+ response = self.client.get(url)
+ self.assertNotIn(self.test_name_2.name, response.json())
diff --git a/api/viewsets.py b/api/viewsets.py
index 0bbb70f2..82f8aea9 100644
--- a/api/viewsets.py
+++ b/api/viewsets.py
@@ -1,3 +1,4 @@
+from django.shortcuts import get_object_or_404
from rest_framework.exceptions import ParseError
from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin
from rest_framework.response import Response
@@ -70,7 +71,7 @@ class DocumentationLinkViewSet(CreateModelMixin, DestroyModelMixin, ListModelMix
lookup_field = 'package'
-class OffTopicChannelNameViewSet(ViewSet):
+class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
"""
View of off-topic channel names used by the bot
to rotate our off-topic names on a daily basis.
@@ -102,12 +103,30 @@ class OffTopicChannelNameViewSet(ViewSet):
- 201: returned on success
- 400: if the request body has invalid fields, see the response for details
+ ### DELETE /bot/off-topic-channel-names/<name:str>
+ Delete the off-topic-channel name with the given `name`.
+
+ #### Status codes
+ - 204: returned on success
+ - 404: returned when the given `name` was not found
+
## Authentication
Requires a API token.
"""
+ lookup_field = 'name'
serializer_class = OffTopicChannelNameSerializer
+ def get_object(self):
+ queryset = self.get_queryset()
+ if self.lookup_field not in self.kwargs:
+ raise ParseError(detail={
+ 'name': ["This query parameter is required."]
+ })
+
+ name = self.kwargs[self.lookup_field]
+ return get_object_or_404(queryset, name=name)
+
def get_queryset(self):
return OffTopicChannelName.objects.all()