back-end communication set up, base screen implemented

This commit is contained in:
2026-01-09 17:30:28 +01:00
parent 362a83b2e3
commit e3672c4b14
6 changed files with 156 additions and 93 deletions
+4 -2
View File
@@ -7,13 +7,14 @@ from justgetit.register import Register
from justgetit.configuration import Configuration from justgetit.configuration import Configuration
from justgetit.providekey import ProvideKey from justgetit.providekey import ProvideKey
from justgetit.lists import Lists from justgetit.lists import Lists
from justgetit.backend import BackEnd
class JustEatIt(toga.App): class JustEatIt(toga.App):
login_screen = toga.Box() login_screen = toga.Box()
register_screen = toga.Box() register_screen = toga.Box()
providekey_screen = toga.Box() providekey_screen = toga.Box()
lists_screen = toga.Box() lists_screen = toga.Box()
backend = None
configuration = None configuration = None
last_screen_width = SCREENWIDTH last_screen_width = SCREENWIDTH
@@ -38,6 +39,7 @@ class JustEatIt(toga.App):
self.register_screen = Register() self.register_screen = Register()
self.providekey_screen = ProvideKey() self.providekey_screen = ProvideKey()
self.lists_screen = Lists() self.lists_screen = Lists()
self.backend = BackEnd(self)
self.main_window = toga.MainWindow(title=self.formal_name) self.main_window = toga.MainWindow(title=self.formal_name)
@@ -65,7 +67,7 @@ class JustEatIt(toga.App):
if width < SCREENWIDTH: if width < SCREENWIDTH:
# Phone-like: fill available width # Phone-like: fill available width
elf.last_screen_width = width self.last_screen_width = width
else: else:
self.last_screen_width = SCREENWIDTH self.last_screen_width = SCREENWIDTH
# set actie screen to the corre ct width # set actie screen to the corre ct width
+52
View File
@@ -0,0 +1,52 @@
from justgetit.constants import *
import httpx
import json
import toga
class BackEnd():
"""
This is the interface for the backend.
Its a bit over the top to subclass it from a widget, but this way it has access to the app
"""
application = None
def __init__(self, app):
self.application = app
async def post(self, endpoint, parameters, callback):
"""
Call the server, always with a POST, for a specific endpoint and the parameters
in the body
:param endpoint: the endpoint without /api/ (no slashes)
:param parameters: A dict with the parameters for the call
:param callback: def in the calling function to return to
"""
self.status = 0
self.payload = None
if len(endpoint)>2:
try:
async with httpx.AsyncClient() as client:
response = await client.post(f"{API}/{endpoint}", data=parameters)
self.payload = response.json()
self.status = int(self.payload['status'])
print(f" payload {self.payload}")
except httpx.RequestError as exc:
self.status = -2
# status 2 means we are in offline mode
if self.status == -2:
pass
elif self.status == 0:
await self.application.main_window.dialog(
toga.InfoDialog("Oops", self.payload['message'])
)
elif self.status == 2:
await self.application.main_window.dialog(
toga.InfoDialog("Thank you", self.payload['message'])
)
callback(parameters, self.payload)
+44 -5
View File
@@ -1,6 +1,6 @@
import toga import toga
from toga.style import Pack from toga.style import Pack
from toga.style.pack import COLUMN, ROW, END, START, CENTER, HIDDEN, JUSTIFY from toga.style.pack import COLUMN, ROW, END, START, CENTER, HIDDEN, VISIBLE, JUSTIFY
# from justgetit.constants import SCREENHEIGHT, SCREENWIDTH # from justgetit.constants import SCREENHEIGHT, SCREENWIDTH
import asyncio import asyncio
from justgetit.constants import * from justgetit.constants import *
@@ -67,6 +67,43 @@ class BasePage(toga.Box):
) )
) )
# the warning text box
self.warningtext = toga.Label("warning here",
style=Pack(margin=10,
background_color="#eee",
text_align=JUSTIFY)
)
warningline_inner = toga.Box(
children = [self.warningtext],
style=Pack(
direction=COLUMN,
justify_content=CENTER,
margin=2,
background_color="#eee"
)
)
self.warningline_outer = toga.Box(
children = [warningline_inner],
style=Pack(
direction=COLUMN,
justify_content=CENTER,
margin=24,
background_color="#ccc",
)
)
self.warning_placeholder = toga.Box(
style=Pack(
height=1,
width=1
)
)
self.warning_box = self.warning_placeholder
# add image # add image
if self.page_pngimage: if self.page_pngimage:
center_image = toga.Image(f"images/{self.page_pngimage}.png") center_image = toga.Image(f"images/{self.page_pngimage}.png")
@@ -79,6 +116,7 @@ class BasePage(toga.Box):
# wrapping up the page elements header liner and body # wrapping up the page elements header liner and body
self.add(menu_bar_outer) self.add(menu_bar_outer)
self.add(liner) self.add(liner)
self.add(self.warning_box)
self.add(self.content_box) self.add(self.content_box)
self.style=Pack( self.style=Pack(
direction=COLUMN, direction=COLUMN,
@@ -95,7 +133,8 @@ class BasePage(toga.Box):
""" """
self.content_box.style.width = w self.content_box.style.width = w
async def dialog(self, widget, **kwargs): async def display_warning(self, message):
ask_a_question = toga.InfoDialog( self.warningtext.text = message
"Help", self.warning_box = self.warningline_outer
"some text") await asyncio.sleep(5)
self.warning_box = self.warning_placeholder
+15 -25
View File
@@ -66,33 +66,23 @@ class Login(BasePage):
self.content_box.add(self.password_input) self.content_box.add(self.password_input)
self.content_box.add(save_button_box) self.content_box.add(save_button_box)
def goto_registration(self, dummy):
self.app.goto_next_page_by_name(PAGE_REGISTER)
async def login_button_pressed(self, widget): async def login_button_pressed(self, widget):
self.result = 0
self.message = ''
user = self.name_input.value user = self.name_input.value
pwd = self.password_input.value pwd = self.password_input.value
if len(user)>0 and len(pwd)>0: if len(user)>1 and len(pwd)>1:
data = {'user': user, 'password': pwd} parameters = {}
try: parameters['user'] = user
async with httpx.AsyncClient() as client: parameters['password'] = pwd
response = await client.post(API+'/login', data=data) await self.app.backend.post('login', parameters, self.after_backend_login)
payload = response.json()
print(payload) def after_backend_login(self, parameters, return_data):
self.result = int(payload['status']) if return_data['status'] > 0:
if 'message' in payload: # store login etc in config
self.message = payload['message'] self.app.configuration.store_login(parameters['user'], parameters['password'])
except httpx.RequestError as exc: # goto lists screen
self.result = -2 self.app.goto_next_page_by_name(PAGE_LISTS)
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)
+15 -31
View File
@@ -53,40 +53,11 @@ class ProvideKey(BasePage):
) )
) )
# # the help text box
# self.helptext = toga.Label(HELP_ON_KEY,
# style=Pack(margin=10,
# background_color="#eee",
# text_align=JUSTIFY)
# )
# self.helpbox_inner = toga.Box(
# children = [self.helptext],
# style=Pack(
# direction=COLUMN,
# justify_content=CENTER,
# margin=2,
# background_color="#eee"
# )
# )
# self.helpbox = toga.Box(
# children = [self.helpbox_inner],
# style=Pack(
# direction=COLUMN,
# justify_content=CENTER,
# margin=24,
# background_color="#ccc",
# visibility=HIDDEN
# )
# )
# wrapping up the windows elements # wrapping up the windows elements
self.content_box.add(self.app_key) self.content_box.add(self.app_key)
self.content_box.add(save_button_box) self.content_box.add(save_button_box)
async def save_button_pressed(self, widget): async def save_button_pressed__(self, widget):
self.result = 0 self.result = 0
print(self.app_key.value) print(self.app_key.value)
if len(self.app_key.value)>1: if len(self.app_key.value)>1:
@@ -125,4 +96,17 @@ class ProvideKey(BasePage):
if await self.app.main_window.dialog(ask_a_question): if await self.app.main_window.dialog(ask_a_question):
print("The user said yes!") print("The user said yes!")
else: else:
print("The user said no.") print("The user said no.")
async def save_button_pressed(self, widget):
if len(self.app_key.value)>1:
parameters = {}
parameters['key'] = self.app_key.value
await self.app.backend.post('validate_key', parameters, self.after_backend_validation)
def after_backend_validation(self, parameters, return_data):
if return_data['status'] > 0:
self.app.configuration.store_app_key(parameters['key'])
# goto login screen
self.app.goto_next_page_by_name(PAGE_LOGIN)
+26 -30
View File
@@ -28,7 +28,7 @@ class Register(BasePage):
# Name input # Name input
self.name_input = toga.TextInput( self.name_input = toga.TextInput(
placeholder="Could be you here", placeholder="Are you in for this?",
style=Pack( style=Pack(
width=250, width=250,
margin_bottom=10 margin_bottom=10
@@ -36,12 +36,21 @@ class Register(BasePage):
) )
# Password input # Password input
self.password_input = toga.PasswordInput( self.password_input = toga.PasswordInput(
placeholder="Feeling lucky?", placeholder="Trust me with your secret?",
style=Pack( style=Pack(
width=250, width=250,
margin_bottom=20 margin_bottom=20
) )
) )
# Password input validation
self.password_input_validation = toga.PasswordInput(
placeholder="...and again?",
style=Pack(
width=250,
margin_bottom=20
)
)
# Save button # Save button
save_button = toga.Button( save_button = toga.Button(
icon=toga.Icon("images/save"), icon=toga.Icon("images/save"),
@@ -64,43 +73,30 @@ class Register(BasePage):
# wrapping up the windows elements # wrapping up the windows elements
self.content_box.add(self.name_input) self.content_box.add(self.name_input)
self.content_box.add(self.password_input) self.content_box.add(self.password_input)
self.content_box.add(self.password_input_validation)
self.content_box.add(save_button_box) self.content_box.add(save_button_box)
def goto_login(self, dummy):
self.app.goto_next_page_by_name(PAGE_LOGIN)
async def register_button_pressed(self, widget): async def register_button_pressed(self, widget):
self.result = 0
self.message = ''
user = self.name_input.value user = self.name_input.value
pwd = self.password_input.value pwd = self.password_input.value
pwd2 = self.password_verify_input.value pwd2 = self.password_input_validation.value
if len(user)>0 and len(pwd)>0 and len(pwd2)>0: if len(user)>0 and len(pwd)>0 and len(pwd2)>0:
if pwd==pwd2: if pwd==pwd2:
data = {'user': user, 'password': pwd} parameters = {}
try: parameters['user'] = user
async with httpx.AsyncClient() as client: parameters['password'] = pwd
response = await client.post(API+'/register', data=data) await self.app.backend.post('register', parameters, self.after_backend_registration)
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_LOGIN)
else:
await self.app.main_window.dialog(
toga.InfoDialog("Registration error", self.message)
)
else: else:
await self.app.main_window.dialog( await self.app.main_window.dialog(
toga.InfoDialog("Error", "Passwords do not match") toga.InfoDialog("Error", "Passwords do not match.")
) )
def goto_login(self, dummy):
self.app.goto_next_page_by_name(PAGE_LOGIN) def after_backend_registration(self, parameters, return_data):
if int(return_data['status']) > 0:
print("registraiton ok")
# self.display_warning("Registration Ok. Awaiting Approval")
self.app.goto_next_page_by_name(PAGE_LOGIN)