[main] Improvements to the import
This commit is contained in:
parent
48163325d8
commit
723be8ae16
@ -79,41 +79,58 @@ class BookImporter:
|
|||||||
def save_books(self, books: List[Book]):
|
def save_books(self, books: List[Book]):
|
||||||
"""Saves a list of books to the database."""
|
"""Saves a list of books to the database."""
|
||||||
try:
|
try:
|
||||||
with DatabaseManager.get_session() as session:
|
# Get a session instance
|
||||||
|
session = DatabaseManager.get_session()
|
||||||
|
|
||||||
|
# Use no_autoflush as a context manager
|
||||||
|
with session.no_autoflush:
|
||||||
|
processed_categories = {} # Cache for processed categories by name
|
||||||
|
|
||||||
for book in books:
|
for book in books:
|
||||||
|
self.logger.debug(f"Attempting to save {book.title}")
|
||||||
|
|
||||||
# Check if the author exists, otherwise add
|
# Check if the author exists, otherwise add
|
||||||
existing_author = session.query(Author).filter_by(
|
existing_author = session.query(Author).filter_by(
|
||||||
first_name=book.author.first_name,
|
first_name=book.author.first_name,
|
||||||
last_name=book.author.last_name
|
last_name=book.author.last_name
|
||||||
).first()
|
).one_or_none()
|
||||||
if existing_author:
|
|
||||||
|
if existing_author is not None:
|
||||||
|
self.logger.debug(f"Author {existing_author.first_name} {existing_author.last_name} already exists. Reusing")
|
||||||
book.author = existing_author
|
book.author = existing_author
|
||||||
else:
|
else:
|
||||||
|
self.logger.debug(f"Creating new author: {book.author.first_name} {book.author.last_name}")
|
||||||
session.add(book.author)
|
session.add(book.author)
|
||||||
session.commit()
|
|
||||||
|
|
||||||
# Handle categories
|
# Handle categories
|
||||||
new_categories = []
|
filtered_categories = []
|
||||||
for category in book.categories:
|
for category in book.categories:
|
||||||
existing_category = session.query(BookCategory).filter_by(name=category.name).first()
|
existing_category = session.query(BookCategory).filter_by(name=category.name).one_or_none()
|
||||||
if existing_category:
|
|
||||||
new_categories.append(existing_category)
|
if existing_category is not None:
|
||||||
|
self.logger.debug(f"Category {existing_category.name} already exists. Reusing")
|
||||||
|
filtered_categories.append(session.merge(existing_category))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.logger.debug(f"Adding new category: {category.name}")
|
self.logger.debug(f"Adding new category: {category.name}")
|
||||||
session.add(category)
|
session.add(category) # Add new category to the session
|
||||||
session.commit()
|
filtered_categories.append(category) # Use the new category
|
||||||
new_categories.append(category)
|
|
||||||
# Replace book categories with the resolved categories
|
book.categories = filtered_categories
|
||||||
book.categories = new_categories
|
|
||||||
|
|
||||||
# Check if the book already exists
|
# Check if the book already exists
|
||||||
existing_book = session.query(Book).filter_by(isbn=book.isbn).first()
|
existing_book = session.query(Book).filter_by(isbn=book.isbn).first()
|
||||||
if not existing_book:
|
if not existing_book:
|
||||||
session.add(book)
|
session.add(book)
|
||||||
else:
|
else:
|
||||||
self.logger.warning(f"Book with ISBN {book.isbn} already exists. Skipping.")
|
self.logger.warning(f"ISBN {book.isbn} already exists. Skipping.")
|
||||||
|
continue
|
||||||
|
|
||||||
# Commit all changes
|
# Commit all changes
|
||||||
session.commit()
|
session.commit()
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
raise ImportError(f"An error occurred when importing books: {e}") from e
|
raise ImportError(f"An error occurred when importing books: {e}") from e
|
||||||
|
finally:
|
||||||
|
# Clean up the session
|
||||||
|
session.close()
|
||||||
|
@ -43,16 +43,9 @@ class LibraryDashboard(QWidget):
|
|||||||
|
|
||||||
# Align the cards to the top
|
# Align the cards to the top
|
||||||
self.scroll_layout.setAlignment(Qt.AlignTop)
|
self.scroll_layout.setAlignment(Qt.AlignTop)
|
||||||
|
self.books = []
|
||||||
# Example cards
|
|
||||||
self.books = self.fetch_books_from_db()
|
|
||||||
self.book_cards = []
|
self.book_cards = []
|
||||||
|
self.redraw_cards()
|
||||||
for book in self.books:
|
|
||||||
card = BookCard(book)
|
|
||||||
|
|
||||||
self.scroll_layout.addWidget(card)
|
|
||||||
self.book_cards.append(card)
|
|
||||||
|
|
||||||
self.scroll_widget.setLayout(self.scroll_layout)
|
self.scroll_widget.setLayout(self.scroll_layout)
|
||||||
self.scroll_area.setWidget(self.scroll_widget)
|
self.scroll_area.setWidget(self.scroll_widget)
|
||||||
@ -83,6 +76,30 @@ class LibraryDashboard(QWidget):
|
|||||||
QMessageBox.information(self, "Add Borrow Record",
|
QMessageBox.information(self, "Add Borrow Record",
|
||||||
"Open dialog to add a borrow record.")
|
"Open dialog to add a borrow record.")
|
||||||
|
|
||||||
|
def clear_layout(self, layout):
|
||||||
|
while layout.count():
|
||||||
|
item = layout.takeAt(0)
|
||||||
|
widget = item.widget()
|
||||||
|
if widget is not None:
|
||||||
|
widget.deleteLater()
|
||||||
|
else:
|
||||||
|
sub_layout = item.layout()
|
||||||
|
if sub_layout is not None:
|
||||||
|
self.clear_layout(sub_layout)
|
||||||
|
del item
|
||||||
|
|
||||||
|
def redraw_cards(self):
|
||||||
|
self.clear_layout(self.scroll_layout)
|
||||||
|
self.book_cards = []
|
||||||
|
|
||||||
|
self.books = self.fetch_books_from_db()
|
||||||
|
|
||||||
|
for book in self.books:
|
||||||
|
card = BookCard(book)
|
||||||
|
|
||||||
|
self.scroll_layout.addWidget(card)
|
||||||
|
self.book_cards.append(card)
|
||||||
|
|
||||||
def fetch_books_from_db(self):
|
def fetch_books_from_db(self):
|
||||||
"""Fetch all books from the database."""
|
"""Fetch all books from the database."""
|
||||||
try:
|
try:
|
||||||
|
@ -37,7 +37,9 @@ class LibraryWindow(QtWidgets.QMainWindow):
|
|||||||
central_widget = QtWidgets.QTabWidget()
|
central_widget = QtWidgets.QTabWidget()
|
||||||
self.setCentralWidget(central_widget)
|
self.setCentralWidget(central_widget)
|
||||||
|
|
||||||
central_widget.addTab(LibraryDashboard(), "Dashboard")
|
|
||||||
|
self.dashboard = LibraryDashboard()
|
||||||
|
central_widget.addTab(self.dashboard, "Dashboard")
|
||||||
central_widget.addTab(MemberEditor(), "Members")
|
central_widget.addTab(MemberEditor(), "Members")
|
||||||
|
|
||||||
self.file_types = {
|
self.file_types = {
|
||||||
@ -198,6 +200,7 @@ class LibraryWindow(QtWidgets.QMainWindow):
|
|||||||
importer.save_books(new_books)
|
importer.save_books(new_books)
|
||||||
QMessageBox.information(
|
QMessageBox.information(
|
||||||
self, "Success", "Books imported successfully!", QMessageBox.Ok)
|
self, "Success", "Books imported successfully!", QMessageBox.Ok)
|
||||||
|
self.dashboard.redraw_cards()
|
||||||
else:
|
else:
|
||||||
QMessageBox.information(
|
QMessageBox.information(
|
||||||
self, "Canceled", "Import was canceled.", QMessageBox.Ok)
|
self, "Canceled", "Import was canceled.", QMessageBox.Ok)
|
||||||
|
@ -30,7 +30,7 @@ class DatabaseManager():
|
|||||||
database_config.name),
|
database_config.name),
|
||||||
pool_pre_ping=True)
|
pool_pre_ping=True)
|
||||||
if self.test_connection():
|
if self.test_connection():
|
||||||
self.session_local = sessionmaker(bind=self.engine)
|
self.Session = sessionmaker(bind=self.engine)
|
||||||
|
|
||||||
def cleanup(self) -> None:
|
def cleanup(self) -> None:
|
||||||
self.logger.debug("Closing connection")
|
self.logger.debug("Closing connection")
|
||||||
@ -49,4 +49,4 @@ class DatabaseManager():
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_session(cls) -> Session:
|
def get_session(cls) -> Session:
|
||||||
return DatabaseManager._instance.session_local()
|
return DatabaseManager._instance.Session()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user