From 329a7a03a5103a6594a39447d187fa2aebd1caa8 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Mon, 22 Jun 2020 19:11:03 +0200 Subject: [PATCH] Fix for errors editing/uploading books with duplicate tags/authors/language names Additional parsing of epubs for cover files Fix for change of database with linked read_column and read column isn't present any more --- cps/editbooks.py | 12 +++++++++--- cps/epub.py | 11 ++++++++--- cps/helper.py | 8 ++++++++ cps/web.py | 6 +++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/cps/editbooks.py b/cps/editbooks.py index abef41ce..f14da1bf 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -317,7 +317,8 @@ def edit_book_ratings(to_save, book): def edit_book_tags(tags, book): input_tags = tags.split(',') input_tags = list(map(lambda it: it.strip(), input_tags)) - # if input_tags[0] !="": ?? + # Remove duplicates + input_tags = helper.uniq(input_tags) return modify_database_object(input_tags, book.tags, db.Tags, calibre_db.session, 'tags') @@ -331,8 +332,6 @@ def edit_book_series_index(series_index, book): # Add default series_index to book modif_date = False series_index = series_index or '1' - #if series_index == '': - # series_index = '1' if book.series_index != series_index: book.series_index = series_index modif_date = True @@ -366,6 +365,8 @@ def edit_book_languages(languages, book, upload=False): if input_l[0] != current_user.filter_language() and current_user.filter_language() != "all": input_l[0] = calibre_db.session.query(db.Languages). \ filter(db.Languages.lang_code == current_user.filter_language()).first() + # Remove duplicates + input_l = helper.uniq(input_l) return modify_database_object(input_l, book.languages, db.Languages, calibre_db.session, 'languages') @@ -568,6 +569,8 @@ def edit_book(book_id): # handle author(s) input_authors = to_save["author_name"].split('&') input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors)) + # Remove duplicates in authors list + input_authors = helper.uniq(input_authors) # we have all author names now if input_authors == ['']: input_authors = [_(u'Unknown')] # prevent empty Author @@ -738,6 +741,9 @@ def upload(): input_authors = authr.split('&') # handle_authors(input_authors) input_authors = list(map(lambda it: it.strip().replace(',', '|'), input_authors)) + # Remove duplicates in authors list + input_authors = helper.uniq(input_authors) + # we have all author names now if input_authors == ['']: input_authors = [_(u'Unknown')] # prevent empty Author diff --git a/cps/epub.py b/cps/epub.py index 8b988084..f863db61 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -122,9 +122,14 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension): markupTree = etree.fromstring(markup) # no matter xhtml or html with no namespace imgsrc = markupTree.xpath("//*[local-name() = 'img']/@src") - # imgsrc maybe startwith "../"" so fullpath join then relpath to cwd - filename = os.path.relpath(os.path.join(os.path.dirname(os.path.join(coverpath, coversection[0])), imgsrc[0])) - coverfile = extractCover(epubZip, filename, "", tmp_file_path) + # Alternative image source + if not len(imgsrc): + imgsrc = markupTree.xpath("//attribute::*[contains(local-name(), 'href')]") + if len(imgsrc): + # imgsrc maybe startwith "../"" so fullpath join then relpath to cwd + filename = os.path.relpath(os.path.join(os.path.dirname(os.path.join(coverpath, coversection[0])), + imgsrc[0])) + coverfile = extractCover(epubZip, filename, "", tmp_file_path) else: coverfile = extractCover(epubZip, coversection[0], coverpath, tmp_file_path) diff --git a/cps/helper.py b/cps/helper.py index b9463416..506613eb 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -468,6 +468,14 @@ def generate_random_password(): passlen = 8 return "".join(s[c % len(s)] for c in os.urandom(passlen)) + +def uniq(input): + output = [] + for x in input: + if x not in output: + output.append(x) + return output + ################################## External interface diff --git a/cps/web.py b/cps/web.py index 6c3aabfd..dacb951d 100644 --- a/cps/web.py +++ b/cps/web.py @@ -446,7 +446,7 @@ def toggle_read(book_id): new_cc = cc_class(value=1, book=book_id) calibre_db.session.add(new_cc) calibre_db.session.commit() - except KeyError: + except (KeyError, AttributeError): log.error(u"Custom Column No.%d is not exisiting in calibre database", config.config_read_column) except OperationalError as e: calibre_db.session.rollback() @@ -1204,7 +1204,7 @@ def render_read_books(page, are_read, as_xml=False, order=None, *args, **kwargs) db_filter, order, db.cc_classes[config.config_read_column]) - except KeyError: + except (KeyError, AttributeError): log.error("Custom Column No.%d is not existing in calibre database", config.config_read_column) if not as_xml: flash(_("Custom Column No.%(column)d is not existing in calibre database", @@ -1711,7 +1711,7 @@ def show_book(book_id): try: matching_have_read_book = getattr(entries, 'custom_column_' + str(config.config_read_column)) have_read = len(matching_have_read_book) > 0 and matching_have_read_book[0].value - except KeyError: + except (KeyError, AttributeError): log.error("Custom Column No.%d is not existing in calibre database", config.config_read_column) have_read = None