From a7b1a5111a936373d454dcaaa1e2cf4ab627f08e Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 6 Jan 2024 18:32:05 +0300 Subject: [PATCH] Feat(wx/books): List books for an author [skip ci] --- m_librarian/wx/ListAuthors.py | 4 ++- m_librarian/wx/ListBooks.py | 55 +++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/m_librarian/wx/ListAuthors.py b/m_librarian/wx/ListAuthors.py index 96f5e8b..15cb59c 100644 --- a/m_librarian/wx/ListAuthors.py +++ b/m_librarian/wx/ListAuthors.py @@ -2,6 +2,7 @@ import wx, wx.grid # noqa: E401 multiple imports on one line from ..compat import string_type, unicode_type +from ..search import books_by_author from ..translations import translations from .AWindow import AWindow from .ListBooks import ListBooksWindow @@ -67,7 +68,8 @@ class ListAuthorsPanel(wx.Panel): def listBooks(self, row): authors = self.search_authors_results['authors'] author = authors[row] - ListBooksWindow(self, author) + _books_by_author = books_by_author(author.id) + ListBooksWindow(self, _books_by_author) def OnDClick(self, event): row = event.GetRow() diff --git a/m_librarian/wx/ListBooks.py b/m_librarian/wx/ListBooks.py index 97f8826..c8b4923 100644 --- a/m_librarian/wx/ListBooks.py +++ b/m_librarian/wx/ListBooks.py @@ -1,6 +1,8 @@ # coding: utf-8 import wx +from ..compat import string_type, unicode_type +from ..translations import translations from .AWindow import AWindow @@ -9,6 +11,55 @@ class ListBooksWindow(AWindow): session_config_section_name = 'list_books' window_title = u"m_Librarian: Список книг" - def __init__(self, parent, author): - self.author = author + def __init__(self, parent, books_by_author): + self.books_by_author = books_by_author AWindow.__init__(self, parent) + + def OnInit(self): + AWindow.OnInit(self) + ListBooksPanel(self, self.books_by_author) + + +class ListBooksPanel(wx.Panel): + + def __init__(self, parent, books_by_author): + wx.Panel.__init__(self, parent) + self.books_by_author = books_by_author + + list_books_sizer = wx.BoxSizer(wx.VERTICAL) + self.SetSizer(list_books_sizer) + + self.grid = grid = wx.grid.Grid(self) + list_books_sizer.Add(grid, 0, wx.EXPAND, 0) + + self.InitGrid() + + def InitGrid(self): + _ = getattr(translations, 'ugettext', None) or translations.gettext + books_by_author = self.books_by_author['books_by_author'] + columns = self.books_by_author['columns'] + author = next(iter(books_by_author)) + books = books_by_author[author] + grid = self.grid + grid.CreateGrid(len(books), len(columns)) + grid.EnableEditing(False) + for row in range(len(books)): + grid.SetRowLabelValue(row, str(row)) + grid.AutoSizeRowLabelSize(row) + for col, col_name in enumerate(columns): + grid.SetColLabelValue(col, _(col_name)) + grid.AutoSizeColLabelSize(col) + if col_name in ('ser_no', 'size'): + cell_attr = wx.grid.GridCellAttr() + cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE) + grid.SetColAttr(col, cell_attr) + for row, book in enumerate(books): + for col, col_name in enumerate(columns): + value = getattr(book, col_name) + if value is None: + value = u'' + elif not isinstance(value, (string_type, unicode_type)): + value = str(value) + grid.SetCellValue(row, col, value) + grid.AutoSizeColumns() + grid.AutoSizeRows() -- 2.39.2