finance-watcher / scripts / insert_new_totals.py
insert_new_totals.py
Raw
import sys
from PySide6.QtWidgets import (
    QLineEdit, 
    QPushButton, 
    QApplication, 
    QLabel,
    QVBoxLayout, 
    QDialog
)
from PySide6.QtCore import Qt
from database.finance_database import FinanceDatabase
from finance_watcher_dataclasses.import_dataclasses import AccountRow
import datetime

LABEL_INDEX = 0
LINE_EDIT_INDEX = 1

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.finance_db = FinanceDatabase()
        self.id_widgets = {}
        self.description_label = QLabel(
            "Enter the new totals for each account:"
        )
        self.feedback_label = QLabel()
        self.feedback_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        # Create widgets
        accounts_totals = self.finance_db.get_all_accounts_and_latest_totals()

        for id, (name, total) in accounts_totals.items():
            label = QLabel(f'{name}: ')
            edit = QLineEdit(f"{total}")
            self.id_widgets[id] = [
                label, edit
            ]
            edit.textChanged.connect(self.reset_feedback)

        self.button = QPushButton("INSERT INTO DATABASE")
        # Create layout and add widgets
        layout = QVBoxLayout()
        self.create_description_label()
        layout.addWidget(self.description_label)
        for id, widgets in self.id_widgets.items():
            layout.addWidget(widgets[LABEL_INDEX])
            layout.addWidget(widgets[LINE_EDIT_INDEX])
        layout.addWidget(self.button)
        layout.addWidget(self.feedback_label)
        
        # Set dialog layout
        self.setLayout(layout)

        self.button.clicked.connect(
            self.get_info_and_insert_into_database
        )

    def get_info_and_insert_into_database(self):
        valid_account_rows = []
        for id, widgets in self.id_widgets.items():
            input_total = widgets[LINE_EDIT_INDEX].text()
            try:
                total = float(input_total)
                valid_account_rows.append(
                    AccountRow(
                        account_id=id,
                        date_to_amount={
                            datetime.datetime.now(): total
                        }
                    )
                )
            except:
                self.feedback_label.setText(
                    "Failed to insert: Incorrect values inserted."
                )
                return
        if self.finance_db.insert_account_totals(valid_account_rows):
            self.feedback_label.setText("Insert was successful!")
        else:
            self.feedback_label.setText(
                "Failed to insert! Check the connection of the database."
            )

    def reset_feedback(self):
        self.feedback_label.setText('')

    def create_description_label(self):
        description_font = self.font()
        description_font.setPointSize(20)
        description_font.setBold(True)
        self.description_label.setFont(description_font)

if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)
    # Create and show the form
    form = Form()
    form.show()
    # Run the main Qt loop
    sys.exit(app.exec())