aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2018-09-23 15:09:28 +0200
committerGravatar Johannes Christ <[email protected]>2018-09-23 15:09:28 +0200
commit152b2ae6afc12cba9f03bc9b136bbf6780deda0e (patch)
treeaeb36d331a8de0ec6b2ac12b484be87d003cc5c0 /api
parentExclude `admin.py` modules. (diff)
Add viewsets and URLs for the Tag API.
Diffstat (limited to 'api')
-rw-r--r--api/serializers.py12
-rw-r--r--api/urls.py8
-rw-r--r--api/viewsets.py110
3 files changed, 125 insertions, 5 deletions
diff --git a/api/serializers.py b/api/serializers.py
index dc4d4a78..c36cce5f 100644
--- a/api/serializers.py
+++ b/api/serializers.py
@@ -1,7 +1,11 @@
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField
from rest_framework_bulk import BulkSerializerMixin
-from .models import DocumentationLink, Member, OffTopicChannelName, Role, SnakeName
+from .models import (
+ DocumentationLink, Member,
+ OffTopicChannelName, Role,
+ SnakeName, Tag
+)
class DocumentationLinkSerializer(ModelSerializer):
@@ -31,6 +35,12 @@ class RoleSerializer(ModelSerializer):
fields = ('id', 'name', 'colour', 'permissions')
+class TagSerializer(ModelSerializer):
+ class Meta:
+ model = Tag
+ fields = ('title', 'embed')
+
+
class MemberSerializer(BulkSerializerMixin, ModelSerializer):
roles = PrimaryKeyRelatedField(many=True, queryset=Role.objects.all())
diff --git a/api/urls.py b/api/urls.py
index f4ed641c..e69f1c73 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -4,7 +4,8 @@ from rest_framework.routers import SimpleRouter
from .views import HealthcheckView
from .viewsets import (
DocumentationLinkViewSet, MemberViewSet,
- OffTopicChannelNameViewSet, SnakeNameViewSet
+ OffTopicChannelNameViewSet, SnakeNameViewSet,
+ TagViewSet
)
@@ -28,7 +29,10 @@ bot_router.register(
SnakeNameViewSet,
base_name='snakename'
)
-
+bot_router.register(
+ 'tags',
+ TagViewSet,
+)
app_name = 'api'
urlpatterns = (
diff --git a/api/viewsets.py b/api/viewsets.py
index 9eec3a03..e3fa219c 100644
--- a/api/viewsets.py
+++ b/api/viewsets.py
@@ -9,10 +9,15 @@ from rest_framework.status import HTTP_201_CREATED
from rest_framework.viewsets import GenericViewSet, ModelViewSet, ViewSet
from rest_framework_bulk import BulkCreateModelMixin
-from .models import DocumentationLink, Member, OffTopicChannelName, SnakeName
+from .models import (
+ DocumentationLink, Member,
+ OffTopicChannelName, SnakeName,
+ Tag
+)
from .serializers import (
DocumentationLinkSerializer, MemberSerializer,
- OffTopicChannelNameSerializer, SnakeNameSerializer
+ OffTopicChannelNameSerializer, SnakeNameSerializer,
+ TagSerializer
)
@@ -225,6 +230,107 @@ class SnakeNameViewSet(ViewSet):
return Response({})
+class TagViewSet(ModelViewSet):
+ """
+ View providing CRUD operations on tags shown by our bot.
+
+ ## Routes
+ ### GET /bot/tags
+ Returns all tags in the database.
+
+ #### Response format
+ >>> [
+ ... {
+ ... 'title': "resources",
+ ... 'embed': {
+ ... 'content': "Did you really think I'd put something useful here?"
+ ... }
+ ... }
+ ... ]
+
+ #### Status codes
+ - 200: returned on success
+
+ ### GET /bot/tags/<title:str>
+ Gets a single tag by its title.
+
+ #### Response format
+ >>> {
+ ... 'title': "My awesome tag",
+ ... 'embed': {
+ ... 'content': "totally not filler words"
+ ... }
+ ... }
+
+ #### Status codes
+ - 200: returned on success
+ - 404: if a tag with the given `title` could not be found
+
+ ### POST /bot/tags
+ Adds a single tag to the database.
+
+ #### Request body
+ >>> {
+ ... 'title': str,
+ ... 'embed': dict
+ ... }
+
+ The embed structure is the same as the embed structure that the Discord API
+ expects. You can view the documentation for it here:
+ https://discordapp.com/developers/docs/resources/channel#embed-object
+
+ #### Status codes
+ - 201: returned on success
+ - 400: if one of the given fields is invalid
+
+ ### PUT /bot/members/<title:str>
+ Update the tag with the given `title`.
+
+ #### Request body
+ >>> {
+ ... 'title': str,
+ ... 'embed': dict
+ ... }
+
+ The embed structure is the same as the embed structure that the Discord API
+ expects. You can view the documentation for it here:
+ https://discordapp.com/developers/docs/resources/channel#embed-object
+
+ #### Status codes
+ - 200: returned on success
+ - 400: if the request body was invalid, see response body for details
+ - 404: if the tag with the given `title` could not be found
+
+ ### PATCH /bot/members/<title:str>
+ Update the tag with the given `title`.
+
+ #### Request body
+ >>> {
+ ... 'title': str,
+ ... 'embed': dict
+ ... }
+
+ The embed structure is the same as the embed structure that the Discord API
+ expects. You can view the documentation for it here:
+ https://discordapp.com/developers/docs/resources/channel#embed-object
+
+ #### Status codes
+ - 200: returned on success
+ - 400: if the request body was invalid, see response body for details
+ - 404: if the tag with the given `title` could not be found
+
+ ### DELETE /bot/members/<title:str>
+ Deletes the tag with the given `title`.
+
+ #### Status codes
+ - 204: returned on success
+ - 404: if a tag with the given `title` does not exist
+ """
+
+ serializer_class = TagSerializer
+ queryset = Tag.objects.all()
+
+
class MemberViewSet(BulkCreateModelMixin, ModelViewSet):
"""
View providing CRUD operations on our Discord server's members through the bot.