def updateComboBox(self): self.ui.bookYear_comboBox.clear() self.ui.bookAuthor_comboBox.clear() self.ui.bookGenre_comboBox.clear() self.ui.bookStatus_comboBox.clear() for year in range(FIRST_YEAR_BOOK, ACTUAL_YEAR + 1): self.ui.bookYear_comboBox.addItem(str(year)) authors = book_session.query(Author).all() if len(authors) == 0: self.ui.bookAuthor_comboBox.addItem('No authors founded') self.ui.bookAuthor_comboBox.setDisabled(True) else: for author in authors: self.ui.bookAuthor_comboBox.addItem(str(author.name + " " + author.surname)) self.ui.bookAuthor_comboBox.setDisabled(False) genres = book_session.query(Genre).all() if len(genres) == 0: self.ui.bookGenre_comboBox.addItem('No genres founded') self.ui.bookGenre_comboBox.setDisabled(True) else: for genre in genres: self.ui.bookGenre_comboBox.addItem(genre.name) self.ui.bookGenre_comboBox.setDisabled(False) status = book_session.query(BookStatus).all() for stat in status: self.ui.bookStatus_comboBox.addItem(stat.name)
def getValues(self): books = book_session.query(Book).count() authors = book_session.query(Author).count() genres = book_session.query(Genre).count() self.ui.booksCounter_label.setText(str(books)) self.ui.authorsCounter_label.setText(str(authors)) self.ui.genresCounter_label.setText(str(genres))
def loadData(self): authors = book_session.query(Author).all() self.model = QStandardItemModel(len(authors), len(self.labels)) self.model.setHorizontalHeaderLabels(self.labels) self.filter_proxy_model = QSortFilterProxyModel() self.filter_proxy_model.setFilterKeyColumn(-1) self.filter_proxy_model.setSourceModel(self.model) for row, author in enumerate(authors): author_id = QStandardItem(str(author.id)) author_id.setTextAlignment(Qt.AlignCenter) author_name = QStandardItem(author.name) author_name.setTextAlignment(Qt.AlignCenter) author_surname = QStandardItem(author.surname) author_surname.setTextAlignment(Qt.AlignCenter) self.model.setItem(row, 0, author_id) self.model.setItem(row, 1, author_name) self.model.setItem(row, 2, author_surname) self.ui.allAuthors_tableView.setModel(self.filter_proxy_model) self.ui.allAuthors_tableView.setColumnHidden(0, True) self.ui.searchAuthor_lineEdit.textChanged.connect(self.filter_proxy_model.setFilterRegExp)
def author_details(self): print(self.ui.allAuthors_tableView.selectionModel().selectedRows()) row = self.ui.allAuthors_tableView.selectionModel().selectedRows()[0].row() id = self.filter_proxy_model.index(row, 0).data() author = book_session.query(Author).filter_by(id=id).first() author_view = self.parent().findChild(QWidget, 'author_window') print(author_view) author_view.updateValues(author) self.parent().setCurrentWidget(author_view)
def updateValues(self): try: counter_books = book_session.query(Book).count() genres = book_session.query(Genre).all() self.genre_series.clear() for genre in genres: genre_books = book_session.query(Book).filter_by( genre_id=genre.id).count() self.genre_series.append(genre.name, genre_books / counter_books) for i in range(len(self.genre_series)): slice = QPieSlice() slice = self.genre_series.slices()[i] slice.setLabelVisible(True) authors = book_session.query(Author).all() self.author_series.clear() for author in authors: author_books = book_session.query(Book).filter_by( author_id=author.id).count() self.author_series.append(author.name + " " + author.surname, author_books / counter_books) for i in range(len(self.author_series)): slice = QPieSlice() slice = self.author_series.slices()[i] slice.setLabelVisible(True) self.status_series.clear() all_status = book_session.query(BookStatus).all() counter_status = {} for status in all_status: book_status = book_session.query(Book).filter_by( status_id=status.id).count() self.status_series.append(status.name, book_status / counter_books) for i in range(len(self.status_series)): slice = QPieSlice() slice = self.status_series.slices()[i] slice.setLabelVisible(True) if slice.label() == 'Not readed': slice.setColor(QColor(255, 0, 0)) elif slice.label() == 'In progress': slice.setColor(QColor(0, 0, 255)) elif slice.label() == 'Readed': slice.setColor(QColor(0, 100, 0)) except ZeroDivisionError: home_window = self.parent().findChild(QWidget, 'home_window') self.parent().setCurrentWidget(home_window) openDialog(QMessageBox.Critical, 'Insufficient data for chart', 'Error')
def deleteGenre(self): books = book_session.query(Book).filter_by( genre_id=self.genre.id).all() for book in books: book_session.delete(book) book_session.delete(self.genre) book_session.commit() home_window = self.parent().findChild(QWidget, 'home_window') self.parent().setCurrentWidget(home_window) openDialog(QMessageBox.Information, 'Genre deleted', 'Success')
def book_details(self): row = self.ui.allBooks_tableView.selectionModel().selectedRows( )[0].row() id = self.filter_proxy_model.index(row, 0).data() result = book_session.query( Book, Author, Genre, BookStatus).select_from(Book).filter_by( id=id).join(Author).join(Genre).join(BookStatus).first() print(result) book, author, genre, status = result print(book.id, author.id, genre.id, status.id) book_view = self.parent().findChild(QWidget, 'book_window') book_view.updateValues(book, author, genre, status) self.parent().setCurrentWidget(book_view)
def deleteAuthor(self): #CANCELLO TUTTI I LIBRI ASSOCIATI ALL'AUTORE books = book_session.query(Book).filter_by( author_id=self.author.id).all() for book in books: book_session.delete(book) book_session.delete(self.author) book_session.commit() home_window = self.parent().findChild(QWidget, 'home_window') self.parent().setCurrentWidget(home_window) openDialog(QMessageBox.Information, 'Author deleted', 'Success')
def getColorStatus(status_id, model): #MOLTO PIÙ STRUTTURATO RISPETTO A DEI SEMPLICI CONTROLLI CON I NUMERI status_dict = { 'Not readed': QColor(255, 0, 0), 'In progress': QColor(0, 0, 255), 'Readed': QColor(0, 100, 0), 'Not watched': QColor(255, 0, 0), 'Watched': QColor(0, 100, 0) } if isinstance(model, Book): all_status = book_session.query(BookStatus).all() else: all_status = movie_session.query(MovieStatus).all() for status in all_status: if status.id == status_id: return status_dict[status.name]
def loadData(self): results = book_session.query( Book, Author, Genre, BookStatus).select_from(Book).join( Author).join(Genre).join(BookStatus).all() self.model = QStandardItemModel(len(results), len(self.labels)) self.model.setHorizontalHeaderLabels(self.labels) self.filter_proxy_model = QSortFilterProxyModel() self.filter_proxy_model.setFilterKeyColumn( -1) # LETTURA DA TUTTE LE COLONNE self.filter_proxy_model.setSourceModel(self.model) for row, (book, author, genre, status) in enumerate(results): book_id = QStandardItem(str(book.id)) book_id.setTextAlignment(Qt.AlignCenter) book_title = QStandardItem(book.title) book_title.setTextAlignment(Qt.AlignCenter) book_isbn = QStandardItem(book.isbn) book_isbn.setTextAlignment(Qt.AlignCenter) authorI = QStandardItem(author.name + " " + author.surname) authorI.setTextAlignment(Qt.AlignCenter) genreI = QStandardItem(genre.name) genreI.setTextAlignment(Qt.AlignCenter) status = QStandardItem(status.name) status.setTextAlignment(Qt.AlignCenter) status.setForeground(QBrush(getColorStatus(book.status_id, book))) self.model.setItem(row, 0, book_id) self.model.setItem(row, 1, book_title) self.model.setItem(row, 2, book_isbn) self.model.setItem(row, 3, authorI) self.model.setItem(row, 4, genreI) self.model.setItem(row, 5, status) self.ui.allBooks_tableView.setModel(self.filter_proxy_model) self.ui.allBooks_tableView.setColumnHidden(0, True) self.ui.searchBook_lineEdit.textChanged.connect( self.filter_proxy_model.setFilterRegExp)
def updateValues(self, book, author, genre, status): self.book = book self.author = author self.genre = genre self.status = status self.ui.bookYear_comboBox.clear() self.ui.bookAuthor_comboBox.clear() self.ui.bookGenre_comboBox.clear() self.ui.bookStatus_comboBox.clear() for year in range(FIRST_YEAR_BOOK, ACTUAL_YEAR): self.ui.bookYear_comboBox.addItem(str(year)) authors = book_session.query(Author).all() if len(authors) == 0: self.ui.bookAuthor_comboBox.addItem('No authors founded') self.ui.bookAuthor_comboBox.setDisabled(True) else: for author_item in authors: self.ui.bookAuthor_comboBox.addItem( str(author_item.name + " " + author_item.surname)) self.ui.bookAuthor_comboBox.setDisabled(False) genres = book_session.query(Genre).all() if len(genres) == 0: self.ui.bookGenre_comboBox.addItem('No genres founded') self.ui.bookGenre_comboBox.setDisabled(True) else: for genre in genres: self.ui.bookGenre_comboBox.addItem(genre.name) self.ui.bookGenre_comboBox.setDisabled(False) all_status = book_session.query(BookStatus).all() for status in all_status: self.ui.bookStatus_comboBox.addItem(status.name) # FIRST TAB self.ui.bookTitle_label.setText(self.book.title + ', ' + self.author.name + ' ' + self.author.surname) self.ui.bookIsbn_label.setText(self.book.isbn) self.ui.bookPages_label_2.setText(str(self.book.pages)) self.ui.bookYear_label_2.setText(str(self.book.year)) image = QPixmap(self.book.cover_path) image = image.scaled(self.ui.bookCover_label.width(), self.ui.bookCover_label.height(), QtCore.Qt.KeepAspectRatio) self.ui.bookCover_label.setPixmap(image) self.ui.bookCover_label.setScaledContents(True) #self.ui.bookAuthor_label.setText(self.author.name + ' ' + self.author.surname) self.ui.bookGenre_label.setText(self.genre.name) self.ui.bookDescription_label.setText(self.book.description) #SECOND TAB self.ui.bookTitle_lineEdit.setText(self.book.title) self.ui.bookIsbn_lineEdit.setText(self.book.isbn) self.ui.bookPages_lineEdit.setText(str(self.book.pages)) self.ui.bookCoverPath_label.setText(self.book.cover_path) self.ui.bookYear_comboBox.setCurrentText(str(self.book.year)) self.ui.bookAuthor_comboBox.setCurrentIndex(self.author.id - 1) self.ui.bookGenre_comboBox.setCurrentIndex(self.genre.id - 1) self.ui.bookStatus_comboBox.setCurrentIndex(self.status.id - 1) self.ui.bookDescription_plainTextEdit.setPlainText( self.book.description)
def editBook(self): try: book_title = self.ui.bookTitle_lineEdit.text() isbn = self.ui.bookIsbn_lineEdit.text() pages = self.ui.bookPages_lineEdit.text() year = str(self.ui.bookYear_comboBox.currentText()) author = book_session.query(Author).filter_by( id=self.ui.bookAuthor_comboBox.currentIndex() + 1).first() genre = book_session.query(Genre).filter_by( id=self.ui.bookGenre_comboBox.currentIndex() + 1).first() status = book_session.query(BookStatus).filter_by( id=self.ui.bookStatus_comboBox.currentIndex() + 1).first() if len(book_title) == 0: raise NoInputException('Enter the title of book') elif len(isbn) == 0: raise NoInputException('Enter the ISBN of book') elif len(pages) == 0: raise NoInputException('Enter book number pages') elif author == None: raise NoInputException('Enter the author of the book') elif genre == None: raise NoInputException('Enter the genre of the book') if not pages.isdigit(): raise NoNumericInputException('Pages value not valid') cover_path = self.ui.bookCoverPath_label.text() book_cover_path = self.book.cover_path if cover_path != book_cover_path: file_name = QUrl.fromLocalFile(cover_path).fileName() print(file_name) new_cover_path = join(COVER_PATH, file_name) shutil.copy(cover_path, new_cover_path) if cover_path != NO_COVER_AVAILABLE_PATH: os.remove(book_cover_path) else: new_cover_path = self.book.cover_path description = self.ui.bookDescription_plainTextEdit.toPlainText() updates = { 'title': book_title, 'isbn': isbn, 'pages': pages, 'year': year, 'author_id': author.id, 'genre_id': genre.id, 'cover_path': new_cover_path, 'status_id': status.id, 'description': description } for key, value in updates.items(): setattr(self.book, key, value) book_session.commit() home_window = self.parent().findChild(QWidget, 'home_window') self.parent().setCurrentWidget(home_window) openDialog(QMessageBox.Information, 'Book edited', 'Success') except NoInputException as e: message = e.error_message openDialog(QMessageBox.Critical, message, 'Error') except IntegrityError: openDialog(QMessageBox.Critical, 'Field already inserted', 'Error') book_session.rollback()
def addBook(self): try: book_title = self.ui.bookTitle_lineEdit.text() isbn = self.ui.bookIsbn_lineEdit.text() pages = self.ui.bookPages_lineEdit.text() year = int(self.ui.bookYear_comboBox.currentText()) author = book_session.query(Author).filter_by(id=self.ui.bookAuthor_comboBox.currentIndex() + 1).first() genre = book_session.query(Genre).filter_by(id=self.ui.bookGenre_comboBox.currentIndex() + 1).first() cover_path = self.ui.bookCoverPath_label.text() description = self.ui.bookDescription_plainTextEdit.toPlainText() status = book_session.query(BookStatus).filter_by(id=self.ui.bookStatus_comboBox.currentIndex() + 1).first() if len(book_title) == 0: raise NoInputException('Enter the title of book') elif len(isbn) == 0: raise NoInputException('Enter the ISBN of book') elif len(pages) == 0: raise NoInputException('Enter book number pages') elif author == None: raise NoInputException('Enter the author of the book') elif genre == None: raise NoInputException('Enter the genre of the book') if not pages.isdigit(): raise NoNumericInputException('Pages value not valid') if len(cover_path) == 0: new_cover_path = NO_COVER_AVAILABLE_PATH else: file_name = QUrl.fromLocalFile(cover_path).fileName() new_cover_path = join(COVER_PATH, file_name) shutil.copy(cover_path, new_cover_path) print(new_cover_path) book = Book(title=book_title, isbn=isbn, pages=pages, year=year, author_id=author.id, genre_id=genre.id, description=description, status_id=status.id, cover_path=new_cover_path) book_session.add(book) book_session.commit() #results = session.query(Book, Author, Category).select_from(Book).join(Author).join(Category).all() #print(session.query(Book).join(Book.category_id).join(Book.author_id)).all() #for book, author, category in results: #print(book.title, author.name, author.surname, category.name) self.clearAll() openDialog(QMessageBox.Information, 'Book added', 'Success') except NoInputException as e: message = e.error_message openDialog(QMessageBox.Critical, message, 'Error') except NoNumericInputException as e: message = e.error_message openDialog(QMessageBox.Critical, message, 'Error') except IntegrityError: openDialog(QMessageBox.Critical, 'Book already inserted', 'Error') book_session.rollback()
from database import movie_models with open('json/data.json') as data_json_file: data = json.load(data_json_file) books = data['books'] for book in books: title = book['title'] isbn = book['isbn'] pages = book['pages'] year = book['year'] description = book['description'] author_name = book['author']['name'] author_surname = book['author']['surname'] author = book_session.query(book_models.Author).filter_by( name=author_name, surname=author_surname).first() if not author: author = book_models.Author(name=author_name, surname=author_surname) book_session.add(author) book_session.commit() genre_name = book['genre'] genre = book_session.query( book_models.Genre).filter_by(name=genre_name).first() if not genre: genre = book_models.Genre(name=genre_name) book_session.add(genre) book_session.commit() status_name = book['status']