hydraveil-gui/gui/v2/ui/pages/duration_selection_page.py

120 lines
4.2 KiB
Python
Executable file

# Core
from core.services.payment_phase.payment_state import PaymentData
from gui.v2.infrastructure.take_plumbing_action import take_action
from core.controllers.profile_state.data_filters import get_plans_for_profile
from core.services.subscriptions.subscriptions_by_protocol import get_current_billing_data
from core.models.enum_models import PaymentCategory
from core.models.Result import Result
# GUI
# from gui.v2.actions.payment_actions import new_individual_billing
from gui.v2.ui.styles.payment_styles import DURATION_SELECTION_STYLE, NEXT_BUTTON_STYLE
from gui.v2.ui.pages.Page import Page
from gui.v2.infrastructure.ThreadSafetyTool import ThreadSafetyTool
mainthread = ThreadSafetyTool()
from PyQt6.QtWidgets import QComboBox, QLabel, QPushButton
from PyQt6 import QtCore
class DurationSelectionPage(Page):
def __init__(self, page_stack, main_window=None, parent=None):
super().__init__("Select Duration", page_stack, main_window, parent)
self.update_status = main_window
self.create_intitial_elements()
self.button_reverse.setVisible(True)
def create_intitial_elements(self):
self.title = QLabel("Select Duration", self)
self.title.setGeometry(QtCore.QRect(530, 30, 210, 40))
self.title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.button_reverse.clicked.connect(self.reverse)
self.combo_box = QComboBox(self)
self.combo_box.setGeometry(200, 200, 400, 60)
self.combo_box.setEditable(False)
self.combo_box.setMaxVisibleItems(4)
self.combo_box.setDuplicatesEnabled(True)
self.combo_box.setPlaceholderText("Select options")
self.combo_box.setStyleSheet(DURATION_SELECTION_STYLE)
self.next_button = QPushButton("Next", self)
self.next_button.setGeometry(350, 300, 100, 40)
self.next_button.setStyleSheet(NEXT_BUTTON_STYLE)
self.next_button.clicked.connect(self.show_next)
# Background thread launch:
self.get_data()
def get_data(self):
"""Launches background thread to get data"""
# get profile id:
profile_id = int(self.update_status.current_profile_id)
# Thread SQL in background:
take_action(
action=get_plans_for_profile,
ui_caller=self.populate_with_data,
profile_id=profile_id,
)
@mainthread
def populate_with_data(self, result: Result):
if not result.valid:
self.show_a_popup(result.message)
return
self.choice_to_id = {}
self.dict_of_choices = result.data
if not self.dict_of_choices:
self.show_a_popup("There is no plan data. Go sync or remove the database and try again.")
return
if not isinstance(self.dict_of_choices, dict):
self.show_a_popup("Plan data is in an invalid format.")
return
for plan_string, plan_id in self.dict_of_choices.items():
self.combo_box.addItem(str(plan_string))
def show_next(self):
# get current selection:
current_choice = self.combo_box.currentText()
# which plan id is that:
plan_id = self.dict_of_choices[current_choice]
# save to singleton across pages:
data_result = get_current_billing_data(category=PaymentCategory.INDIVIDUAL)
if not data_result.valid:
message = data_result.message
if not message:
message = "Error fetching data"
self.show_a_popup(str(message))
billing_data = data_result.data
if billing_data:
print(f"we have billing data: {billing_data}")
print(f"saving plan_id to the billing data")
billing_data.plan_id = plan_id
else:
message = "No Billing Data"
self.show_a_popup(message)
# go to next page:
print("going to the next page")
self.custom_window.navigator.navigate("currency_selection")
def reverse(self):
self.custom_window.navigator.navigate("id")
# currency_page.selected_duration
# self.combo_box.addItems(
# ["1 month (€1.50)", "3 months (€4.30)", "6 months (€8)", "12 months (€16)"])