From b0cc52e0aab2039602efb4c98ff5983c8a049a53 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Thu, 13 May 2021 10:39:36 +0200 Subject: [PATCH] Enable custom column datetime (adv. search not working yet) #1984 --- cps/db.py | 10 ++++++---- cps/editbooks.py | 7 ++++++- cps/static/js/edit_books.js | 6 +----- cps/templates/book_edit.html | 14 ++++++++++++++ cps/templates/detail.html | 2 ++ 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cps/db.py b/cps/db.py index 66c289dd..808d8abe 100644 --- a/cps/db.py +++ b/cps/db.py @@ -59,7 +59,7 @@ except ImportError: log = logger.create() -cc_exceptions = ['datetime', 'comments', 'composite', 'series'] +cc_exceptions = ['comments', 'composite', 'series'] cc_classes = {} Base = declarative_base() @@ -491,23 +491,25 @@ class CalibreDB(): ccdict['value'] = Column(Float) elif row.datatype == 'int': ccdict['value'] = Column(Integer) + elif row.datatype == 'datetime': + ccdict['value'] = Column(TIMESTAMP) elif row.datatype == 'bool': ccdict['value'] = Column(Boolean) else: ccdict['value'] = Column(String) - if row.datatype in ['float', 'int', 'bool']: + if row.datatype in ['float', 'int', 'bool', 'datetime']: ccdict['book'] = Column(Integer, ForeignKey('books.id')) cc_classes[row.id] = type(str('custom_column_' + str(row.id)), (Base,), ccdict) for cc_id in cc_ids: - if (cc_id[1] == 'bool') or (cc_id[1] == 'int') or (cc_id[1] == 'float'): + if cc_id[1] in ['bool', 'int', 'float', 'datetime']: setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]], primaryjoin=( Books.id == cc_classes[cc_id[0]].book), backref='books')) - elif (cc_id[1] == 'series'): + elif cc_id[1] == 'series': setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(books_custom_column_links[cc_id[0]], diff --git a/cps/editbooks.py b/cps/editbooks.py index 45e0f9fe..f231ab0d 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -501,6 +501,11 @@ def edit_cc_data_number(book_id, book, c, to_save, cc_db_value, cc_string): to_save[cc_string] = None elif c.datatype == 'bool': to_save[cc_string] = 1 if to_save[cc_string] == 'True' else 0 + elif c.datatype == 'datetime': + try: + to_save[cc_string] = datetime.strptime(to_save[cc_string], "%Y-%m-%d") + except ValueError: + to_save[cc_string] = db.Books.DEFAULT_PUBDATE if to_save[cc_string] != cc_db_value: if cc_db_value is not None: @@ -559,7 +564,7 @@ def edit_cc_data(book_id, book, to_save): else: cc_db_value = None if to_save[cc_string].strip(): - if c.datatype == 'int' or c.datatype == 'bool' or c.datatype == 'float': + if c.datatype in ['int', 'bool', 'float', "datetime"]: changed, to_save = edit_cc_data_number(book_id, book, c, to_save, cc_db_value, cc_string) else: changed, to_save = edit_cc_data_string(book, c, to_save, cc_db_value, cc_string) diff --git a/cps/static/js/edit_books.js b/cps/static/js/edit_books.js index 8cedf688..fa3885bb 100644 --- a/cps/static/js/edit_books.js +++ b/cps/static/js/edit_books.js @@ -63,6 +63,7 @@ if (!Modernizr.inputtypes.date) { }).trigger("change"); } + /* Takes a prefix, query typeahead callback, Bloodhound typeahead adapter and returns the completions it gets from the bloodhound engine prefixed. @@ -78,11 +79,6 @@ function prefixedSource(prefix, query, cb, bhAdapter) { }); } -/*function getPath() { - var jsFileLocation = $("script[src*=edit_books]").attr("src"); // the js file path - return jsFileLocation.substr(0, jsFileLocation.search("/static/js/edit_books.js")); // the js folder path -}*/ - var authors = new Bloodhound({ name: "authors", datumTokenizer: function datumTokenizer(datum) { diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index c0fc141e..daafc013 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -149,6 +149,20 @@ {% endif %}> {% endif %} + {% if c.datatype == 'datetime' %} + +
+ 0 %} + value="{% if book['custom_column_' ~ c.id][0].value %}{{ book['custom_column_' ~ c.id][0].value|formatdateinput}}{% endif %}" + {% endif %}> + 0 %} + value="{% if book['custom_column_' ~ c.id][0].value %}{{book['custom_column_' ~ c.id][0].value|formatdate}}{% endif %}" + {% endif %}> +
+ {% endif %} + {% if c.datatype == 'enumeration' %}