| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | import os | |
| 2 | + from contextlib import asynccontextmanager | ||
| 2 | 3 | ||
| 3 | 4 | import redis.asyncio as redis | |
| 4 | 5 | import uvicorn | |
@@ -27,8 +28,30 @@ | |||
| 27 | 28 | ) | |
| 28 | 29 | ||
| 29 | 30 | ||
| 31 | + @asynccontextmanager | ||
| 32 | + async def lifespan(app: FastAPI): | ||
| 33 | + r = redis.from_url( | ||
| 34 | + settings.REDIS_URL, | ||
| 35 | + decode_responses=True, | ||
| 36 | + encoding="utf8", | ||
| 37 | + ) | ||
| 38 | + await admin_app.configure( | ||
| 39 | + logo_url="https://preview.tabler.io/static/logo-white.svg", | ||
| 40 | + template_folders=[os.path.join(BASE_DIR, "templates")], | ||
| 41 | + favicon_url="https://raw.githubusercontent.com/fastapi-admin/fastapi-admin/dev/images/favicon.png", | ||
| 42 | + providers=[ | ||
| 43 | + LoginProvider( | ||
| 44 | + login_logo_url="https://preview.tabler.io/static/logo.svg", | ||
| 45 | + admin_model=Admin, | ||
| 46 | + ) | ||
| 47 | + ], | ||
| 48 | + redis=r, | ||
| 49 | + ) | ||
| 50 | + yield | ||
| 51 | + | ||
| 52 | + | ||
| 30 | 53 | def create_app(): | |
| 31 | - app = FastAPI() | ||
| 54 | + app = FastAPI(lifespan=lifespan) | ||
| 32 | 55 | app.mount( | |
| 33 | 56 | "/static", | |
| 34 | 57 | StaticFiles(directory=os.path.join(BASE_DIR, "static")), | |
@@ -44,26 +67,6 @@ async def index(): | |||
| 44 | 67 | admin_app.add_exception_handler(HTTP_403_FORBIDDEN, forbidden_error_exception) | |
| 45 | 68 | admin_app.add_exception_handler(HTTP_401_UNAUTHORIZED, unauthorized_error_exception) | |
| 46 | 69 | ||
| 47 | - @app.on_event("startup") | ||
| 48 | - async def startup(): | ||
| 49 | - r = redis.from_url( | ||
| 50 | - settings.REDIS_URL, | ||
| 51 | - decode_responses=True, | ||
| 52 | - encoding="utf8", | ||
| 53 | - ) | ||
| 54 | - await admin_app.configure( | ||
| 55 | - logo_url="https://preview.tabler.io/static/logo-white.svg", | ||
| 56 | - template_folders=[os.path.join(BASE_DIR, "templates")], | ||
| 57 | - favicon_url="https://raw.githubusercontent.com/fastapi-admin/fastapi-admin/dev/images/favicon.png", | ||
| 58 | - providers=[ | ||
| 59 | - LoginProvider( | ||
| 60 | - login_logo_url="https://preview.tabler.io/static/logo.svg", | ||
| 61 | - admin_model=Admin, | ||
| 62 | - ) | ||
| 63 | - ], | ||
| 64 | - redis=r, | ||
| 65 | - ) | ||
| 66 | - | ||
| 67 | 70 | app.mount("/admin", admin_app) | |
| 68 | 71 | app.add_middleware( | |
| 69 | 72 | CORSMiddleware, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,7 +119,7 @@ async def authenticate( | |||
| 119 | 119 | request: Request, | |
| 120 | 120 | call_next: RequestResponseEndpoint, | |
| 121 | 121 | ): | |
| 122 | - redis = request.app.redis # type:Redis | ||
| 122 | + redis = request.app.redis # type:ignore | ||
| 123 | 123 | token = request.cookies.get(self.access_token) | |
| 124 | 124 | path = request.scope["path"] | |
| 125 | 125 | admin = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,7 @@ class Input(Widget): | |||
| 15 | 15 | template = "widgets/inputs/input.html" | |
| 16 | 16 | ||
| 17 | 17 | def __init__( | |
| 18 | - self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context | ||
| 18 | + self, help_text: Optional[str] = None, default: Any = None, null: bool = False, **context | ||
| 19 | 19 | ): | |
| 20 | 20 | super().__init__(null=null, help_text=help_text, **context) | |
| 21 | 21 | self.default = default | |
@@ -44,12 +44,12 @@ class Text(Input): | |||
| 44 | 44 | input_type: Optional[str] = "text" | |
| 45 | 45 | ||
| 46 | 46 | def __init__( | |
| 47 | - self, | ||
| 48 | - help_text: Optional[str] = None, | ||
| 49 | - default: Any = None, | ||
| 50 | - null: bool = False, | ||
| 51 | - placeholder: str = "", | ||
| 52 | - disabled: bool = False, | ||
| 47 | + self, | ||
| 48 | + help_text: Optional[str] = None, | ||
| 49 | + default: Any = None, | ||
| 50 | + null: bool = False, | ||
| 51 | + placeholder: str = "", | ||
| 52 | + disabled: bool = False, | ||
| 53 | 53 | ): | |
| 54 | 54 | super().__init__( | |
| 55 | 55 | null=null, | |
@@ -65,11 +65,11 @@ class Select(Input): | |||
| 65 | 65 | template = "widgets/inputs/select.html" | |
| 66 | 66 | ||
| 67 | 67 | def __init__( | |
| 68 | - self, | ||
| 69 | - help_text: Optional[str] = None, | ||
| 70 | - default: Any = None, | ||
| 71 | - null: bool = False, | ||
| 72 | - disabled: bool = False, | ||
| 68 | + self, | ||
| 69 | + help_text: Optional[str] = None, | ||
| 70 | + default: Any = None, | ||
| 71 | + null: bool = False, | ||
| 72 | + disabled: bool = False, | ||
| 73 | 73 | ): | |
| 74 | 74 | super().__init__(help_text=help_text, null=null, default=default, disabled=disabled) | |
| 75 | 75 | ||
@@ -91,12 +91,12 @@ async def render(self, request: Request, value: Any): | |||
| 91 | 91 | ||
| 92 | 92 | class ForeignKey(Select): | |
| 93 | 93 | def __init__( | |
| 94 | - self, | ||
| 95 | - model: Type[Model], | ||
| 96 | - default: Any = None, | ||
| 97 | - null: bool = False, | ||
| 98 | - disabled: bool = False, | ||
| 99 | - help_text: Optional[str] = None, | ||
| 94 | + self, | ||
| 95 | + model: Type[Model], | ||
| 96 | + default: Any = None, | ||
| 97 | + null: bool = False, | ||
| 98 | + disabled: bool = False, | ||
| 99 | + help_text: Optional[str] = None, | ||
| 100 | 100 | ): | |
| 101 | 101 | super().__init__(help_text=help_text, default=default, null=null, disabled=disabled) | |
| 102 | 102 | self.model = model | |
@@ -116,10 +116,10 @@ class ManyToMany(Select): | |||
| 116 | 116 | template = "widgets/inputs/many_to_many.html" | |
| 117 | 117 | ||
| 118 | 118 | def __init__( | |
| 119 | - self, | ||
| 120 | - model: Type[Model], | ||
| 121 | - disabled: bool = False, | ||
| 122 | - help_text: Optional[str] = None, | ||
| 119 | + self, | ||
| 120 | + model: Type[Model], | ||
| 121 | + disabled: bool = False, | ||
| 122 | + help_text: Optional[str] = None, | ||
| 123 | 123 | ): | |
| 124 | 124 | super().__init__(help_text=help_text, disabled=disabled) | |
| 125 | 125 | self.model = model | |
@@ -144,13 +144,13 @@ async def render(self, request: Request, value: Any): | |||
| 144 | 144 | ||
| 145 | 145 | class Enum(Select): | |
| 146 | 146 | def __init__( | |
| 147 | - self, | ||
| 148 | - enum: Type[EnumCLS], | ||
| 149 | - default: Any = None, | ||
| 150 | - enum_type: Type = int, | ||
| 151 | - null: bool = False, | ||
| 152 | - disabled: bool = False, | ||
| 153 | - help_text: Optional[str] = None, | ||
| 147 | + self, | ||
| 148 | + enum: Type[EnumCLS], | ||
| 149 | + default: Any = None, | ||
| 150 | + enum_type: Type = int, | ||
| 151 | + null: bool = False, | ||
| 152 | + disabled: bool = False, | ||
| 153 | + help_text: Optional[str] = None, | ||
| 154 | 154 | ): | |
| 155 | 155 | super().__init__(help_text=help_text, default=default, null=null, disabled=disabled) | |
| 156 | 156 | self.enum = enum | |
@@ -174,10 +174,10 @@ class Json(Input): | |||
| 174 | 174 | template = "widgets/inputs/json.html" | |
| 175 | 175 | ||
| 176 | 176 | def __init__( | |
| 177 | - self, | ||
| 178 | - help_text: Optional[str] = None, | ||
| 179 | - null: bool = False, | ||
| 180 | - options: Optional[dict] = None, | ||
| 177 | + self, | ||
| 178 | + help_text: Optional[str] = None, | ||
| 179 | + null: bool = False, | ||
| 180 | + options: Optional[dict] = None, | ||
| 181 | 181 | ): | |
| 182 | 182 | """ | |
| 183 | 183 | options config to jsoneditor, see https://github.com/josdejong/jsoneditor | |
@@ -215,12 +215,12 @@ class File(Input): | |||
| 215 | 215 | input_type = "file" | |
| 216 | 216 | ||
| 217 | 217 | def __init__( | |
| 218 | - self, | ||
| 219 | - upload: FileUpload, | ||
| 220 | - default: Any = None, | ||
| 221 | - null: bool = False, | ||
| 222 | - disabled: bool = False, | ||
| 223 | - help_text: Optional[str] = None, | ||
| 218 | + self, | ||
| 219 | + upload: FileUpload, | ||
| 220 | + default: Any = None, | ||
| 221 | + null: bool = False, | ||
| 222 | + disabled: bool = False, | ||
| 223 | + help_text: Optional[str] = None, | ||
| 224 | 224 | ): | |
| 225 | 225 | super().__init__( | |
| 226 | 226 | null=null, | |
@@ -246,11 +246,11 @@ class Radio(Select): | |||
| 246 | 246 | template = "widgets/inputs/radio.html" | |
| 247 | 247 | ||
| 248 | 248 | def __init__( | |
| 249 | - self, | ||
| 250 | - options: List[Tuple[str, Any]], | ||
| 251 | - help_text: Optional[str] = None, | ||
| 252 | - default: Any = None, | ||
| 253 | - disabled: bool = False, | ||
| 249 | + self, | ||
| 250 | + options: List[Tuple[str, Any]], | ||
| 251 | + help_text: Optional[str] = None, | ||
| 252 | + default: Any = None, | ||
| 253 | + disabled: bool = False, | ||
| 254 | 254 | ): | |
| 255 | 255 | super().__init__(default=default, disabled=disabled, help_text=help_text) | |
| 256 | 256 | self.options = options | |
| Back | FazBrowse Home | New Git URL |
0 commit comments