Files
api-copp/web/views/login_view.py
2026-03-31 18:10:12 +05:00

92 lines
3.0 KiB
Python

import flet as ft
import designer as ds
from info import AuthService
class LoginView(ft.View):
def __init__(self, page: ft.Page):
self._auth = AuthService(page)
self._username = ds.CastomTextField_input(
label="Логин",
hint_text="Введите логин...",
prefix_icon=ft.Icons.PERSON,
)
self._password = ds.CastomTextField_input(
label="Пароль",
hint_text="Введите пароль...",
prefix_icon=ft.Icons.LOCK,
password=True,
can_reveal_password=True,
)
self._error = ft.Text("", color=ft.Colors.RED_400, size=14, visible=False)
self._btn = ds.CastomButton(
text="Войти",
on_click=lambda e: page.run_task(self._do_login),
)
self._reg_link = ft.TextButton(
content="Нет аккаунта? Зарегистрироваться",
style=ft.ButtonStyle(color=ds.clolors.ferst),
on_click=lambda e: page.run_task(page.push_route, "/reg"),
)
super().__init__(
route="/login",
bgcolor=ds.clolors.background,
horizontal_alignment=ft.CrossAxisAlignment.CENTER,
vertical_alignment=ft.MainAxisAlignment.CENTER,
)
self.controls = [
ft.Text("COPP", color=ds.clolors.ferst, size=30, weight=ft.FontWeight.BOLD),
ft.Container(height=8),
ft.Text("Вход в систему", color=ds.clolors.ferst, size=16),
ft.Container(height=24),
ft.Container(
width=360,
padding=ft.padding.all(24),
bgcolor=ds.clolors.laer3,
border_radius=16,
content=ft.Column(
spacing=16,
controls=[
self._username,
self._password,
self._error,
self._btn,
self._reg_link,
],
),
),
]
async def _do_login(self):
username = self._username.value.strip()
password = self._password.value
if not username or not password:
self._show_error("Заполните логин и пароль")
return
self._btn.disabled = True
self._error.visible = False
self.update()
error = await self._auth.login(username, password)
if error:
self._btn.disabled = False
self._show_error(error)
else:
# Успешный вход — переходим на страницу пользователя
url_key = self.page.session.store.get("url_key")
await self.page.push_route(f"/user/{url_key}")
def _show_error(self, message: str):
self._error.value = message
self._error.visible = True
self.update()