Moved show error to be inherited from the parent

This commit is contained in:
SimplifiedPrivacy 2026-10-09 15:13:39 -04:00
parent e1a3b920c9
commit daa3815c38
2 changed files with 51 additions and 48 deletions

View file

@ -1,10 +1,16 @@
from gui.v2.ui.popups.error_popup import ErrorPopup from gui.v2.ui.popups.error_popup import ErrorPopup
import os import os
from typing import Any
from PyQt6.QtWidgets import QWidget, QLabel, QPushButton from PyQt6.QtWidgets import QWidget, QLabel, QPushButton
from PyQt6.QtGui import QIcon from PyQt6.QtGui import QIcon
from PyQt6 import QtCore from PyQt6 import QtCore
from enum import Enum
# For formatting errors graphically:
class DataError(Enum):
FORMAT: "format"
BLANK: "blank"
class Page(QWidget): class Page(QWidget):
@ -117,3 +123,28 @@ class Page(QWidget):
) )
error_popup.show() error_popup.show()
def display_error(self, whats_wrong: DataError, whats_it_called: str, the_thing: Any):
"""Generic re-usable error prompt for various data related issues"""
# Diagnosis
if whats_wrong == DataError.FORMAT:
error_msg = f"The {whats_it_called} is in the wrong format"
status_update = f'Error: Bad {whats_it_called} Format'
elif whats_wrong == DataError.BLANK:
error_msg = f"The {whats_it_called} is blank! But you need a {whats_it_called} to continue!"
status_update = f'Error: Blank {whats_it_called}'
else:
error_msg = f"The {whats_it_called} has unknown errors!"
status_update = f'Unknown Error with {whats_it_called}'
# Display
if the_thing and isinstance(the_thing, str):
self.show_a_popup(f"{error_msg}: {the_thing}")
elif the_thing and isinstance(the_thing, dict):
message = the_thing.get('message', "!")
self.show_a_popup(f"{error_msg} {message}")
else:
self.show_a_popup(error_msg)
self.update_status.update_status(status_update)

View file

