add cors and fix cyclic import

This commit is contained in:
sam 2024-03-25 19:20:23 +01:00
parent 9d24f79436
commit a09f40fa35
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
4 changed files with 27 additions and 9 deletions

View file

@ -1,4 +1,5 @@
from quart import Quart, g, request from quart import Quart, g, request
from quart_cors import cors
from quart_schema import QuartSchema, RequestSchemaValidationError from quart_schema import QuartSchema, RequestSchemaValidationError
from .blueprints import members_blueprint, users_blueprint from .blueprints import members_blueprint, users_blueprint
@ -7,6 +8,13 @@ from .db.util import validate_token
from .exceptions import ExpectedError from .exceptions import ExpectedError
app = Quart(__name__) app = Quart(__name__)
app = cors(
app,
allow_origin="*",
allow_methods="*",
allow_headers=["Content-Type", "Authorization", "User-Agent"],
max_age=86400,
)
QuartSchema(app) QuartSchema(app)
for bp in (users_blueprint, members_blueprint): for bp in (users_blueprint, members_blueprint):
@ -39,6 +47,5 @@ async def get_user_from_token():
token, user = await validate_token(session, token) token, user = await validate_token(session, token)
g.token = token g.token = token
g.user = user g.user = user
except Exception as e: except:
print(e)
raise raise

View file

@ -30,7 +30,7 @@ class User(Base):
auth_methods: Mapped[list["AuthMethod"]] = relationship( auth_methods: Mapped[list["AuthMethod"]] = relationship(
back_populates="user", cascade="all, delete-orphan" 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" 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})" 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): class Token(Base):
__tablename__ = "tokens" __tablename__ = "tokens"

17
poetry.lock generated
View file

@ -966,6 +966,21 @@ werkzeug = ">=3.0.0"
docs = ["pydata_sphinx_theme"] docs = ["pydata_sphinx_theme"]
dotenv = ["python-dotenv"] 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]] [[package]]
name = "quart-schema" name = "quart-schema"
version = "0.19.1" version = "0.19.1"
@ -1240,4 +1255,4 @@ h11 = ">=0.9.0,<1"
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "a04cf866b1b5efd1f9484114b0e5924947a4044c7b7e70127f042778246c0ce6" content-hash = "820a4c895eee0304a7cb8572d441c377a3ecb82cb300aa5f21823c96413c0d6d"

View file

@ -22,6 +22,7 @@ uvicorn = "^0.28.0"
asyncpg = "^0.29.0" asyncpg = "^0.29.0"
environs = "^11.0.0" environs = "^11.0.0"
alembic = "^1.13.1" alembic = "^1.13.1"
quart-cors = "^0.7.0"
[tool.poetry.group.dev] [tool.poetry.group.dev]
optional = true optional = true