diff options
author | 2020-12-26 23:13:53 +0000 | |
---|---|---|
committer | 2020-12-26 23:13:53 +0000 | |
commit | ff914d45be250081bb321d7637364d1550edaf6b (patch) | |
tree | cad1fe0200ac407730e9b49664e73814487f2561 | |
parent | Let Pydantic validate bulk responses delete data (diff) | |
parent | Merge pull request #44 from python-discord/ks123/admin-adding (diff) |
Merge branch 'main' into ks123/responses-bulk-delete
-rw-r--r-- | backend/routes/admin.py | 41 | ||||
-rw-r--r-- | poetry.lock | 19 | ||||
-rw-r--r-- | pyproject.toml | 2 |
3 files changed, 52 insertions, 10 deletions
diff --git a/backend/routes/admin.py b/backend/routes/admin.py new file mode 100644 index 0000000..5254f8b --- /dev/null +++ b/backend/routes/admin.py @@ -0,0 +1,41 @@ +""" +Adds new admin user. +""" +from pydantic import BaseModel, Field +from spectree import Response +from starlette.authentication import requires +from starlette.requests import Request +from starlette.responses import JSONResponse + +from backend.route import Route +from backend.validation import ErrorMessage, OkayResponse, api + + +class AdminModel(BaseModel): + id: str = Field(alias="_id") + + +class AdminRoute(Route): + """Adds new admin user.""" + + name = "admin" + path = "/admin" + + @requires(["authenticated", "admin"]) + @api.validate( + json=AdminModel, + resp=Response(HTTP_200=OkayResponse, HTTP_400=ErrorMessage), + tags=["admin"] + ) + async def post(self, request: Request) -> JSONResponse: + """Grant a user administrator privileges.""" + data = await request.json() + admin = AdminModel(**data) + + if await request.state.db.admins.find_one( + {"_id": admin.id} + ): + return JSONResponse({"error": "already_exists"}, status_code=400) + + await request.state.db.admins.insert_one(admin.dict(by_alias=True)) + return JSONResponse({"status": "ok"}) diff --git a/poetry.lock b/poetry.lock index 785fec6..cf19c08 100644 --- a/poetry.lock +++ b/poetry.lock @@ -186,16 +186,17 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" [[package]] name = "pyjwt" -version = "1.7.1" +version = "2.0.0" description = "JSON Web Token implementation in Python" category = "main" optional = false -python-versions = "*" +python-versions = ">=3.6" [package.extras] -crypto = ["cryptography (>=1.4)"] -flake8 = ["flake8", "flake8-import-order", "pep8-naming"] -test = ["pytest (>=4.0.1,<5.0.0)", "pytest-cov (>=2.6.0,<3.0.0)", "pytest-runner (>=4.2,<5.0.0)"] +crypto = ["cryptography (>=3.3.1,<4.0.0)"] +dev = ["sphinx", "sphinx-rtd-theme", "zope.interface", "cryptography (>=3.3.1,<4.0.0)", "pytest (>=6.0.0,<7.0.0)", "coverage[toml] (==5.0.4)", "mypy", "pre-commit"] +docs = ["sphinx", "sphinx-rtd-theme", "zope.interface"] +tests = ["pytest (>=6.0.0,<7.0.0)", "coverage[toml] (==5.0.4)"] [[package]] name = "pymongo" @@ -232,7 +233,7 @@ version = "5.3.1" description = "YAML parser and emitter for Python" category = "main" optional = false -python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +python-versions = "*" [[package]] name = "rfc3986" @@ -333,7 +334,7 @@ python-versions = ">=3.6.1" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "7584e4eacc1b2615adce06899fe6e6b8d696955fea97533ccac2bd2c642e136f" +content-hash = "d863036c29d41783f127e3ebd0dce61349c5c071d5de438ab208863505d00c2d" [metadata.files] certifi = [ @@ -438,8 +439,8 @@ pyflakes = [ {file = "pyflakes-2.2.0.tar.gz", hash = "sha256:35b2d75ee967ea93b55750aa9edbbf72813e06a66ba54438df2cfac9e3c27fc8"}, ] pyjwt = [ - {file = "PyJWT-1.7.1-py2.py3-none-any.whl", hash = "sha256:5c6eca3c2940464d106b99ba83b00c6add741c9becaec087fb7ccdefea71350e"}, - {file = "PyJWT-1.7.1.tar.gz", hash = "sha256:8d59a976fb773f3e6a39c85636357c4f0e242707394cadadd9814f5cbaa20e96"}, + {file = "PyJWT-2.0.0-py3-none-any.whl", hash = "sha256:5c2ff2eb27d7e342dfc3cafcc16412781f06db2690fbef81922b0172598f085b"}, + {file = "PyJWT-2.0.0.tar.gz", hash = "sha256:7a2b271c6dac2fda9e0c33d176c4253faba2c6c6b3a99c7f28a32c3c97522779"}, ] pymongo = [ {file = "pymongo-3.11.2-cp27-cp27m-macosx_10_14_intel.whl", hash = "sha256:9be785bd4e1ba0148fb00ca84e4dbfbd1c74df3af3a648559adc60b0782f34de"}, diff --git a/pyproject.toml b/pyproject.toml index 642fb6c..8dd723d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ nested_dict = "^1.61" uvicorn = {extras = ["standard"], version = "^0.13.0"} motor = "^2.3.0" python-dotenv = "^0.15.0" -pyjwt = "^1.7.1" +pyjwt = "^2.0.0" httpx = "^0.16.1" gunicorn = "^20.0.4" pydantic = "^1.7.2" |