diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/urls.py | 6 | ||||
-rw-r--r-- | api/viewsets.py | 100 |
2 files changed, 101 insertions, 5 deletions
diff --git a/api/urls.py b/api/urls.py index af275381..7d6a4f7d 100644 --- a/api/urls.py +++ b/api/urls.py @@ -4,7 +4,7 @@ from rest_framework.routers import DefaultRouter from .views import HealthcheckView from .viewsets import ( DocumentationLinkViewSet, InfractionViewSet, - OffTopicChannelNameViewSet, + OffTopicChannelNameViewSet, RoleViewSet, SnakeFactViewSet, SnakeIdiomViewSet, SnakeNameViewSet, SpecialSnakeViewSet, TagViewSet, UserViewSet @@ -27,6 +27,10 @@ bot_router.register( base_name='offtopicchannelname' ) bot_router.register( + 'roles', + RoleViewSet +) +bot_router.register( 'snake-facts', SnakeFactViewSet ) diff --git a/api/viewsets.py b/api/viewsets.py index aa5d5fd3..ab62f8a7 100644 --- a/api/viewsets.py +++ b/api/viewsets.py @@ -14,7 +14,7 @@ from rest_framework_bulk import BulkCreateModelMixin from .models import ( DocumentationLink, Infraction, - OffTopicChannelName, + OffTopicChannelName, Role, SnakeFact, SnakeIdiom, SnakeName, SpecialSnake, Tag, User @@ -22,9 +22,10 @@ from .models import ( from .serializers import ( DocumentationLinkSerializer, ExpandedInfractionSerializer, InfractionSerializer, OffTopicChannelNameSerializer, - SnakeFactSerializer, SnakeIdiomSerializer, - SnakeNameSerializer, SpecialSnakeSerializer, - TagSerializer, UserSerializer + RoleSerializer, SnakeFactSerializer, + SnakeIdiomSerializer, SnakeNameSerializer, + SpecialSnakeSerializer, TagSerializer, + UserSerializer ) @@ -319,6 +320,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_class = RoleSerializer + + class SnakeFactViewSet(ListModelMixin, GenericViewSet): """ View providing snake facts created by the Pydis community in the first code jam. |