New database columns, allow sorting by newest

This commit is contained in:
BasioMeusPuga
2018-01-16 23:24:22 +05:30
parent 82befeb894
commit 353eef2203
4 changed files with 77 additions and 30 deletions

View File

@@ -19,6 +19,9 @@
import os import os
import pickle import pickle
import sqlite3 import sqlite3
import datetime
from PyQt5 import QtCore
class DatabaseInit: class DatabaseInit:
@@ -39,8 +42,9 @@ class DatabaseInit:
# addition mode # addition mode
self.database.execute( self.database.execute(
"CREATE TABLE books \ "CREATE TABLE books \
(id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, \ (id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, DateAdded TEXT, \
Path TEXT, Position BLOB, ISBN TEXT, Tags TEXT, Hash TEXT, CoverImage BLOB)") Path TEXT, Position BLOB, ISBN TEXT, Tags TEXT, DirTags TEXT, Hash TEXT, \
Bookmarks BLOB, CoverImage BLOB)")
# CheckState is the standard QtCore.Qt.Checked / Unchecked # CheckState is the standard QtCore.Qt.Checked / Unchecked
self.database.execute( self.database.execute(
@@ -83,6 +87,14 @@ class DatabaseFunctions:
# whatever else needs insertion # whatever else needs insertion
# Haha I said insertion # Haha I said insertion
# Add the current datetime value to each file's database entry
# current_time = datetime.datetime.now()
current_datetime = QtCore.QDateTime().currentDateTime()
current_datetime_bin = sqlite3.Binary(pickle.dumps(current_datetime))
# TODO
# Account for directory tags
for i in data.items(): for i in data.items():
book_hash = i[0] book_hash = i[0]
title = i[1]['title'] title = i[1]['title']
@@ -98,8 +110,8 @@ class DatabaseFunctions:
sql_command_add = ( sql_command_add = (
"INSERT OR REPLACE INTO \ "INSERT OR REPLACE INTO \
books (Title, Author, Year, Path, ISBN, Tags, Hash, CoverImage) \ books (Title, Author, Year, DateAdded, Path, ISBN, Tags, Hash, CoverImage) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?)") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
cover_insert = None cover_insert = None
if cover: if cover:
@@ -107,7 +119,7 @@ class DatabaseFunctions:
self.database.execute( self.database.execute(
sql_command_add, sql_command_add,
[title, author, year, [title, author, year, current_datetime_bin,
path, isbn, tags, book_hash, cover_insert]) path, isbn, tags, book_hash, cover_insert])
self.database.commit() self.database.commit()

View File

@@ -22,9 +22,9 @@
import os import os
import pickle import pickle
import database
from PyQt5 import QtGui, QtCore from PyQt5 import QtGui, QtCore
import database
from models import MostExcellentTableModel, TableProxyModel from models import MostExcellentTableModel, TableProxyModel
@@ -44,7 +44,8 @@ class Library:
books = database.DatabaseFunctions( books = database.DatabaseFunctions(
self.parent.database_path).fetch_data( self.parent.database_path).fetch_data(
('Title', 'Author', 'Year', 'Path', 'Position', 'ISBN', 'Tags', 'Hash',), ('Title', 'Author', 'Year', 'DateAdded', 'Path',
'Position', 'ISBN', 'Tags', 'Hash',),
'books', 'books',
{'Title': ''}, {'Title': ''},
'LIKE') 'LIKE')
@@ -59,14 +60,15 @@ class Library:
# database using background threads # database using background threads
books = [] books = []
current_qdatetime = QtCore.QDateTime().currentDateTime()
for i in parsed_books.items(): for i in parsed_books.items():
_tags = i[1]['tags'] _tags = i[1]['tags']
if _tags: if _tags:
_tags = ', '.join([j for j in _tags if j]) _tags = ', '.join([j for j in _tags if j])
books.append([ books.append([
i[1]['title'], i[1]['author'], i[1]['year'], i[1]['path'], i[1]['title'], i[1]['author'], i[1]['year'], current_qdatetime,
None, i[1]['isbn'], _tags, i[0]]) i[1]['path'], None, i[1]['isbn'], _tags, i[0]])
else: else:
return return
@@ -78,11 +80,15 @@ class Library:
title = i[0] title = i[0]
author = i[1] author = i[1]
year = i[2] year = i[2]
path = i[3] path = i[4]
tags = i[6] tags = i[7]
# cover = i[9]
position = i[4] try:
date_added = pickle.loads(i[3])
except TypeError: # Because of datetime.datetime.now() above
date_added = i[3]
position = i[5]
if position: if position:
position = pickle.loads(position) position = pickle.loads(position)
@@ -92,11 +98,12 @@ class Library:
'title': title, 'title': title,
'author': author, 'author': author,
'year': year, 'year': year,
'date_added': date_added,
'path': path, 'path': path,
'position': position, 'position': position,
'isbn': i[5], 'isbn': i[6],
'tags': tags, 'tags': tags,
'hash': i[7], 'hash': i[8],
'file_exists': file_exists} 'file_exists': file_exists}
tooltip_string = title + '\nAuthor: ' + author + '\nYear: ' + str(year) tooltip_string = title + '\nAuthor: ' + author + '\nYear: ' + str(year)
@@ -129,9 +136,10 @@ class Library:
item.setData(all_metadata, QtCore.Qt.UserRole + 3) item.setData(all_metadata, QtCore.Qt.UserRole + 3)
item.setData(search_workaround, QtCore.Qt.UserRole + 4) item.setData(search_workaround, QtCore.Qt.UserRole + 4)
item.setData(file_exists, QtCore.Qt.UserRole + 5) item.setData(file_exists, QtCore.Qt.UserRole + 5)
item.setData(i[7], QtCore.Qt.UserRole + 6) # File hash item.setData(i[8], QtCore.Qt.UserRole + 6) # File hash
item.setData(position, QtCore.Qt.UserRole + 7) item.setData(position, QtCore.Qt.UserRole + 7)
item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed? item.setData(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed?
item.setData(date_added, QtCore.Qt.UserRole + 9)
item.setIcon(QtGui.QIcon(img_pixmap)) item.setIcon(QtGui.QIcon(img_pixmap))
self.view_model.appendRow(item) self.view_model.appendRow(item)
@@ -183,10 +191,29 @@ class Library:
self.parent.statusMessage.setText( self.parent.statusMessage.setText(
str(self.proxy_model.rowCount()) + ' books') str(self.proxy_model.rowCount()) + ' books')
# Sorting according to roles and the drop down in the library # TODO
# Allow sorting by type
# Index of the sorting drop down corresponding to the
# UserRole of the item model
# This keeps from having to rearrange all the UserRoles in the
# existing model
sort_roles = {
0: 0,
1: 1,
2: 2,
3: 9}
# Sorting according to roles and the drop down in the library toolbar
self.proxy_model.setSortRole( self.proxy_model.setSortRole(
QtCore.Qt.UserRole + self.parent.libraryToolBar.sortingBox.currentIndex()) QtCore.Qt.UserRole + sort_roles[self.parent.libraryToolBar.sortingBox.currentIndex()])
self.proxy_model.sort(0)
# This can be expanded to other fields by appending to the list
sort_order = QtCore.Qt.AscendingOrder
if self.parent.libraryToolBar.sortingBox.currentIndex() in [3]:
sort_order = QtCore.Qt.DescendingOrder
self.proxy_model.sort(0, sort_order)
self.parent.start_culling_timer() self.parent.start_culling_timer()
def prune_models(self, valid_paths): def prune_models(self, valid_paths):

View File

@@ -111,20 +111,22 @@ class BookSorter:
self.hashes_and_paths = { self.hashes_and_paths = {
i[0]: i[1] for i in all_hashes_and_paths} i[0]: i[1] for i in all_hashes_and_paths}
def database_position(self, file_hash): def database_entry_for_book(self, file_hash):
position = database.DatabaseFunctions( database_return = database.DatabaseFunctions(
self.database_path).fetch_data( self.database_path).fetch_data(
('Position',), ('DateAdded', 'Position', 'Bookmarks'),
'books', 'books',
{'Hash': file_hash}, {'Hash': file_hash},
'EQUALS', 'EQUALS',
True) True)
if position: book_data = []
position_dict = pickle.loads(position) for i in database_return:
return position_dict if i:
book_data.append(pickle.loads(i))
else: else:
return None book_data.append(None)
return book_data
def read_book(self, filename): def read_book(self, filename):
# filename is expected as a string containg the # filename is expected as a string containg the
@@ -216,9 +218,15 @@ class BookSorter:
if not content.keys(): if not content.keys():
content['Invalid'] = 'Possible Parse Error' content['Invalid'] = 'Possible Parse Error'
position = self.database_position(file_md5) book_data = self.database_entry_for_book(file_md5)
date_added = book_data[0]
position = book_data[1]
bookmarks = book_data[2]
this_book[file_md5]['date_added'] = date_added
this_book[file_md5]['position'] = position this_book[file_md5]['position'] = position
this_book[file_md5]['bookmarks'] = bookmarks
this_book[file_md5]['content'] = content this_book[file_md5]['content'] = content
this_book[file_md5]['images_only'] = images_only this_book[file_md5]['images_only'] = images_only

View File

@@ -333,7 +333,7 @@ class LibraryToolBar(QtWidgets.QToolBar):
self.searchBar.setObjectName('searchBar') self.searchBar.setObjectName('searchBar')
# Sorter # Sorter
sorting_choices = ['Title', 'Author', 'Year'] sorting_choices = ['Title', 'Author', 'Year', 'Newest']
self.sortingBox = FixedComboBox(self) self.sortingBox = FixedComboBox(self)
self.sortingBox.addItems(sorting_choices) self.sortingBox.addItems(sorting_choices)
self.sortingBox.setObjectName('sortingBox') self.sortingBox.setObjectName('sortingBox')