From da87b4e2731541f3faaa48b4116d125d188dcb82 Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 26 Mar 2024 17:15:35 +0100 Subject: [PATCH] fix type of User.member --- foxnouns/app.py | 16 +++++++++++++--- foxnouns/db/user.py | 7 +++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/foxnouns/app.py b/foxnouns/app.py index 84e4a11..bbc8dde 100644 --- a/foxnouns/app.py +++ b/foxnouns/app.py @@ -5,7 +5,7 @@ from quart_schema import QuartSchema, RequestSchemaValidationError from .blueprints import members_blueprint, users_blueprint from .db.aio import async_session from .db.util import validate_token -from .exceptions import ExpectedError +from .exceptions import ExpectedError, ErrorCode app = Quart(__name__) app = cors( @@ -23,8 +23,8 @@ for bp in (users_blueprint, members_blueprint): @app.errorhandler(RequestSchemaValidationError) async def handle_request_validation_error(error: RequestSchemaValidationError): - print(error.validation_error) - return {"error": "VALIDATION"}, 400 + # TODO: parse the error and return a format closer to the draft APIv2 + return {"code": ErrorCode.BadRequest, "message": "Bad request"}, 400 @app.errorhandler(ExpectedError) @@ -32,6 +32,16 @@ async def handle_expected_error(error: ExpectedError): return {"code": error.type, "message": error.msg}, error.status_code +@app.errorhandler(404) +async def handle_404(_): + return {"code": 404, "message": "Not found"}, 404 + + +@app.errorhandler(500) +async def handle_500(_): + return {"code": 500, "message": "Internal server error"}, 500 + + @app.before_request async def get_user_from_token(): """Get the current user from a token given in the `Authorization` header or the `pronounscc-token` cookie. diff --git a/foxnouns/db/user.py b/foxnouns/db/user.py index 9c2050d..2ef8461 100644 --- a/foxnouns/db/user.py +++ b/foxnouns/db/user.py @@ -1,6 +1,6 @@ import enum from datetime import datetime -from typing import Any +from typing import Any, TYPE_CHECKING from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, Text from sqlalchemy.dialects.postgresql import ARRAY, JSONB @@ -9,6 +9,9 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship from .base import Base from .snowflake import Snowflake +if TYPE_CHECKING: + from .member import Member + class User(Base): __tablename__ = "users" @@ -30,7 +33,7 @@ class User(Base): auth_methods: Mapped[list["AuthMethod"]] = relationship( back_populates="user", cascade="all, delete-orphan" ) - members: Mapped[list["Member"]] = relationship( # noqa: F821 + members: Mapped[list["Member"]] = relationship( back_populates="user", cascade="all, delete-orphan" )