From 9d5e9b28ae9a290730a4db5446baab25c580ec9b Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 16 Oct 2021 20:46:16 +0200 Subject: [PATCH] Enabled editing of "number" custom_columns in books list --- cps/db.py | 7 +++++-- cps/editbooks.py | 26 +++++++++++++++++++------- cps/static/js/table.js | 4 ++++ cps/templates/book_table.html | 11 +++++++++++ cps/web.py | 9 ++------- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/cps/db.py b/cps/db.py index d8709335..82696768 100644 --- a/cps/db.py +++ b/cps/db.py @@ -395,7 +395,7 @@ class AlchemyEncoder(json.JSONEncoder): if isinstance(o.__class__, DeclarativeMeta): # an SQLAlchemy class fields = {} - for field in [x for x in dir(o) if not x.startswith('_') and x != 'metadata' and x!="password"]: + for field in [x for x in dir(o) if not x.startswith('_') and x != 'metadata' and x != "password"]: if field == 'books': continue data = o.__getattribute__(field) @@ -404,8 +404,11 @@ class AlchemyEncoder(json.JSONEncoder): data = data.replace("'", "\'") elif isinstance(data, InstrumentedList): el = list() + # ele = None for ele in data: - if ele.get: + if hasattr(ele, 'value'): # converter for custom_column values + el = [str(ele.value)] + elif ele.get: el.append(ele.get()) else: el.append(json.dumps(ele, cls=AlchemyEncoder)) diff --git a/cps/editbooks.py b/cps/editbooks.py index d3e1f01e..e44d30c5 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -573,10 +573,19 @@ def edit_cc_data_string(book, c, to_save, cc_db_value, cc_string): getattr(book, cc_string).append(new_cc) return changed, to_save +def edit_single_cc_data(book_id, book, column_id, to_save): + cc = (calibre_db.session.query(db.Custom_Columns) + .filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)) + .filter(db.Custom_Columns.id == column_id) + .all()) + return edit_cc_data(book_id, book, to_save, cc) -def edit_cc_data(book_id, book, to_save): - changed = False +def edit_all_cc_data(book_id, book, to_save): cc = calibre_db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() + return edit_cc_data(book_id, book, to_save, cc) + +def edit_cc_data(book_id, book, to_save, cc): + changed = False for c in cc: cc_string = "custom_column_" + str(c.id) if not c.is_multiple: @@ -811,7 +820,7 @@ def edit_book(book_id): # handle book ratings modif_date |= edit_book_ratings(to_save, book) # handle cc data - modif_date |= edit_cc_data(book_id, book, to_save) + modif_date |= edit_all_cc_data(book_id, book, to_save) if to_save["pubdate"]: try: @@ -1131,10 +1140,6 @@ def edit_list_book(param): lang_names = list() for lang in book.languages: lang_names.append(isoLanguages.get_language_name(get_locale(), lang.lang_code)) - #try: - # lang_names.append(LC.parse(lang.lang_code).get_language_name(get_locale())) - #except UnknownLocaleError: - # lang_names.append(_(isoLanguages.get(part3=lang.lang_code).name)) ret = Response(json.dumps({'success': True, 'newValue': ', '.join(lang_names)}), mimetype='application/json') elif param =='author_sort': @@ -1157,6 +1162,13 @@ def edit_list_book(param): ret = Response(json.dumps({'success': True, 'newValue': ' & '.join([author.replace('|',',') for author in input_authors])}), mimetype='application/json') + elif param.startswith("custom_column_"): + new_val = dict() + new_val[param] = vals['value'] + edit_single_cc_data(book.id, book, param[14:], new_val) + ret = Response(json.dumps({'success': True, 'newValue': vals['value']}), + mimetype='application/json') + book.last_modified = datetime.utcnow() try: calibre_db.session.commit() diff --git a/cps/static/js/table.js b/cps/static/js/table.js index a55ec5d1..ac597c4a 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -639,6 +639,10 @@ function singlecheckboxFormatter(value, row){ return ''; } +function ratingFormatter(value, row) { + return (value/2); +} + /* Do some hiding disabling after user list is loaded */ function loadSuccess() { diff --git a/cps/templates/book_table.html b/cps/templates/book_table.html index f26e488d..3722d6c1 100644 --- a/cps/templates/book_table.html +++ b/cps/templates/book_table.html @@ -59,6 +59,17 @@ {{ text_table_row('languages', _('Enter Languages'),_('Languages'), false, true) }} {{ text_table_row('publishers', _('Enter Publishers'),_('Publishers'), false, true) }} + {% for c in cc %} + {% if c.datatype == "int" %} + {{c.name}} + {% elif c.datatype == "rating" %} + {{c.name}} + {% elif c.datatype == "float" %} + {{c.name}} + {% else %} + + {% endif %} + {% endfor %} {% if g.user.role_delete_books() and g.user.role_edit()%} {{_('Delete')}} {% endif %} diff --git a/cps/web.py b/cps/web.py index 0627d490..f8a8610f 100644 --- a/cps/web.py +++ b/cps/web.py @@ -765,7 +765,8 @@ def books_list(data, sort_param, book_id, page): @login_required def books_table(): visibility = current_user.view_settings.get('table', {}) - return render_title_template('book_table.html', title=_(u"Books List"), page="book_table", + cc = get_cc_columns(filter_config_custom_read=True) + return render_title_template('book_table.html', title=_(u"Books List"), cc=cc, page="book_table", visiblility=visibility) @web.route("/ajax/listbooks") @@ -822,12 +823,6 @@ def list_books(): for index in range(0, len(entry.languages)): entry.languages[index].language_name = isoLanguages.get_language_name(get_locale(), entry.languages[ index].lang_code) - #try: - # entry.languages[index].language_name = LC.parse(entry.languages[index].lang_code)\ - # .get_language_name(get_locale()) - #except UnknownLocaleError: - # entry.languages[index].language_name = _( - # isoLanguages.get(part3=entry.languages[index].lang_code).name) table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": entries} js_list = json.dumps(table_entries, cls=db.AlchemyEncoder)