97 lines
3.2 KiB
Python
97 lines
3.2 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 = ft.TextField(
|
|
label="Логин",
|
|
hint_text="Введите логин...",
|
|
filled=True,
|
|
bgcolor=ds.clolors.ferst,
|
|
border_color=ds.clolors.laer2,
|
|
border_radius=12,
|
|
prefix_icon=ft.Icons.PERSON,
|
|
label_style=ft.TextStyle(color=ds.clolors.laer3, weight=ft.FontWeight.BOLD),
|
|
text_style=ft.TextStyle(color=ds.clolors.laer3, size=16),
|
|
)
|
|
|
|
self._password = ft.TextField(
|
|
label="Пароль",
|
|
hint_text="Введите пароль...",
|
|
filled=True,
|
|
bgcolor=ds.clolors.ferst,
|
|
border_color=ds.clolors.laer2,
|
|
border_radius=12,
|
|
prefix_icon=ft.Icons.LOCK,
|
|
password=True,
|
|
can_reveal_password=True,
|
|
label_style=ft.TextStyle(color=ds.clolors.laer3, weight=ft.FontWeight.BOLD),
|
|
text_style=ft.TextStyle(color=ds.clolors.laer3, size=16),
|
|
on_submit=lambda e: page.run_task(self._do_login),
|
|
)
|
|
|
|
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),
|
|
)
|
|
|
|
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,
|
|
],
|
|
),
|
|
),
|
|
]
|
|
|
|
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:
|
|
# Успешный вход — переходим на главную
|
|
await self.page.push_route("/main")
|
|
|
|
def _show_error(self, message: str):
|
|
self._error.value = message
|
|
self._error.visible = True
|
|
self.update()
|