rename users
This commit is contained in:
89
web/views/content_users/my_tests_page.py
Normal file
89
web/views/content_users/my_tests_page.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import asyncio
|
||||
import flet as ft
|
||||
import designer as ds
|
||||
import api_client
|
||||
|
||||
|
||||
class MyTestsPage(ft.Column):
|
||||
def __init__(self, page: ft.Page):
|
||||
self._page = page
|
||||
self._loading = ft.ProgressRing(width=40, height=40, color=ds.clolors.laer1)
|
||||
|
||||
super().__init__(
|
||||
alignment=ft.MainAxisAlignment.START,
|
||||
horizontal_alignment=ft.CrossAxisAlignment.STRETCH,
|
||||
spacing=8,
|
||||
controls=[
|
||||
ft.Row(
|
||||
alignment=ft.MainAxisAlignment.CENTER,
|
||||
controls=[self._loading],
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
async def load(self):
|
||||
user_id = self._page.session.store.get("user_id")
|
||||
if not user_id:
|
||||
self.controls = [ds.CastomText(text="Нет данных пользователя", size=14)]
|
||||
self.update()
|
||||
return
|
||||
|
||||
responses = await api_client.get_user_responses(user_id)
|
||||
if not responses:
|
||||
self.controls = [ds.CastomText(text="Вы ещё не проходили тесты", size=14)]
|
||||
self.update()
|
||||
return
|
||||
|
||||
# Подгружаем названия опросов параллельно
|
||||
poll_ids = list({r["poll_id"] for r in responses})
|
||||
polls_list = await asyncio.gather(*[api_client.get_poll(pid) for pid in poll_ids])
|
||||
polls_map = {p["id"]: p for p in polls_list if p}
|
||||
|
||||
self._render(responses, polls_map)
|
||||
self.update()
|
||||
|
||||
def _render(self, responses: list, polls_map: dict):
|
||||
url_key = self._page.session.store.get("url_key") or ""
|
||||
|
||||
# Сортируем по дате — новые вверху
|
||||
responses = sorted(responses, key=lambda r: r["submitted_at"], reverse=True)
|
||||
|
||||
cards = []
|
||||
for r in responses:
|
||||
poll = polls_map.get(r["poll_id"])
|
||||
title = poll["title"] if poll else "Неизвестный тест"
|
||||
submitted_at = r["submitted_at"][:19].replace("T", " ")
|
||||
|
||||
cards.append(
|
||||
ft.Container(
|
||||
border_radius=12,
|
||||
padding=16,
|
||||
bgcolor=ds.clolors.laer2,
|
||||
content=ft.Row(
|
||||
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
|
||||
controls=[
|
||||
ft.Column(
|
||||
spacing=4,
|
||||
controls=[
|
||||
ds.CastomText(
|
||||
text=title,
|
||||
size=15,
|
||||
weight=ft.FontWeight.BOLD,
|
||||
),
|
||||
ds.CastomText(
|
||||
text=f"Пройден: {submitted_at}",
|
||||
size=12,
|
||||
),
|
||||
],
|
||||
),
|
||||
ft.Icon(ft.Icons.CHEVRON_RIGHT, color=ds.clolors.ferst),
|
||||
],
|
||||
),
|
||||
on_click=lambda e, rid=r["id"]: self._page.go(
|
||||
f"/response/{url_key}/{rid}"
|
||||
),
|
||||
ink=True,
|
||||
)
|
||||
)
|
||||
|
||||
self.controls = cards
|
||||
Reference in New Issue
Block a user