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 .multiUserInfo import MultiUserFound
|
||||||
from .newentry import NewEntry
|
from .newentry import NewEntry
|
||||||
from src import config
|
from src import config
|
||||||
from src.logic import Database, Catalogue
|
from src.logic import Database, Catalogue, Backup
|
||||||
from src.utils.stringtodate import stringToDate
|
from src.utils import stringToDate
|
||||||
from src.schemas import User, Book
|
from src.schemas import User, Book
|
||||||
from PyQt6 import QtCore, QtGui, QtWidgets
|
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||||
import sys
|
import sys
|
||||||
|
import atexit
|
||||||
|
|
||||||
|
backup = Backup()
|
||||||
|
|
||||||
class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -64,7 +66,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
self.input_username.clear()
|
self.input_username.clear()
|
||||||
self.input_userno.clear()
|
self.input_userno.clear()
|
||||||
self.userdata.clear()
|
self.userdata.clear()
|
||||||
self.mediaOverview.clearContents()
|
self.mediaOverview.setRowCount(0)
|
||||||
self.btn_show_lentmedia.setText("")
|
self.btn_show_lentmedia.setText("")
|
||||||
self.input_file_ident.clear()
|
self.input_file_ident.clear()
|
||||||
self.nextReturnDate.hide()
|
self.nextReturnDate.hide()
|
||||||
@@ -106,25 +108,31 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
self.activeUser = user[0]
|
self.activeUser = user[0]
|
||||||
|
|
||||||
if self.activeUser is not None:
|
if self.activeUser is not None:
|
||||||
|
print("User found", self.activeUser)
|
||||||
self.setUserData()
|
self.setUserData()
|
||||||
self.input_file_ident.setFocus()
|
self.input_file_ident.setFocus()
|
||||||
self.mode.setText("Ausleihe")
|
self.mode.setText("Ausleihe")
|
||||||
self.btn_show_lentmedia.setText(self.db.getActiveLoans(self.activeUser.id))
|
self.btn_show_lentmedia.setText(self.db.getActiveLoans(self.activeUser.id))
|
||||||
retdate = self.db.selectClosestReturnDate(self.activeUser.id)
|
retdate = self.db.selectClosestReturnDate(self.activeUser.id)
|
||||||
date = stringToDate(retdate)
|
if retdate:
|
||||||
self.nextReturnDate.setText(date)
|
date = stringToDate(retdate)
|
||||||
self.nextReturnDate.show()
|
self.nextReturnDate.setText(date)
|
||||||
self.label_7.show()
|
self.nextReturnDate.show()
|
||||||
|
self.label_7.show()
|
||||||
|
|
||||||
def moveToLine(self, line):
|
def moveToLine(self, line):
|
||||||
line.setFocus()
|
line.setFocus()
|
||||||
|
|
||||||
def mediaAdd(self, identifier):
|
def mediaAdd(self, identifier):
|
||||||
print("Adding Media", identifier)
|
print("Adding Media", identifier)
|
||||||
|
self.setStatusTip("")
|
||||||
|
self.input_file_ident.clear()
|
||||||
|
self.input_file_ident.setEnabled(False)
|
||||||
|
|
||||||
user_id = self.activeUser.id
|
user_id = self.activeUser.id
|
||||||
cat = Catalogue()
|
cat = Catalogue()
|
||||||
media = cat.get_book(identifier)
|
media = cat.get_book(identifier)
|
||||||
|
print(media)
|
||||||
book_id = self.db.checkMediaExists(media)
|
book_id = self.db.checkMediaExists(media)
|
||||||
print(book_id)
|
print(book_id)
|
||||||
if book_id:
|
if book_id:
|
||||||
@@ -137,6 +145,8 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
if loaned:
|
if loaned:
|
||||||
print("Book already loaned")
|
print("Book already loaned")
|
||||||
self.setStatusTip("Book already loaned")
|
self.setStatusTip("Book already loaned")
|
||||||
|
self.input_file_ident.setEnabled(True)
|
||||||
|
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
print("Book not loaned, loaning now")
|
print("Book not loaned, loaning now")
|
||||||
@@ -156,33 +166,22 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
self.mediaOverview.setItem(
|
self.mediaOverview.setItem(
|
||||||
0, 2, QtWidgets.QTableWidgetItem("Entliehen")
|
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)
|
self.btn_show_lentmedia.setText(self.db.getActiveLoans(self.activeUser.id))
|
||||||
# if media:
|
self.nextReturnDate.setText(
|
||||||
# print(book_id, type(book_id))
|
stringToDate(self.db.selectClosestReturnDate(self.activeUser.id))
|
||||||
# loaned = self.db.checkLoanState(book_id)
|
)
|
||||||
# print(loaned)
|
self.nextReturnDate.show()
|
||||||
# if self.db.checkMediaExists(media):
|
self.label_7.show()
|
||||||
# print("Book already exists", book_id)
|
self.input_file_ident.setEnabled(True)
|
||||||
# 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))
|
|
||||||
|
|
||||||
def callShortcut(self, shortcut):
|
def callShortcut(self, shortcut):
|
||||||
print("Calling Shortcut", shortcut)
|
print("Calling Shortcut", shortcut)
|
||||||
@@ -216,7 +215,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
# set userdata in lineedits
|
# set userdata in lineedits
|
||||||
self.activeUser = user
|
self.activeUser = user
|
||||||
self.setUserData()
|
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.insertRow(0)
|
||||||
self.mediaOverview.setItem(0, 0, QtWidgets.QTableWidgetItem(book.isbn))
|
self.mediaOverview.setItem(0, 0, QtWidgets.QTableWidgetItem(book.isbn))
|
||||||
self.mediaOverview.setItem(0, 1, QtWidgets.QTableWidgetItem(book.title))
|
self.mediaOverview.setItem(0, 1, QtWidgets.QTableWidgetItem(book.title))
|
||||||
@@ -234,6 +233,7 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
print("Book not found")
|
print("Book not found")
|
||||||
|
|
||||||
def lendMedia(self, value):
|
def lendMedia(self, value):
|
||||||
|
value = value.strip()
|
||||||
if value.isnumeric():
|
if value.isnumeric():
|
||||||
self.mediaAdd(value)
|
self.mediaAdd(value)
|
||||||
else:
|
else:
|
||||||
@@ -242,8 +242,22 @@ class MainUI(QtWidgets.QMainWindow, Ui_MainWindow):
|
|||||||
else:
|
else:
|
||||||
self.mediaAdd(value)
|
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():
|
def launch():
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
main_ui = MainUI()
|
main_ui = MainUI()
|
||||||
|
atexit.register(exit_handler)
|
||||||
sys.exit(app.exec())
|
sys.exit(app.exec())
|
||||||
|
# sys.exit(app.exec())
|
||||||
|
|||||||
Reference in New Issue
Block a user