aboutsummaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2018-11-18 23:32:05 +0100
committerGravatar Johannes Christ <[email protected]>2018-11-18 23:32:05 +0100
commit98ff68d1646cb12b7cc3e46e7747195e811fe2ca (patch)
tree6937fe95c4691a17259e389a562b1b0ef72183fe /api
parentHook up the new viewset to the router. (diff)
Add a role API viewset and mount it in the router.
Closes #147.
Diffstat (limited to 'api')
-rw-r--r--api/urls.py10
-rw-r--r--api/viewsets.py103
2 files changed, 104 insertions, 9 deletions
diff --git a/api/urls.py b/api/urls.py
index 7d90e8be..2bca5689 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -5,9 +5,9 @@ from .views import HealthcheckView
from .viewsets import (
DeletedMessageViewSet, DocumentationLinkViewSet,
MemberViewSet, OffTopicChannelNameViewSet,
- SnakeFactViewSet, SnakeIdiomViewSet,
- SnakeNameViewSet, SpecialSnakeViewSet,
- TagViewSet
+ RoleViewSet, SnakeFactViewSet,
+ SnakeIdiomViewSet, SnakeNameViewSet,
+ SpecialSnakeViewSet, TagViewSet
)
@@ -31,6 +31,10 @@ bot_router.register(
MemberViewSet
)
bot_router.register(
+ 'roles',
+ RoleViewSet
+)
+bot_router.register(
'snake-facts',
SnakeFactViewSet
)
diff --git a/api/viewsets.py b/api/viewsets.py
index bc34de1d..8efc999e 100644
--- a/api/viewsets.py
+++ b/api/viewsets.py
@@ -12,16 +12,16 @@ from rest_framework_bulk import BulkCreateModelMixin
from .models import (
DocumentationLink, Member,
MessageDeletionContext, OffTopicChannelName,
- SnakeFact, SnakeIdiom,
- SnakeName, SpecialSnake,
- Tag
+ Role, SnakeFact,
+ SnakeIdiom, SnakeName,
+ SpecialSnake, Tag
)
from .serializers import (
DocumentationLinkSerializer, MemberSerializer,
MessageDeletionContextSerializer, OffTopicChannelNameSerializer,
- SnakeFactSerializer, SnakeIdiomSerializer,
- SnakeNameSerializer, SpecialSnakeSerializer,
- TagSerializer
+ RoleSerializer, SnakeFactSerializer,
+ SnakeIdiomSerializer, SnakeNameSerializer,
+ SpecialSnakeSerializer, TagSerializer
)
@@ -213,6 +213,97 @@ class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
return Response(serialized.data)
+class RoleViewSet(ModelViewSet):
+ """
+ View providing CRUD access to the roles on our server, used
+ by the bot to keep a mirror of our server's roles on the site.
+
+ ## Routes
+ ### GET /bot/roles
+ Returns all roles in the database.
+
+ #### Response format
+ >>> [
+ ... {
+ ... 'id': 267628507062992896,
+ ... 'name': "Admins",
+ ... 'colour': 1337,
+ ... 'permissions': 8
+ ... }
+ ... ]
+
+ #### Status codes
+ - 200: returned on success
+
+ ### GET /bot/roles/<snowflake:int>
+ Gets a single role by ID.
+
+ #### Response format
+ >>> {
+ ... 'id': 267628507062992896,
+ ... 'name': "Admins",
+ ... 'colour': 1337,
+ ... 'permissions': 8
+ ... }
+
+ #### Status codes
+ - 200: returned on success
+ - 404: if a role with the given `snowflake` could not be found
+
+ ### POST /bot/roles
+ Adds a single new role.
+
+ #### Request body
+ >>> {
+ ... 'id': int,
+ ... 'name': str,
+ ... 'colour': int,
+ ... 'permissions': int,
+ ... }
+
+ #### Status codes
+ - 201: returned on success
+ - 400: if the body format is invalid
+
+ ### PUT /bot/roles/<snowflake:int>
+ Update the role with the given `snowflake`.
+ All fields in the request body are required.
+
+ #### Request body
+ >>> {
+ ... 'id': int,
+ ... 'name': str,
+ ... 'colour': int,
+ ... 'permissions': int
+ ... }
+
+ #### Status codes
+ - 200: returned on success
+ - 400: if the request body was invalid
+
+ ### PATCH /bot/roles/<snowflake:int>
+ Update the role with the given `snowflake`.
+ All fields in the request body are required.
+
+ >>> {
+ ... 'id': int,
+ ... 'name': str,
+ ... 'colour': int,
+ ... 'permissions': int
+ ... }
+
+ ### DELETE /bot/roles/<snowflake:int>
+ Deletes the role with the given `snowflake`.
+
+ #### Status codes
+ - 204: returned on success
+ - 404: if a role with the given `snowflake` does not exist
+ """
+
+ queryset = Role.objects.all()
+ serializer = RoleSerializer
+
+
class SnakeFactViewSet(ListModelMixin, GenericViewSet):
"""
View providing snake facts created by the Pydis community in the first code jam.