update code to allow both txt and csv generation
This commit is contained in:
106
src/ui/reportUi.py
Normal file
106
src/ui/reportUi.py
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
from PyQt6 import QtCore, QtWidgets, QtGui
|
||||||
|
from .sources.Ui_dialog_generateReport import Ui_Dialog
|
||||||
|
from src.utils import Icon
|
||||||
|
from src.utils.reportThread import ReportThread
|
||||||
|
from src.logic import Database
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class ReportUi(QtWidgets.QDialog, Ui_Dialog):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super(ReportUi, self).__init__(parent)
|
||||||
|
self.setupUi(self)
|
||||||
|
self.setWindowIcon(Icon("report").icon)
|
||||||
|
self.setWindowTitle("Report erstellen")
|
||||||
|
self.radioButton.hide()
|
||||||
|
self.db = Database()
|
||||||
|
|
||||||
|
self.reportprogress.hide()
|
||||||
|
|
||||||
|
# variables
|
||||||
|
self.maxrecords = 0
|
||||||
|
self.days = 0
|
||||||
|
self.rthread = ReportThread()
|
||||||
|
|
||||||
|
# buttons
|
||||||
|
self.generateReport.setEnabled(False)
|
||||||
|
self.generateReport.clicked.connect(self.generate_report)
|
||||||
|
self.radio_year.clicked.connect(self.set_days_by_radio)
|
||||||
|
self.radio_month.clicked.connect(self.set_days_by_radio)
|
||||||
|
self.radio_week.clicked.connect(self.set_days_by_radio)
|
||||||
|
self.format_txt.clicked.connect(lambda: self.rthread.setFormat("txt"))
|
||||||
|
self.format_csv.clicked.connect(lambda: self.rthread.setFormat("csv"))
|
||||||
|
self.format_csv.clicked.connect(lambda: self.generateReport.setEnabled(True))
|
||||||
|
self.format_txt.clicked.connect(lambda: self.generateReport.setEnabled(True))
|
||||||
|
# sliders
|
||||||
|
self.dayslider.valueChanged.connect(self.set_days)
|
||||||
|
self.show()
|
||||||
|
|
||||||
|
# labels
|
||||||
|
self.label_4.hide()
|
||||||
|
|
||||||
|
def set_days_by_radio(self):
|
||||||
|
if self.radio_year.isChecked():
|
||||||
|
self.set_days(365)
|
||||||
|
self.dayslider.setValue(365)
|
||||||
|
elif self.radio_month.isChecked():
|
||||||
|
self.set_days(30)
|
||||||
|
self.dayslider.setValue(30)
|
||||||
|
elif self.radio_week.isChecked():
|
||||||
|
self.set_days(7)
|
||||||
|
self.dayslider.setValue(7)
|
||||||
|
|
||||||
|
def set_days(self, value):
|
||||||
|
# if value is not 7,30,365, deactivate radio buttons
|
||||||
|
if value != 7 and value != 30 and value != 365:
|
||||||
|
self.radioButton.setChecked(True)
|
||||||
|
|
||||||
|
self.days = value
|
||||||
|
self.dayValue.setText(str(value))
|
||||||
|
|
||||||
|
def generate_report(self):
|
||||||
|
print(self.days)
|
||||||
|
self.rthread.setDays(self.days)
|
||||||
|
self.rthread.report_signal.connect(self.report_generated)
|
||||||
|
self.rthread.report_nums_signal.connect(self.show_progress)
|
||||||
|
self.rthread.report_progress_signal.connect(self.update_progress)
|
||||||
|
self.rthread.finished.connect(self.reset)
|
||||||
|
# self.rthread.finished.connect(self.rthread.deleteLater)
|
||||||
|
self.rthread.start()
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.days = 0
|
||||||
|
self.reportprogress.hide()
|
||||||
|
self.reportprogress.setValue(0)
|
||||||
|
self.label_4.setText("Fortschritt:")
|
||||||
|
self.label_4.hide()
|
||||||
|
|
||||||
|
def update_progress(self, num):
|
||||||
|
self.reportlink.clear()
|
||||||
|
self.reportprogress.setValue(num)
|
||||||
|
self.label_4.setText("Fortschritt: " + str(num) + "/" + str(self.maxrecords))
|
||||||
|
if num == self.reportprogress.maximum():
|
||||||
|
self.label_4.setText("Datei wird generiert")
|
||||||
|
|
||||||
|
def show_progress(self, num):
|
||||||
|
self.reportprogress.show()
|
||||||
|
self.label_4.show()
|
||||||
|
self.maxrecords = num
|
||||||
|
self.reportprogress.setMaximum(self.maxrecords)
|
||||||
|
|
||||||
|
def report_generated(self):
|
||||||
|
self.reportlink.setOpenExternalLinks(True)
|
||||||
|
fileformat = self.rthread.format
|
||||||
|
print(fileformat)
|
||||||
|
self.reportlink.setText(
|
||||||
|
f'<a href="file:///{os.getcwd()}/report.{fileformat}">Report</a>'
|
||||||
|
)
|
||||||
|
self.reportprogress.hide()
|
||||||
|
|
||||||
|
|
||||||
|
def launch():
|
||||||
|
import sys
|
||||||
|
|
||||||
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
|
window = ReportUi()
|
||||||
|
sys.exit(app.exec())
|
||||||
66
src/utils/reportThread.py
Normal file
66
src/utils/reportThread.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
from PyQt6.QtCore import QThread, pyqtSignal, QDate
|
||||||
|
from prettytable import PrettyTable
|
||||||
|
from src.logic import Database
|
||||||
|
from src.utils import stringToDate
|
||||||
|
import sqlite3 as sql
|
||||||
|
|
||||||
|
|
||||||
|
class ReportThread(QThread):
|
||||||
|
report_signal = pyqtSignal(str)
|
||||||
|
report_progress_signal = pyqtSignal(int)
|
||||||
|
report_nums_signal = pyqtSignal(int)
|
||||||
|
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super(ReportThread, self).__init__(parent)
|
||||||
|
self.days = None
|
||||||
|
self.format = "txt"
|
||||||
|
|
||||||
|
def setFormat(self, format):
|
||||||
|
self.format = format
|
||||||
|
|
||||||
|
def setDays(self, days):
|
||||||
|
self.days = days
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
db = Database()
|
||||||
|
|
||||||
|
path = db.db_path
|
||||||
|
day = QDate.currentDate().addDays(-self.days).toString("yyyy-MM-dd")
|
||||||
|
query = f"""SELECT * FROM loans WHERE loan_date >= '{day}';"""
|
||||||
|
print(query)
|
||||||
|
colnames = ["UserId", "Title", "Action", "Datum"]
|
||||||
|
table = PrettyTable(colnames)
|
||||||
|
table.align[colnames[0]] = "l"
|
||||||
|
table.align[colnames[1]] = "l"
|
||||||
|
table.align[colnames[2]] = "l"
|
||||||
|
|
||||||
|
with sql.connect(path) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute(query)
|
||||||
|
loans = cursor.fetchall()
|
||||||
|
self.report_nums_signal.emit(len(loans))
|
||||||
|
counter = 0
|
||||||
|
for loan in loans:
|
||||||
|
counter += 1
|
||||||
|
self.report_progress_signal.emit(counter)
|
||||||
|
loan_action = "Ausleihe" if loan[5] == 0 else "Rückgabe"
|
||||||
|
loan_action_date = stringToDate(
|
||||||
|
loan[3] if loan[5] == 0 else loan[6]
|
||||||
|
).toString("dd.MM.yyyy")
|
||||||
|
table.add_row(
|
||||||
|
[
|
||||||
|
loan[1],
|
||||||
|
db.getMedia(loan[2]).title,
|
||||||
|
loan_action,
|
||||||
|
loan_action_date,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
# # print(table)
|
||||||
|
# # wruitng the table to a file
|
||||||
|
if self.format == "csv":
|
||||||
|
with open("report.csv", "w", encoding="utf-8") as f:
|
||||||
|
f.write(table.get_csv_string())
|
||||||
|
else:
|
||||||
|
with open("report.txt", "w", encoding="utf-8") as f:
|
||||||
|
f.write(str(table))
|
||||||
|
self.report_signal.emit("Report generated successfully.")
|
||||||
Reference in New Issue
Block a user