diff options
| author | 2026-07-30 16:03:32 +0200 | |
|---|---|---|
| committer | 2026-07-30 16:03:32 +0200 | |
| commit | 2d22158dd95f76399f5716d556be60c77da04a46 (patch) | |
| tree | 15fca3adae0f44674fe06bc141879804659c3664 /authentication/endpoints.py | |
| parent | aa56bc05285981d57c5adb2cee3c4e898fb2a702 (diff) | |
| download | server-2d22158dd95f76399f5716d556be60c77da04a46.tar.gz server-2d22158dd95f76399f5716d556be60c77da04a46.tar.bz2 server-2d22158dd95f76399f5716d556be60c77da04a46.zip | |
Admin uiv3
Diffstat (limited to 'authentication/endpoints.py')
| -rw-r--r-- | authentication/endpoints.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/authentication/endpoints.py b/authentication/endpoints.py index 6877383..3af49fc 100644 --- a/authentication/endpoints.py +++ b/authentication/endpoints.py | |||
| @@ -6,6 +6,7 @@ from uuid import uuid7 | |||
| 6 | 6 | ||
| 7 | from fastapi import FastAPI, Request, Form, Query | 7 | from fastapi import FastAPI, Request, Form, Query |
| 8 | from pydantic import BaseModel, ValidationError, model_validator, EmailStr | 8 | from pydantic import BaseModel, ValidationError, model_validator, EmailStr |
| 9 | from sqlalchemy import func | ||
| 9 | from sqlmodel import select | 10 | from sqlmodel import select |
| 10 | from starlette.authentication import requires | 11 | from starlette.authentication import requires |
| 11 | from starlette.middleware.authentication import AuthenticationMiddleware | 12 | from starlette.middleware.authentication import AuthenticationMiddleware |
| @@ -106,10 +107,17 @@ class RegisterForm(BaseModel): | |||
| 106 | return self | 107 | return self |
| 107 | 108 | ||
| 108 | 109 | ||
| 110 | def is_first_run() -> bool: | ||
| 111 | with get_session_context() as session: | ||
| 112 | user_count = session.exec(select(func.count(User.id))) | ||
| 113 | return user_count.first() == 0 | ||
| 114 | |||
| 115 | |||
| 109 | async def base_register(request: Request, form: RegisterForm | None = None, errors: dict[str, str] | None = None): | 116 | async def base_register(request: Request, form: RegisterForm | None = None, errors: dict[str, str] | None = None): |
| 110 | return templates.TemplateResponse(request, 'register.html', context={ | 117 | return templates.TemplateResponse(request, 'register.html', context={ |
| 111 | 'form': form, | 118 | 'form': form, |
| 112 | 'errors': errors, | 119 | 'errors': errors, |
| 120 | 'is_first_run': is_first_run(), | ||
| 113 | }) | 121 | }) |
| 114 | 122 | ||
| 115 | 123 | ||
| @@ -145,7 +153,7 @@ async def post_register(request: Request, email: Annotated[str, Form()] = '', pa | |||
| 145 | return RedirectResponse(next, status_code=303) | 153 | return RedirectResponse(next, status_code=303) |
| 146 | 154 | ||
| 147 | except ValidationError as exc: | 155 | except ValidationError as exc: |
| 148 | errors.update({e['loc'][0]: e['msg'] for e in exc.errors()}) | 156 | errors.update({(e['loc'][0] if e['loc'] else 'verify_password'): e['msg'] for e in exc.errors()}) |
| 149 | 157 | ||
| 150 | return await base_register(request, form, errors) | 158 | return await base_register(request, form, errors) |
| 151 | 159 | ||