@ -6,7 +6,7 @@ from core.models.Result import Result
# GUI # GUI
from gui.v2.actions.payment_actions import save_and_begin_timer, gui_starts_invoice, stop_payment_timer from gui.v2.actions.payment_actions import save_and_begin_timer, gui_starts_invoice, stop_payment_timer
from gui.v2.ui.pages.Page import Page from gui.v2.ui.pages.Page import Page, DataError
from gui.v2.ui.popups.qrcode_dialog import QRCodeDialog from gui.v2.ui.popups.qrcode_dialog import QRCodeDialog
from gui.v2.ui.styles.css.payment_styles import PAYMENT_STYLE_SHEET from gui.v2.ui.styles.css.payment_styles import PAYMENT_STYLE_SHEET
@ -15,27 +15,25 @@ from gui.v2.infrastructure.ThreadSafetyTool import ThreadSafetyTool
mainthread = ThreadSafetyTool() mainthread = ThreadSafetyTool()
# generic # generic
from typing import Optional
import os import os
from typing import Any
from PyQt6 import QtCore, QtWidgets from PyQt6 import QtCore, QtWidgets
from PyQt6.QtWidgets import ( from PyQt6.QtWidgets import (
QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit,
QPushButton, QWidget, QApplication, QFrame, QSpacerItem, QSizePolicy QPushButton, QWidget, QApplication, QFrame, QSpacerItem, QSizePolicy, QStackedWidget, QMainWindow
) )
from PyQt6.QtCore import Qt from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon from PyQt6.QtGui import QIcon
from enum import Enum
# For formatting errors graphically:
class DataError(Enum):
FORMAT: "format"
BLANK: "blank"
class PaymentDetailsPage(Page): class PaymentDetailsPage(Page):
"""Display Payment Details, fetch an invoice, and launch a repeat timer to check payment status.""" """
Display Payment Details, fetch an invoice, and launch a repeat timer to check payment status.
From Page: Inherits display_error, show_a_popup
From Navigator: gets the functions navigate, get_cached, page_stack object
"""
def __init__(self, page_stack, main_window=None, parent=None): def __init__(self, page_stack: QStackedWidget, main_window: Optional[QMainWindow] = None, parent: Optional[QWidget] = None):
super().__init__("Payment Details", page_stack, main_window, parent) super().__init__("Payment Details", page_stack, main_window, parent)
self.update_status = main_window self.update_status = main_window
@ -140,7 +138,7 @@ class PaymentDetailsPage(Page):
self.profile_id = int(self.update_status.current_profile_id) self.profile_id = int(self.update_status.current_profile_id)
if not self.profile_id: if not self.profile_id:
self._display_error(whats_wrong=DataError.BLANK, whats_it_called="Profile ID", the_thing=profile_id) self.display_error(whats_wrong=DataError.BLANK, whats_it_called="Profile ID", the_thing=profile_id)
return return
# this is not used here, but this is the right place to put it. Since we should have it by now: # this is not used here, but this is the right place to put it. Since we should have it by now:
@ -180,7 +178,6 @@ class PaymentDetailsPage(Page):
self.begin_invoice() self.begin_invoice()
def background_fetch_mode(self): def background_fetch_mode(self):
"""Continue checking if paid, but navigate back to the menu page""" """Continue checking if paid, but navigate back to the menu page"""
if self.polling: if self.polling:
@ -190,31 +187,6 @@ class PaymentDetailsPage(Page):
self.show_a_popup(message) self.show_a_popup(message)
def _display_error(self, whats_wrong: DataError, whats_it_called: str, the_thing: Any):
"""Generic re-usable error prompt for various data related issues"""
# Diagnosis
if whats_wrong == DataError.FORMAT:
error_msg = f"The {whats_it_called} is in the wrong format"
status_update = f'Error: Bad {whats_it_called} Format'
elif whats_wrong == DataError.BLANK:
error_msg = f"The {whats_it_called} is blank! But you need a {whats_it_called} to continue!"
status_update = f'Error: Blank {whats_it_called}'
else:
error_msg = f"The {whats_it_called} has unknown errors!"
status_update = f'Unknown Error with {whats_it_called}'
# Display
if the_thing and isinstance(the_thing, str):
self.show_a_popup(f"{error_msg}: {the_thing}")
elif the_thing and isinstance(the_thing, dict):
message = the_thing.get('message', "!")
self.show_a_popup(f"{error_msg} {message}")
else:
self.show_a_popup(error_msg)
self.update_status.update_status(status_update)
def copy_text(self, key: str): def copy_text(self, key: str):
"""Copy/Paste fields and update the bottom status""" """Copy/Paste fields and update the bottom status"""
try: try:
@ -239,11 +211,11 @@ class PaymentDetailsPage(Page):
"""Callback for the background API thread to update the UI""" """Callback for the background API thread to update the UI"""
if not invoice_result: if not invoice_result:
self._display_error(whats_wrong=DataError.BLANK, whats_it_called="Backend Invoice Result", the_thing=invoice_result) self.display_error(whats_wrong=DataError.BLANK, whats_it_called="Backend Invoice Result", the_thing=invoice_result)
return return
if not isinstance(invoice_result, Result): if not isinstance(invoice_result, Result):
self._display_error(whats_wrong=DataError.FORMAT, whats_it_called="Backend Invoice Result", the_thing=invoice_result) self.display_error(whats_wrong=DataError.FORMAT, whats_it_called="Backend Invoice Result", the_thing=invoice_result)
return return
if not invoice_result.valid: if not invoice_result.valid:
@ -255,11 +227,11 @@ class PaymentDetailsPage(Page):
invoice = invoice_result.data invoice = invoice_result.data
if not invoice: if not invoice:
self._display_error(whats_wrong=DataError.BLANK, whats_it_called="invoice", the_thing=invoice) self.display_error(whats_wrong=DataError.BLANK, whats_it_called="invoice", the_thing=invoice)
return return
if not isinstance(invoice, SubscriptionInvoice): if not isinstance(invoice, SubscriptionInvoice):
self._display_error(whats_wrong=DataError.FORMAT, whats_it_called="invoice", the_thing=invoice) self.display_error(whats_wrong=DataError.FORMAT, whats_it_called="invoice", the_thing=invoice)
return return
self.fill_in_invoice_data(invoice) self.fill_in_invoice_data(invoice)
@ -381,19 +353,19 @@ class PaymentDetailsPage(Page):
"""Get the currency from prior cached PaymentData""" """Get the currency from prior cached PaymentData"""
if not self.profile_id or not isinstance(self.profile_id, int): if not self.profile_id or not isinstance(self.profile_id, int):
self._display_error(whats_wrong=DataError.FORMAT, whats_it_called="profile_id", the_thing=self.profile_id) self.display_error(whats_wrong=DataError.FORMAT, whats_it_called="profile_id", the_thing=self.profile_id)
billing_data = PaymentData(instance_id=self.profile_id) billing_data = PaymentData(instance_id=self.profile_id)
if not billing_data: if not billing_data:
self._display_error(whats_wrong=DataError.BLANK, whats_it_called="Profile's Billing Data", the_thing=billing_data) self.display_error(whats_wrong=DataError.BLANK, whats_it_called="Profile's Billing Data", the_thing=billing_data)
currency = billing_data.currency currency = billing_data.currency
if not currency: if not currency:
self._display_error(whats_wrong=DataError.BLANK, whats_it_called="currency", the_thing=currency) self.display_error(whats_wrong=DataError.BLANK, whats_it_called="currency", the_thing=currency)
if not isinstance(currency, Currency): if not isinstance(currency, Currency):
self._display_error(whats_wrong=DataError.FORMAT, whats_it_called="currency", the_thing=currency) self.display_error(whats_wrong=DataError.FORMAT, whats_it_called="currency", the_thing=currency)
return currency return currency