base app with app-key, registration, login, logout

This commit is contained in:
2026-01-09 13:36:21 +01:00
parent e316001fa7
commit 362a83b2e3
13 changed files with 602 additions and 160 deletions
+63 -71
View File
@@ -1,106 +1,98 @@
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, END, START, CENTER
from justgetit.constants import SCREENHEIGHT, SCREENWIDTH
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
class Login(toga.Box):
class Login(BasePage):
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'
def __init__(self, navigate_to, *args, **kwargs):
super().__init__(*args, **kwargs)
self.navigate_to= navigate_to
register_button = toga.Button(
icon=toga.Icon("images/register"),
style=Pack(
width=50,
margin=5,
),
id="register",
on_press=self.navigate_to)
liner =toga.Box(style=Pack(width=SCREENWIDTH, height = 2, background_color="#ddd"))
menu_bar= toga.Row(
children=[register_button, liner],
style=Pack(
justify_content=END,
width = SCREENWIDTH,
background_color="#eee"
)
)
# Title icon
my_image = toga.Image("images/login.png")
title = toga.ImageView(my_image, style=Pack(
width=50,
margin_bottom=20
))
# Email input
name_input = toga.TextInput(
# Name input
self.name_input = toga.TextInput(
placeholder="Could be you here",
style=Pack(
width=250,
margin_bottom=10
)
)
# Password input
password_input = toga.PasswordInput(
self.password_input = toga.PasswordInput(
placeholder="Feeling lucky?",
style=Pack(
width=250,
margin_bottom=20
)
)
# Login button
login_button = toga.Button(
# Save button
save_button = toga.Button(
icon=toga.Icon("images/save"),
style=Pack(
width=50,
margin=10,
background_color="white"
),
on_press=self.login_button_press)
on_press=self.login_button_pressed)
login_area= toga.Box(
children=[login_button],
save_button_box= toga.Column(
children=[save_button],
style=Pack(
width=250,
direction=ROW,
flex=1,
align_items=START,
margin=20
)
)
# Main content box (centered)
content_box = toga.Column(
children=[
title,
name_input,
password_input,
login_area
],
style=Pack(
align_items=CENTER,
height=SCREENHEIGHT - 50, # iPhone height
margin=20,
)
)
# Outer box to simulate iPhone screen
screen = toga.Column(
children=[menu_bar,
liner,
content_box],
style=Pack(
width=SCREENWIDTH, # iPhone width
)
)
# 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)
self.add(screen)
async def login_button_pressed(self, widget):
self.result = 0
self.message = ''
user = self.name_input.value
pwd = self.password_input.value
def login_button_press(self, widget):
print("pressesd")
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
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)