-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaccount_controller.py
More file actions
66 lines (59 loc) · 2.89 KB
/
Copy pathaccount_controller.py
File metadata and controls
66 lines (59 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
import logging
from typing import Literal
from pyqttoast import ToastPreset
from controllers.api_controller import ExternalApiError
from misc.api_models import CreateAccountResponse, Student, StudentResponse
from misc.global_context import context
class AccountController:
def __init__(self) -> None:
logging.info("account controller initialized")
def lookup(self, by: Literal["pid", "barcode"], value: str) -> Student | None:
context().main_window.show_toast_async("Looking Up Student", "", ToastPreset.INFORMATION)
logging.info(f"looking up student by {by}: {value}")
try:
response = context().api_controller.request("GET", f"/accounts/{by}/{value}")
if response.status_code == 404:
context().main_window.show_toast_async("Student Not Found", "Please enter your details manually", ToastPreset.ERROR)
return None
response.raise_for_status()
return StudentResponse.model_validate(response.json()).student
except ExternalApiError as e:
context().main_window.show_toast_async(f"System Error: {e.api}", "Please talk to a staff member", ToastPreset.ERROR)
return None
except Exception as e:
logging.error(f"error looking up student by {by}: {e}")
context().main_window.show_toast_async("Student Not Found", "Please enter your details manually", ToastPreset.ERROR)
return None
def create_account(
self,
*,
barcode: str | None = None,
pid: str | None = None,
first_name: str | None = None,
last_name: str | None = None,
email: str | None = None,
) -> bool:
context().main_window.show_toast_async("Account creation in progress!", "", ToastPreset.INFORMATION)
logging.info(f"creating account: pid={pid} barcode={barcode}")
payload = {k: v for k, v in {
"rfid": context().session.rfid,
"barcode": barcode,
"pid": pid,
"first_name": first_name,
"last_name": last_name,
"email": email,
}.items() if v is not None}
try:
response = context().api_controller.request("POST", "/accounts", json=payload, timeout=8)
response.raise_for_status()
CreateAccountResponse.model_validate(response.json())
logging.info("account creation succeeded")
return True
except ExternalApiError as e:
context().main_window.show_toast_async(f"System Error: {e.api}", "Please talk to a staff member", ToastPreset.ERROR)
return False
except Exception as e:
logging.error(f"error creating account: {e}")
context().main_window.show_toast_async("Account Creation Failed", "Please try again or talk to a staff member", ToastPreset.ERROR)
return False