Skip to content

Commit 3a048c5

Browse files
move off main thread, improve logging
1 parent bf2df37 commit 3a048c5

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

src/controllers/account_controller.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
2+
from threading import Thread
23

34
from PyQt6.QtCore import QTimer
4-
from PyQt6.QtWidgets import QApplication
55

66
from controllers.api_controller import ApiController
77

@@ -12,16 +12,19 @@ def __init__(self, ctx):
1212

1313
def go_to_review_from_barcode(self, barcode):
1414
self.ctx.nav.show_status("Looking up student...")
15-
QApplication.processEvents()
15+
logging.info(f"Looking up student by barcode: {barcode}")
16+
Thread(target=self._lookup_barcode_worker, args=(barcode,), daemon=True).start()
1617

18+
def _lookup_barcode_worker(self, barcode):
1719
student = ApiController.lookup_by_barcode(barcode)
18-
self.ctx.nav.hide_status()
20+
self.ctx.dispatcher.call.emit(lambda s=student: self._on_barcode_result(s))
1921

22+
def _on_barcode_result(self, student):
23+
self.ctx.nav.hide_status()
2024
if student is None:
2125
self.ctx.nav.show_status("Student not found. Please enter your details manually.")
2226
QTimer.singleShot(3000, self.ctx.nav.hide_status)
2327
return
24-
2528
self.ctx.nav.go_to_create_account_review(
2629
pid=student["pid"],
2730
first_name=student["first_name"],
@@ -31,16 +34,19 @@ def go_to_review_from_barcode(self, barcode):
3134

3235
def go_to_review_from_pid(self, pid):
3336
self.ctx.nav.show_status("Looking up student...")
34-
QApplication.processEvents()
37+
logging.info(f"Looking up student by PID: {pid}")
38+
Thread(target=self._lookup_pid_worker, args=(pid,), daemon=True).start()
3539

40+
def _lookup_pid_worker(self, pid):
3641
student = ApiController.lookup_by_pid(pid)
37-
self.ctx.nav.hide_status()
42+
self.ctx.dispatcher.call.emit(lambda s=student: self._on_pid_result(s, pid))
3843

44+
def _on_pid_result(self, student, pid):
45+
self.ctx.nav.hide_status()
3946
if student is None:
4047
self.ctx.nav.show_status("Student not found. Please check your PID.")
4148
QTimer.singleShot(3000, self.ctx.nav.hide_status)
4249
return
43-
4450
self.ctx.nav.go_to_create_account_review(
4551
pid=pid,
4652
first_name=student["first_name"],
@@ -56,8 +62,14 @@ def create_account_from_review(self, *, first_name, last_name, email, pid):
5662

5763
def _create(self, *, barcode=None, pid=None, first_name=None, last_name=None, email=None):
5864
self.ctx.nav.show_status("Account creation in progress!")
59-
QApplication.processEvents()
60-
65+
logging.info(f"Creating account: pid={pid} barcode={barcode}")
66+
Thread(
67+
target=self._create_worker,
68+
kwargs=dict(barcode=barcode, pid=pid, first_name=first_name, last_name=last_name, email=email),
69+
daemon=True,
70+
).start()
71+
72+
def _create_worker(self, *, barcode, pid, first_name, last_name, email):
6173
result = ApiController.create_account(
6274
self.ctx.rfid,
6375
barcode=barcode,
@@ -66,12 +78,13 @@ def _create(self, *, barcode=None, pid=None, first_name=None, last_name=None, em
6678
last_name=last_name,
6779
email=email,
6880
)
69-
self.ctx.nav.hide_status()
81+
self.ctx.dispatcher.call.emit(lambda r=result: self._on_create_result(r))
7082

83+
def _on_create_result(self, result):
84+
self.ctx.nav.hide_status()
7185
if result is None:
7286
self.ctx.nav.show_status("ERROR! Could not create account, please try manually.")
7387
QTimer.singleShot(3000, self.ctx.nav.hide_status)
7488
return
75-
7689
logging.info("Account creation succeeded")
7790
self.ctx.nav.pop()

0 commit comments

Comments
 (0)