From cef41661ddd2afea181bb18d311bda1ffc628e5c Mon Sep 17 00:00:00 2001 From: Simon Latapie Date: Sun, 12 Jan 2020 13:25:09 +0100 Subject: [PATCH] custom columns: add Float type support - remove float datatype from cc_exceptions - handle float datatype like int or bool - change edit template to modify input step (0.01 precision, like in calibre UI) --- cps/db.py | 11 ++++++++--- cps/editbooks.py | 4 ++-- cps/templates/book_edit.html | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cps/db.py b/cps/db.py index 47f07bf0..f40e0cda 100755 --- a/cps/db.py +++ b/cps/db.py @@ -25,13 +25,13 @@ import ast from sqlalchemy import create_engine from sqlalchemy import Table, Column, ForeignKey -from sqlalchemy import String, Integer, Boolean, TIMESTAMP +from sqlalchemy import String, Integer, Boolean, TIMESTAMP, Float from sqlalchemy.orm import relationship, sessionmaker, scoped_session from sqlalchemy.ext.declarative import declarative_base session = None -cc_exceptions = ['datetime', 'comments', 'float', 'composite', 'series'] +cc_exceptions = ['datetime', 'comments', 'composite', 'series'] cc_classes = {} engine = None @@ -378,6 +378,11 @@ def setup_db(config): 'id': Column(Integer, primary_key=True), 'book': Column(Integer, ForeignKey('books.id')), 'value': Column(Integer)} + elif row.datatype == 'float': + ccdict = {'__tablename__': 'custom_column_' + str(row.id), + 'id': Column(Integer, primary_key=True), + 'book': Column(Integer, ForeignKey('books.id')), + 'value': Column(Float)} else: ccdict = {'__tablename__': 'custom_column_' + str(row.id), 'id': Column(Integer, primary_key=True), @@ -385,7 +390,7 @@ def setup_db(config): 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'): + if (cc_id[1] == 'bool') or (cc_id[1] == 'int') or (cc_id[1] == 'float'): setattr(Books, 'custom_column_' + str(cc_id[0]), relationship(cc_classes[cc_id[0]], primaryjoin=( Books.id == cc_classes[cc_id[0]].book), diff --git a/cps/editbooks.py b/cps/editbooks.py index de375cac..0bb005fb 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -175,7 +175,7 @@ def delete_book(book_id, book_format): cc_string = "custom_column_" + str(c.id) if not c.is_multiple: if len(getattr(book, cc_string)) > 0: - if c.datatype == 'bool' or c.datatype == 'integer': + if c.datatype == 'bool' or c.datatype == 'integer' or c.datatype == 'float': del_cc = getattr(book, cc_string)[0] getattr(book, cc_string).remove(del_cc) db.session.delete(del_cc) @@ -254,7 +254,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': + if c.datatype == 'int' or c.datatype == 'bool' or c.datatype == 'float': if to_save[cc_string] == 'None': to_save[cc_string] = None elif c.datatype == 'bool': diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 88369f05..78b427eb 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -113,8 +113,8 @@ {% endif %} - {% if c.datatype == 'int' %} - + {% if c.datatype == 'int' or c.datatype == 'float' %} + {% endif %} {% if c.datatype in ['text', 'series'] and not c.is_multiple %}