async def password( self, request: Request, old_password: str = Form(...), new_password: str = Form(...), re_new_password: str = Form(...), admin: AbstractAdmin = Depends(get_current_admin), resources=Depends(get_resources), ): error = None if not check_password(old_password, admin.password): error = _("old_password_error") elif new_password != re_new_password: error = _("new_password_different") if error: return templates.TemplateResponse( "password.html", context={ "request": request, "resources": resources, "error": error }, ) await self.update_password(admin, new_password) return await self.logout(request)
async def get_actions(self, request: Request) -> List[Action]: return [ Action(label=_("update"), icon="ti ti-edit", name="update", ajax=False), Action(label=_("delete"), icon="ti ti-trash", name="delete", method="DELETE"), ]
async def login(self, request: Request, redis: Redis = Depends(get_redis)): form = await request.form() username = form.get("username") password = form.get("password") remember_me = form.get("remember_me") admin = await self.admin_model.get_or_none(username=username) if not admin or not check_password(password, admin.password): return templates.TemplateResponse( self.template, status_code=HTTP_401_UNAUTHORIZED, context={ "request": request, "error": _("login_failed") }, ) response = RedirectResponse(url=request.app.admin_path, status_code=HTTP_303_SEE_OTHER) if remember_me == "on": expire = 3600 * 24 * 30 response.set_cookie("remember_me", "on") else: expire = 3600 response.delete_cookie("remember_me") token = uuid.uuid4().hex response.set_cookie( self.access_token, token, expires=expire, path=request.app.admin_path, httponly=True, ) await redis.set(constants.LOGIN_USER.format(token=token), admin.pk, expire=expire) return response
async def get_bulk_actions(self, request: Request) -> List[Action]: return [ Action( label=_("delete_selected"), icon="ti ti-trash", name="delete", method="DELETE", ), ]
async def get_toolbar_actions(self, request: Request) -> List[ToolbarAction]: return [ ToolbarAction( label=_("create"), icon="fas fa-plus", name="create", method=Method.GET, ajax=False, class_="btn-dark", ) ]
async def init( self, request: Request, ): exists = await self.admin_model.all().limit(1).exists() if exists: return self.redirect_login(request) form = await request.form() password = form.get("password") confirm_password = form.get("confirm_password") username = form.get("username") if password != confirm_password: return templates.TemplateResponse( "init.html", context={ "request": request, "error": _("confirm_password_different") }, ) await self.create_user(username, password) return self.redirect_login(request)