From a09f40fa3585380a2967649636a2a80b0d15e029 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 25 Mar 2024 19:20:23 +0100 Subject: [PATCH] add cors and fix cyclic import --- foxnouns/app.py | 11 +++++++++-- foxnouns/db/user.py | 7 +------ poetry.lock | 17 ++++++++++++++++- pyproject.toml | 1 + 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/foxnouns/app.py b/foxnouns/app.py index ca5bb7f..84e4a11 100644 --- a/foxnouns/app.py +++ b/foxnouns/app.py @@ -1,4 +1,5 @@ from quart import Quart, g, request +from quart_cors import cors from quart_schema import QuartSchema, RequestSchemaValidationError from .blueprints import members_blueprint, users_blueprint @@ -7,6 +8,13 @@ from .db.util import validate_token from .exceptions import ExpectedError app = Quart(__name__) +app = cors( + app, + allow_origin="*", + allow_methods="*", + allow_headers=["Content-Type", "Authorization", "User-Agent"], + max_age=86400, +) QuartSchema(app) for bp in (users_blueprint, members_blueprint): @@ -39,6 +47,5 @@ async def get_user_from_token(): token, user = await validate_token(session, token) g.token = token g.user = user - except Exception as e: - print(e) + except: raise diff --git a/foxnouns/db/user.py b/foxnouns/db/user.py index ad65237..9c2050d 100644 --- a/foxnouns/db/user.py +++ b/foxnouns/db/user.py @@ -30,7 +30,7 @@ class User(Base): auth_methods: Mapped[list["AuthMethod"]] = relationship( back_populates="user", cascade="all, delete-orphan" ) - members: Mapped[list["Member"]] = relationship( + members: Mapped[list["Member"]] = relationship( # noqa: F821 back_populates="user", cascade="all, delete-orphan" ) @@ -38,11 +38,6 @@ class User(Base): return f"User(id={self.id!r}, username={self.username!r})" -# Import Member here--it's needed for the back reference in User, but Member references User, so we can only import it -# after User is initialized. -from .member import Member # noqa: E402 - - class Token(Base): __tablename__ = "tokens" diff --git a/poetry.lock b/poetry.lock index 37dfcd3..69a2dd6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -966,6 +966,21 @@ werkzeug = ">=3.0.0" docs = ["pydata_sphinx_theme"] dotenv = ["python-dotenv"] +[[package]] +name = "quart-cors" +version = "0.7.0" +description = "A Quart extension to provide Cross Origin Resource Sharing, access control, support" +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "quart_cors-0.7.0-py3-none-any.whl", hash = "sha256:fa872cc94a2ae6b51a35b028ebca65c14069d7121d63a4caa3526ebbfb7c5a99"}, + {file = "quart_cors-0.7.0.tar.gz", hash = "sha256:d667a0f13b4ce6d9e926489de5d819780844fbff5b2cdea156bd8867dd426a37"}, +] + +[package.dependencies] +quart = ">=0.15" + [[package]] name = "quart-schema" version = "0.19.1" @@ -1240,4 +1255,4 @@ h11 = ">=0.9.0,<1" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "a04cf866b1b5efd1f9484114b0e5924947a4044c7b7e70127f042778246c0ce6" +content-hash = "820a4c895eee0304a7cb8572d441c377a3ecb82cb300aa5f21823c96413c0d6d" diff --git a/pyproject.toml b/pyproject.toml index bfa2bca..3973289 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ uvicorn = "^0.28.0" asyncpg = "^0.29.0" environs = "^11.0.0" alembic = "^1.13.1" +quart-cors = "^0.7.0" [tool.poetry.group.dev] optional = true