diff --git a/parameciofast/modules/fastadmin/app.py b/parameciofast/modules/fastadmin/app.py index a13699b..e392190 100644 --- a/parameciofast/modules/fastadmin/app.py +++ b/parameciofast/modules/fastadmin/app.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Cookie, Request, Response +from fastapi import FastAPI, Cookie, Request, Response, Body from fastapi.responses import HTMLResponse, RedirectResponse from parameciofast.modules.fastadmin import admin_app from typing import Annotated @@ -91,6 +91,51 @@ def check_login_admin(user: UserAdmin, request: Request) -> ResponseData: return {'error': error, 'message': message} +class UserSignup(BaseModel): + username: str = Field(description="The username of new user", min_length=4, pattern=r"\w+") + email: str = Field(description="The email of new user", min_length=7 , pattern=r"\w[\w\.-]*@\w[\w\.-]+\.\w+") + password: str = Field(description="The password of new user", min_length=4) + repeat_password: str = Field(description="Repeat the password of the new user", min_length=4) + + model_config = { + "json_schema_extra": { + "examples": [ + { + "username": "johnny", + "email": "trial@example.com", + "password": "anrandompasswordthatineverused", + "repeat_password": "anrandompasswordthatineverused", + } + ] + } + } + +@admin_app.post('/signup') +def signup_insert_admin(user: Annotated[UserSignup, Body(embed=True)], request: Request) -> ResponseData: + + i18n=I18n('admin', I18n.session_lang(request.session)) + + error=1 + + message='' + + db=WebModel.connection() + + #Only can exist and user + + with db.query('select count(id) as num_users from useradmin', []) as cursor: + num_users=cursor.fetchone()['num_users'] + + if num_users: + return RedirectResponse(app.url_path_for('login_admin')) + else: + + pass + + db.close() + + return {'error': error, 'message': message} + @admin_app.get('/logout') def logout_admin(request: Request) -> RedirectResponse: diff --git a/parameciofast/modules/fastadmin/templates/signup.phtml b/parameciofast/modules/fastadmin/templates/signup.phtml index 5519f93..dee1743 100644 --- a/parameciofast/modules/fastadmin/templates/signup.phtml +++ b/parameciofast/modules/fastadmin/templates/signup.phtml @@ -5,7 +5,7 @@ ${tlang('Signup')}
-
+
@@ -39,6 +39,7 @@
<%block name="jscript"> +