implement backup, fix ret date bug
This commit is contained in:
25
src/logic/backup.py
Normal file
25
src/logic/backup.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
from src import config
|
||||
|
||||
|
||||
class Backup:
|
||||
def __init__(self):
|
||||
self.source_path = config.database.path + "/" + config.database.name
|
||||
self.backup_path = config.database.backupLocation + "/" + config.database.name
|
||||
self.backup = False
|
||||
self.checkpaths()
|
||||
|
||||
def checkpaths(self):
|
||||
if os.path.exists(config.database.backupLocation):
|
||||
self.backup = True
|
||||
|
||||
def createBackup(self):
|
||||
if self.backup:
|
||||
if os.path.exists(self.source_path):
|
||||
if os.path.exists(self.backup_path):
|
||||
os.remove(self.backup_path)
|
||||
shutil.copy(self.source_path, self.backup_path)
|
||||
return True
|
||||
return False
|
||||
@@ -5,12 +5,14 @@ from .createUser import CreateUser
|
||||
from .multiUserInfo import MultiUserFound
|
||||
from .newentry import NewEntry
|
||||
from src import config
|
||||
from src.logic import Database, Catalogue
|
||||
from src.utils.stringtodate import stringToDate
|
||||
from src.logic import Database, Catalogue, Backup
|
||||
from src.utils import stringToDate
|
||||
from src.schemas import User, Book
|
||||
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||
import sys
|
||||
import atexit
|
||||
|
||||
backup = Backup()
|
||||
|
||||
class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
def __init__(self):
|
||||
@@ -64,7 +66,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
self.input_username.clear()
|
||||
self.input_userno.clear()
|
||||
self.userdata.clear()
|
||||
self.mediaOverview.clearContents()
|
||||
self.mediaOverview.setRowCount(0)
|
||||
self.btn_show_lentmedia.setText("")
|
||||
self.input_file_ident.clear()
|
||||
self.nextReturnDate.hide()
|
||||
@@ -106,11 +108,13 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
self.activeUser = user[0]
|
||||
|
||||
if self.activeUser is not None:
|
||||
print("User found", self.activeUser)
|
||||
self.setUserData()
|
||||
self.input_file_ident.setFocus()
|
||||
self.mode.setText("Ausleihe")
|
||||
self.btn_show_lentmedia.setText(self.db.getActiveLoans(self.activeUser.id))
|
||||
retdate = self.db.selectClosestReturnDate(self.activeUser.id)
|
||||
if retdate:
|
||||
date = stringToDate(retdate)
|
||||
self.nextReturnDate.setText(date)
|
||||
self.nextReturnDate.show()
|
||||
@@ -121,10 +125,14 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
|
||||
def mediaAdd(self, identifier):
|
||||
print("Adding Media", identifier)
|
||||
self.setStatusTip("")
|
||||
self.input_file_ident.clear()
|
||||
self.input_file_ident.setEnabled(False)
|
||||
|
||||
user_id = self.activeUser.id
|
||||
cat = Catalogue()
|
||||
media = cat.get_book(identifier)
|
||||
|
||||
print(media)
|
||||
book_id = self.db.checkMediaExists(media)
|
||||
print(book_id)
|
||||
if book_id:
|
||||
@@ -137,6 +145,8 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
if loaned:
|
||||
print("Book already loaned")
|
||||
self.setStatusTip("Book already loaned")
|
||||
self.input_file_ident.setEnabled(True)
|
||||
|
||||
return
|
||||
else:
|
||||
print("Book not loaned, loaning now")
|
||||
@@ -156,33 +166,22 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
self.mediaOverview.setItem(
|
||||
0, 2, QtWidgets.QTableWidgetItem("Entliehen")
|
||||
)
|
||||
else:
|
||||
book_id = self.db.insertMedia(media)
|
||||
self.db.insertLoan(
|
||||
userid=user_id,
|
||||
mediaid=book_id,
|
||||
loandate=self.currentDate.toString(),
|
||||
duedate=self.duedate.date().toString(),
|
||||
)
|
||||
|
||||
# print(media)
|
||||
# if media:
|
||||
# print(book_id, type(book_id))
|
||||
# loaned = self.db.checkLoanState(book_id)
|
||||
# print(loaned)
|
||||
# if self.db.checkMediaExists(media):
|
||||
# print("Book already exists", book_id)
|
||||
# else:
|
||||
# book_id = self.db.insertMedia(media)
|
||||
# print(book_id)
|
||||
# self.db.insertLoan(
|
||||
# self.activeUser.id,
|
||||
# book_id,
|
||||
# self.currentDate.toString(),
|
||||
# self.duedate.date().toString(),
|
||||
# )
|
||||
|
||||
# self.mediaOverview.insertRow(0)
|
||||
# self.mediaOverview.setItem(0, 0, QtWidgets.QTableWidgetItem(media.isbn))
|
||||
# self.mediaOverview.setItem(0, 1, QtWidgets.QTableWidgetItem(media.title))
|
||||
# self.mediaOverview.setItem(0, 2, QtWidgets.QTableWidgetItem("Entliehen"))
|
||||
|
||||
# # add media to database
|
||||
# # check if book present in database
|
||||
# print(self.db.getActiveLoans(self.activeUser.id))
|
||||
# self.btn_show_lentmedia.setText(self.db.getActiveLoans(self.activeUser.id))
|
||||
self.btn_show_lentmedia.setText(self.db.getActiveLoans(self.activeUser.id))
|
||||
self.nextReturnDate.setText(
|
||||
stringToDate(self.db.selectClosestReturnDate(self.activeUser.id))
|
||||
)
|
||||
self.nextReturnDate.show()
|
||||
self.label_7.show()
|
||||
self.input_file_ident.setEnabled(True)
|
||||
|
||||
def callShortcut(self, shortcut):
|
||||
print("Calling Shortcut", shortcut)
|
||||
@@ -216,7 +215,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
# set userdata in lineedits
|
||||
self.activeUser = user
|
||||
self.setUserData()
|
||||
book = self.db.returnMedia(book_id[0])
|
||||
book = self.db.returnMedia(book_id[0], self.currentDate.toString())
|
||||
self.mediaOverview.insertRow(0)
|
||||
self.mediaOverview.setItem(0, 0, QtWidgets.QTableWidgetItem(book.isbn))
|
||||
self.mediaOverview.setItem(0, 1, QtWidgets.QTableWidgetItem(book.title))
|
||||
@@ -234,6 +233,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
print("Book not found")
|
||||
|
||||
def lendMedia(self, value):
|
||||
value = value.strip()
|
||||
if value.isnumeric():
|
||||
self.mediaAdd(value)
|
||||
else:
|
||||
@@ -242,8 +242,22 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||
else:
|
||||
self.mediaAdd(value)
|
||||
|
||||
def exit_handler():
|
||||
print("Exiting")
|
||||
state = backup.createBackup()
|
||||
# create dialog to show state
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
dialog = QtWidgets.QMessageBox()
|
||||
if state == True:
|
||||
dialog.setText("Backup created successfully")
|
||||
else:
|
||||
dialog.setText("Backup creation failed")
|
||||
dialog.exec()
|
||||
|
||||
|
||||
def launch():
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
main_ui = MainUI()
|
||||
atexit.register(exit_handler)
|
||||
sys.exit(app.exec())
|
||||
# sys.exit(app.exec())
|
||||
|
||||
Reference in New Issue
Block a user