diff options
author | 2022-01-22 01:54:15 +0300 | |
---|---|---|
committer | 2022-01-22 01:54:15 +0300 | |
commit | 3e786da5537cc243097b4bedb8e6b78ad1525f95 (patch) | |
tree | eb5fc12d7cec3e4623a03483a33370fcd28559d1 /backend/routes/roles.py | |
parent | Merge pull request #132 from python-discord/anon-webhooks (diff) |
Add Helper Functions For Managing Roles
Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'backend/routes/roles.py')
-rw-r--r-- | backend/routes/roles.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/backend/routes/roles.py b/backend/routes/roles.py new file mode 100644 index 0000000..b18a04b --- /dev/null +++ b/backend/routes/roles.py @@ -0,0 +1,36 @@ +import starlette.background +from pymongo.database import Database +from spectree import Response +from starlette.authentication import requires +from starlette.responses import JSONResponse +from starlette.routing import Request + +from backend import discord, route +from backend.validation import OkayResponse, api + + +async def refresh_roles(database: Database) -> None: + """Connect to the discord API and refresh the roles database.""" + roles = await discord.get_role_info() + roles_collection = database.get_collection("roles") + roles_collection.drop() + roles_collection.insert_many([role.dict() for role in roles]) + + +class RolesRoute(route.Route): + """Refreshes the roles database.""" + + name = "roles" + path = "/roles" + + @requires(["authenticated", "admin"]) + @api.validate( + resp=Response(HTTP_200=OkayResponse), + tags=["roles"] + ) + async def patch(self, request: Request) -> JSONResponse: + """Refresh the roles database.""" + return JSONResponse( + {"status": "ok"}, + background=starlette.background.BackgroundTask(refresh_roles, request.state.db) + ) |