2026-01-06 22:56:13 +01:00
|
|
|
import toga
|
|
|
|
|
from toga.style import Pack
|
2026-01-09 13:36:21 +01:00
|
|
|
from toga.style.pack import COLUMN, ROW, END, START, CENTER, HIDDEN, JUSTIFY
|
|
|
|
|
# from justgetit.constants import SCREENHEIGHT, SCREENWIDTH
|
|
|
|
|
import asyncio
|
|
|
|
|
from justgetit.constants import *
|
|
|
|
|
import httpx
|
|
|
|
|
from justgetit.basepage import BasePage
|
2026-01-06 22:56:13 +01:00
|
|
|
|
|
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
class Login(BasePage):
|
2026-01-06 22:56:13 +01:00
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
This page allows the user to login
|
|
|
|
|
Login details will be kept for a week after the user last uses this app
|
|
|
|
|
|
|
|
|
|
:param self: login-screen
|
|
|
|
|
:param args: args for super
|
|
|
|
|
:param kwargs: kwars for super
|
|
|
|
|
"""
|
|
|
|
|
# set button and image
|
|
|
|
|
self.first_menu_button_icon = 'register'
|
|
|
|
|
self.first_menu_button_callback = self.goto_registration
|
|
|
|
|
self.page_pngimage = 'login'
|
2026-01-06 22:56:13 +01:00
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2026-01-06 22:56:13 +01:00
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
# Name input
|
|
|
|
|
self.name_input = toga.TextInput(
|
2026-01-06 22:56:13 +01:00
|
|
|
placeholder="Could be you here",
|
|
|
|
|
style=Pack(
|
|
|
|
|
width=250,
|
|
|
|
|
margin_bottom=10
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
# Password input
|
2026-01-09 13:36:21 +01:00
|
|
|
self.password_input = toga.PasswordInput(
|
2026-01-06 22:56:13 +01:00
|
|
|
placeholder="Feeling lucky?",
|
|
|
|
|
style=Pack(
|
|
|
|
|
width=250,
|
|
|
|
|
margin_bottom=20
|
|
|
|
|
)
|
|
|
|
|
)
|
2026-01-09 13:36:21 +01:00
|
|
|
# Save button
|
|
|
|
|
save_button = toga.Button(
|
2026-01-06 22:56:13 +01:00
|
|
|
icon=toga.Icon("images/save"),
|
|
|
|
|
style=Pack(
|
|
|
|
|
width=50,
|
|
|
|
|
margin=10,
|
|
|
|
|
),
|
2026-01-09 13:36:21 +01:00
|
|
|
on_press=self.login_button_pressed)
|
2026-01-06 22:56:13 +01:00
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
save_button_box= toga.Column(
|
|
|
|
|
children=[save_button],
|
2026-01-06 22:56:13 +01:00
|
|
|
style=Pack(
|
2026-01-09 13:36:21 +01:00
|
|
|
flex=1,
|
2026-01-06 22:56:13 +01:00
|
|
|
align_items=START,
|
|
|
|
|
margin=20
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
# wrapping up the windows elements
|
|
|
|
|
self.content_box.add(self.name_input)
|
|
|
|
|
self.content_box.add(self.password_input)
|
|
|
|
|
self.content_box.add(save_button_box)
|
|
|
|
|
|
|
|
|
|
async def login_button_pressed(self, widget):
|
|
|
|
|
self.result = 0
|
|
|
|
|
self.message = ''
|
|
|
|
|
user = self.name_input.value
|
|
|
|
|
pwd = self.password_input.value
|
2026-01-06 22:56:13 +01:00
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
if len(user)>0 and len(pwd)>0:
|
|
|
|
|
data = {'user': user, 'password': pwd}
|
|
|
|
|
try:
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
response = await client.post(API+'/login', data=data)
|
|
|
|
|
payload = response.json()
|
|
|
|
|
print(payload)
|
|
|
|
|
self.result = int(payload['status'])
|
|
|
|
|
if 'message' in payload:
|
|
|
|
|
self.message = payload['message']
|
|
|
|
|
except httpx.RequestError as exc:
|
|
|
|
|
self.result = -2
|
2026-01-06 22:56:13 +01:00
|
|
|
|
2026-01-09 13:36:21 +01:00
|
|
|
if self.result == 1:
|
|
|
|
|
# store login etc in config
|
|
|
|
|
self.app.configuration.store_login(user, pwd)
|
|
|
|
|
# goto lists screen
|
|
|
|
|
self.app.goto_next_page_by_name(PAGE_LISTS)
|
|
|
|
|
else:
|
|
|
|
|
await self.app.main_window.dialog(
|
|
|
|
|
toga.InfoDialog("Login error", self.message)
|
|
|
|
|
)
|
|
|
|
|
def goto_registration(self, dummy):
|
|
|
|
|
self.app.goto_next_page_by_name(PAGE_REGISTER)
|