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 pickle
import sqlite3
import datetime
from PyQt5 import QtCore
class DatabaseInit:
@@ -39,8 +42,9 @@ class DatabaseInit:
# addition mode
self.database.execute(
"CREATE TABLE books \
(id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, \
Path TEXT, Position BLOB, ISBN TEXT, Tags TEXT, Hash TEXT, CoverImage BLOB)")
(id INTEGER PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, DateAdded TEXT, \
Path TEXT, Position BLOB, ISBN TEXT, Tags TEXT, DirTags TEXT, Hash TEXT, \
Bookmarks BLOB, CoverImage BLOB)")
# CheckState is the standard QtCore.Qt.Checked / Unchecked
self.database.execute(
@@ -83,6 +87,14 @@ class DatabaseFunctions:
# whatever else needs 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():
book_hash = i[0]
title = i[1]['title']
@@ -98,8 +110,8 @@ class DatabaseFunctions:
sql_command_add = (
"INSERT OR REPLACE INTO \
books (Title, Author, Year, Path, ISBN, Tags, Hash, CoverImage) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
books (Title, Author, Year, DateAdded, Path, ISBN, Tags, Hash, CoverImage) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
cover_insert = None
if cover:
@@ -107,7 +119,7 @@ class DatabaseFunctions:
self.database.execute(
sql_command_add,
[title, author, year,
[title, author, year, current_datetime_bin,
path, isbn, tags, book_hash, cover_insert])
self.database.commit()

View File

@@ -22,9 +22,9 @@
import os
import pickle
import database
from PyQt5 import QtGui, QtCore
import database
from models import MostExcellentTableModel, TableProxyModel
@@ -44,7 +44,8 @@ class Library:
books = database.DatabaseFunctions(
self.parent.database_path).fetch_data(
('Title', 'Author', 'Year', 'Path', 'Position', 'ISBN', 'Tags', 'Hash',),
('Title', 'Author', 'Year', 'DateAdded', 'Path',
'Position', 'ISBN', 'Tags', 'Hash',),
'books',
{'Title': ''},
'LIKE')
@@ -59,14 +60,15 @@ class Library:
# database using background threads
books = []
current_qdatetime = QtCore.QDateTime().currentDateTime()
for i in parsed_books.items():
_tags = i[1]['tags']
if _tags:
_tags = ', '.join([j for j in _tags if j])
books.append([
i[1]['title'], i[1]['author'], i[1]['year'], i[1]['path'],
None, i[1]['isbn'], _tags, i[0]])
i[1]['title'], i[1]['author'], i[1]['year'], current_qdatetime,
i[1]['path'], None, i[1]['isbn'], _tags, i[0]])
else:
return
@@ -78,11 +80,15 @@ class Library:
title = i[0]
author = i[1]
year = i[2]
path = i[3]
tags = i[6]
# cover = i[9]
path = i[4]
tags = i[7]
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:
position = pickle.loads(position)
@@ -92,11 +98,12 @@ class Library:
'title': title,
'author': author,
'year': year,
'date_added': date_added,
'path': path,
'position': position,
'isbn': i[5],
'isbn': i[6],
'tags': tags,
'hash': i[7],
'hash': i[8],
'file_exists': file_exists}
tooltip_string = title + '\nAuthor: ' + author + '\nYear: ' + str(year)
@@ -129,9 +136,10 @@ class Library:
item.setData(all_metadata, QtCore.Qt.UserRole + 3)
item.setData(search_workaround, QtCore.Qt.UserRole + 4)
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(False, QtCore.Qt.UserRole + 8) # Is the cover being displayed?
item.setData(date_added, QtCore.Qt.UserRole + 9)
item.setIcon(QtGui.QIcon(img_pixmap))
self.view_model.appendRow(item)
@@ -183,10 +191,29 @@ class Library:
self.parent.statusMessage.setText(
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(
QtCore.Qt.UserRole + self.parent.libraryToolBar.sortingBox.currentIndex())
self.proxy_model.sort(0)
QtCore.Qt.UserRole + sort_roles[self.parent.libraryToolBar.sortingBox.currentIndex()])
# 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()
def prune_models(self, valid_paths):

View File

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

View File

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