fix error
Browse files- app/main.py +53 -20
app/main.py
CHANGED
|
@@ -8,9 +8,8 @@ import datetime
|
|
| 8 |
def main(page: ft.Page):
|
| 9 |
page.title = "MAKERSPACE DATABASE"
|
| 10 |
page.theme_mode = ft.ThemeMode.DARK
|
| 11 |
-
page.padding = 10
|
| 12 |
|
| 13 |
-
# --- CONFIGURACIÓN DE VARIABLES ---
|
| 14 |
TG_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 15 |
TG_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
| 16 |
GITLAB_URL = "https://gitlab.com"
|
|
@@ -25,24 +24,19 @@ def main(page: ft.Page):
|
|
| 25 |
chat_id = str(TG_CHAT_ID).strip()
|
| 26 |
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
| 27 |
payload = {"chat_id": chat_id, "text": mensaje, "parse_mode": "Markdown"}
|
| 28 |
-
|
| 29 |
for intento in range(3):
|
| 30 |
try:
|
| 31 |
response = requests.post(url, json=payload, timeout=12)
|
| 32 |
-
if response.status_code == 200:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return f"API_{response.status_code}"
|
| 36 |
-
except requests.exceptions.RequestException:
|
| 37 |
-
continue
|
| 38 |
return "ERR_RED"
|
| 39 |
|
| 40 |
def mostrar_factura(i):
|
| 41 |
dialog = ft.AlertDialog(
|
| 42 |
title=ft.Text("RECIBO DE PRÉSTAMO", weight="bold"),
|
| 43 |
content=ft.Column([
|
| 44 |
-
ft.Text(f"ESTADO: {'✅ ENVIADO' if i['status']=='OK' else '⚠️ LOCAL'}",
|
| 45 |
-
color="green" if i['status']=='OK' else "orange", weight="bold"),
|
| 46 |
ft.Divider(),
|
| 47 |
ft.Text(f"ALUMNO: {i['alumno']}", size=16),
|
| 48 |
ft.Text(f"ITEM: {i['item']}", weight="bold"),
|
|
@@ -59,8 +53,6 @@ def main(page: ft.Page):
|
|
| 59 |
|
| 60 |
def route_change(route):
|
| 61 |
page.views.clear()
|
| 62 |
-
|
| 63 |
-
# --- MENÚ PRINCIPAL ---
|
| 64 |
if page.route == "/":
|
| 65 |
page.views.append(
|
| 66 |
ft.View("/", [
|
|
@@ -72,11 +64,8 @@ def main(page: ft.Page):
|
|
| 72 |
], spacing=20, horizontal_alignment=ft.CrossAxisAlignment.CENTER)
|
| 73 |
])
|
| 74 |
)
|
| 75 |
-
|
| 76 |
-
# --- SECCIÓN PRÉSTAMOS (DISEÑO MÓVIL + HORA) ---
|
| 77 |
elif page.route == "/prestamos":
|
| 78 |
ahora_rd = (datetime.datetime.utcnow() - datetime.timedelta(hours=4)).strftime('%H:%M')
|
| 79 |
-
|
| 80 |
nombre = ft.TextField(label="Alumno", expand=True)
|
| 81 |
item = ft.TextField(label="Descripción del item", multiline=True, min_lines=2)
|
| 82 |
cant = ft.TextField(label="Cant.", value="1", expand=1)
|
|
@@ -92,8 +81,7 @@ def main(page: ft.Page):
|
|
| 92 |
subtitle=ft.Text(f"{d['item']} (x{d['cantidad']})"),
|
| 93 |
trailing=ft.Icon(ft.Icons.CHECK_CIRCLE, color="green") if d['status']=="OK" else ft.Icon(ft.Icons.REPORT_GMAILERRORRED, color="orange"),
|
| 94 |
on_click=lambda _: mostrar_factura(d)
|
| 95 |
-
),
|
| 96 |
-
bgcolor="#2d3238", border_radius=10
|
| 97 |
)
|
| 98 |
|
| 99 |
for d in reversed(historial_datos):
|
|
@@ -102,9 +90,54 @@ def main(page: ft.Page):
|
|
| 102 |
def registrar(e):
|
| 103 |
if not nombre.value or not item.value:
|
| 104 |
st_txt.value = "⚠️ Completa los datos"; st_txt.color="red"; page.update(); return
|
| 105 |
-
|
| 106 |
res = enviar_telegram(f"🛠 *PRÉSTAMO*\n👤 {nombre.value}\n📦 {item.value}\n🔢 Cant: {cant.value}\n🕒 Hora: {h_ext.value}")
|
| 107 |
nuevo = {"alumno": nombre.value, "item": item.value, "cantidad": cant.value, "hora": h_ext.value, "devolucion": h_dev.value or "--:--", "status": res}
|
| 108 |
historial_datos.append(nuevo)
|
| 109 |
lista_historial.controls.insert(0, crear_card(nuevo))
|
| 110 |
-
st_txt.value = "✅ Registrado" if res=="OK" else
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def main(page: ft.Page):
|
| 9 |
page.title = "MAKERSPACE DATABASE"
|
| 10 |
page.theme_mode = ft.ThemeMode.DARK
|
| 11 |
+
page.padding = 10
|
| 12 |
|
|
|
|
| 13 |
TG_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 14 |
TG_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
|
| 15 |
GITLAB_URL = "https://gitlab.com"
|
|
|
|
| 24 |
chat_id = str(TG_CHAT_ID).strip()
|
| 25 |
url = f"https://api.telegram.org/bot{token}/sendMessage"
|
| 26 |
payload = {"chat_id": chat_id, "text": mensaje, "parse_mode": "Markdown"}
|
|
|
|
| 27 |
for intento in range(3):
|
| 28 |
try:
|
| 29 |
response = requests.post(url, json=payload, timeout=12)
|
| 30 |
+
if response.status_code == 200: return "OK"
|
| 31 |
+
else: return f"API_{response.status_code}"
|
| 32 |
+
except: continue
|
|
|
|
|
|
|
|
|
|
| 33 |
return "ERR_RED"
|
| 34 |
|
| 35 |
def mostrar_factura(i):
|
| 36 |
dialog = ft.AlertDialog(
|
| 37 |
title=ft.Text("RECIBO DE PRÉSTAMO", weight="bold"),
|
| 38 |
content=ft.Column([
|
| 39 |
+
ft.Text(f"ESTADO: {'✅ ENVIADO' if i['status']=='OK' else '⚠️ LOCAL'}", color="green" if i['status']=='OK' else "orange", weight="bold"),
|
|
|
|
| 40 |
ft.Divider(),
|
| 41 |
ft.Text(f"ALUMNO: {i['alumno']}", size=16),
|
| 42 |
ft.Text(f"ITEM: {i['item']}", weight="bold"),
|
|
|
|
| 53 |
|
| 54 |
def route_change(route):
|
| 55 |
page.views.clear()
|
|
|
|
|
|
|
| 56 |
if page.route == "/":
|
| 57 |
page.views.append(
|
| 58 |
ft.View("/", [
|
|
|
|
| 64 |
], spacing=20, horizontal_alignment=ft.CrossAxisAlignment.CENTER)
|
| 65 |
])
|
| 66 |
)
|
|
|
|
|
|
|
| 67 |
elif page.route == "/prestamos":
|
| 68 |
ahora_rd = (datetime.datetime.utcnow() - datetime.timedelta(hours=4)).strftime('%H:%M')
|
|
|
|
| 69 |
nombre = ft.TextField(label="Alumno", expand=True)
|
| 70 |
item = ft.TextField(label="Descripción del item", multiline=True, min_lines=2)
|
| 71 |
cant = ft.TextField(label="Cant.", value="1", expand=1)
|
|
|
|
| 81 |
subtitle=ft.Text(f"{d['item']} (x{d['cantidad']})"),
|
| 82 |
trailing=ft.Icon(ft.Icons.CHECK_CIRCLE, color="green") if d['status']=="OK" else ft.Icon(ft.Icons.REPORT_GMAILERRORRED, color="orange"),
|
| 83 |
on_click=lambda _: mostrar_factura(d)
|
| 84 |
+
), bgcolor="#2d3238", border_radius=10
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
for d in reversed(historial_datos):
|
|
|
|
| 90 |
def registrar(e):
|
| 91 |
if not nombre.value or not item.value:
|
| 92 |
st_txt.value = "⚠️ Completa los datos"; st_txt.color="red"; page.update(); return
|
|
|
|
| 93 |
res = enviar_telegram(f"🛠 *PRÉSTAMO*\n👤 {nombre.value}\n📦 {item.value}\n🔢 Cant: {cant.value}\n🕒 Hora: {h_ext.value}")
|
| 94 |
nuevo = {"alumno": nombre.value, "item": item.value, "cantidad": cant.value, "hora": h_ext.value, "devolucion": h_dev.value or "--:--", "status": res}
|
| 95 |
historial_datos.append(nuevo)
|
| 96 |
lista_historial.controls.insert(0, crear_card(nuevo))
|
| 97 |
+
st_txt.value = "✅ Registrado" if res == "OK" else f"❌ Error: {res}"
|
| 98 |
+
st_txt.color = "green" if res == "OK" else "orange"
|
| 99 |
+
nombre.value = ""; item.value = ""; page.update()
|
| 100 |
+
|
| 101 |
+
page.views.append(
|
| 102 |
+
ft.View("/prestamos", [
|
| 103 |
+
ft.AppBar(title=ft.Text("Préstamos"), bgcolor="#1a1c1e"),
|
| 104 |
+
ft.Column([
|
| 105 |
+
ft.Text("REGISTRO", size=18, weight="bold"),
|
| 106 |
+
nombre, ft.Row([cant, h_ext, h_dev], spacing=10), item,
|
| 107 |
+
ft.ElevatedButton("CONFIRMAR", icon=ft.Icons.SEND, on_click=registrar, bgcolor=ft.Colors.GREEN_800, color="white", height=50, width=float("inf")),
|
| 108 |
+
st_txt, ft.Divider(), ft.Text("HISTORIAL (Click p/ detalle)", size=18, weight="bold"),
|
| 109 |
+
lista_historial, ft.TextButton("Volver", on_click=lambda _: page.go("/"))
|
| 110 |
+
], scroll=ft.ScrollMode.ADAPTIVE, expand=True, spacing=10)
|
| 111 |
+
])
|
| 112 |
+
)
|
| 113 |
+
elif page.route == "/repos":
|
| 114 |
+
repo_list = ft.ListView(expand=True, spacing=10)
|
| 115 |
+
page.views.append(ft.View("/repos", [ft.AppBar(title=ft.Text("Proyectos")), repo_list, ft.TextButton("Volver", on_click=lambda _: page.go("/"))]))
|
| 116 |
+
try:
|
| 117 |
+
gl = gitlab.Gitlab(GITLAB_URL, private_token=GIT_TOKEN)
|
| 118 |
+
for p in gl.groups.get(GIT_GROUP).projects.list(all=True):
|
| 119 |
+
repo_list.controls.append(ft.ListTile(title=ft.Text(p.name), on_click=lambda _, pid=p.id, pname=p.name: page.go(f"/ver/{pid}/{pname}")))
|
| 120 |
+
page.update()
|
| 121 |
+
except: pass
|
| 122 |
+
elif page.route.startswith("/ver/"):
|
| 123 |
+
parts = page.route.split("/")
|
| 124 |
+
pid, pname = parts[2], parts[3]
|
| 125 |
+
c_area = ft.Column(scroll=ft.ScrollMode.AUTO, expand=True)
|
| 126 |
+
page.views.append(ft.View(f"/ver/{pid}", [ft.AppBar(title=ft.Text(pname)), c_area, ft.TextButton("Volver", on_click=lambda _: page.go("/repos"))]))
|
| 127 |
+
try:
|
| 128 |
+
gl = gitlab.Gitlab(GITLAB_URL, private_token=GIT_TOKEN)
|
| 129 |
+
project = gl.projects.get(pid)
|
| 130 |
+
c_area.controls.append(ft.Row([
|
| 131 |
+
ft.ElevatedButton("ZIP", icon=ft.Icons.DOWNLOAD, on_click=lambda _: page.launch_url(f"{GITLAB_URL}/api/v4/projects/{pid}/repository/archive.zip?private_token={GIT_TOKEN}")),
|
| 132 |
+
ft.OutlinedButton("WEB", icon=ft.Icons.OPEN_IN_NEW, on_click=lambda _: page.launch_url(project.web_url))
|
| 133 |
+
], alignment=ft.MainAxisAlignment.CENTER))
|
| 134 |
+
page.update()
|
| 135 |
+
except: pass
|
| 136 |
+
page.update()
|
| 137 |
+
|
| 138 |
+
page.on_route_change = route_change
|
| 139 |
+
page.go("/")
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
port = int(os.getenv("PORT", 7860))
|
| 143 |
+
ft.app(target=main, view=ft.AppView.WEB_BROWSER, host="0.0.0.0", port=port)
|