diff --git a/src/database/book.py b/src/database/book.py index a34c8a8..19cc76b 100644 --- a/src/database/book.py +++ b/src/database/book.py @@ -29,9 +29,8 @@ def create_new_book(book: Book): def update_book(book: Book): - session = DatabaseManager.get_session() try: - with session: + with DatabaseManager.get_session() as session: logger.debug(f"Updating book {book.title}") existing_book = session.query(Book).get(book.id) @@ -57,10 +56,10 @@ def update_book(book: Book): raise DatabaseConnectionError( "Connection with database interrupted") from e except SQLAlchemyError as e: - logger.error(f"An error occured when saving book: {e}") + logger.error(f"An error occurred when saving book: {e}") session.rollback() raise DatabaseError( - "An error occured when updating the book") from e + "An error occurred when updating the book") from e __all__ = ["create_new_book", "update_book"] diff --git a/src/importer/book/book_importer.py b/src/importer/book/book_importer.py index 9e93ef8..1e8ff15 100644 --- a/src/importer/book/book_importer.py +++ b/src/importer/book/book_importer.py @@ -1,14 +1,19 @@ -from typing import List, Dict import os import logging +import time + +from typing import List, Dict + from xml.etree import ElementTree as ET from xmlschema import XMLSchema +from sqlalchemy.exc import IntegrityError + from database.manager import DatabaseManager from utils.errors.import_error.xsd_scheme_not_found import XsdSchemeNotFoundError from utils.errors.import_error.invalid_contents_error import InvalidContentsError from utils.errors.import_error.import_error import ImportError +from utils.config import UserConfig from models import Book, Author, BookCategory -from sqlalchemy.exc import IntegrityError class BookImporter: def __init__(self): @@ -124,6 +129,12 @@ class BookImporter: categories=filtered_categories ) session.add(book) + user_config = UserConfig() + + if user_config.simulate_slowdown: + self.logger.info("Simulating slowdown for 10 seconds") + time.sleep(10) + # Commit all changes session.commit() except e: diff --git a/src/models/__init__.py b/src/models/__init__.py index 75a6878..70d192d 100644 --- a/src/models/__init__.py +++ b/src/models/__init__.py @@ -2,6 +2,7 @@ from .author import * from .book import * from .book_category import * from .book_category_link import * +from .book_category_statistics import * from .book_overview import * from .member import * from .librarian import * @@ -12,6 +13,7 @@ __all__ = [ *book.__all__, *book_category.__all__, *book_category_link.__all__, + *book_category_statistics.__all__, *book_overview.__all__, *member.__all__, *librarian.__all__, diff --git a/src/models/book.py b/src/models/book.py index caf6c5a..2a885cd 100644 --- a/src/models/book.py +++ b/src/models/book.py @@ -26,8 +26,9 @@ class Book(Base): created_at = Column(TIMESTAMP, nullable=False, server_default=func.now()) last_updated = Column(TIMESTAMP, nullable=False, server_default=func.now()) - author = relationship('Author', back_populates='books') - categories = relationship('BookCategory',secondary='book_category_link',back_populates='books') + author = relationship('Author', back_populates='books') + categories = relationship('BookCategory',secondary='book_category_link',back_populates='books') + book_category_statistics = relationship('BookCategoryStatistics', back_populates='book_category_statistics') __all__ = ["Book", "BookStatusEnum"] diff --git a/src/models/book_category_statistics.py b/src/models/book_category_statistics.py new file mode 100644 index 0000000..4a9888e --- /dev/null +++ b/src/models/book_category_statistics.py @@ -0,0 +1,18 @@ +from sqlalchemy import Column, Integer, ForeignKey +from sqlalchemy.orm import relationship + +from .base import Base + +class BookCategoryStatistics(Base): + __tablename__ = 'book_category_statistics' + + book_category_id = Column(Integer, ForeignKey('book_category.id', ondelete="cascade"), primary_key=True) + count = Column(Integer(unsigned=True), nullable=False, default=0) + + category = relationship( + 'BookCategory', + back_populates='book_category_statistics' + ) + + +__all__ = ["BookCategoryStatistics"]