Skip to content

Commit

Permalink
Make edit metadata pass all data in a single REST call. Modularize ed…
Browse files Browse the repository at this point in the history
…itbooks
  • Loading branch information
jmarmstrong1207 committed Aug 3, 2024
1 parent 96fb2c1 commit a335dd7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 50 deletions.
71 changes: 71 additions & 0 deletions cps/editbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,77 @@ def table_get_custom_enum(c_id):
@edit_required
def edit_list_book(param):
vals = request.form.to_dict()
return edit_book_param(param, vals)

@editbook.route("/ajax/editselectedbooks", methods=['POST'])
@login_required_if_no_ano
@edit_required
def edit_selected_books():
d = request.get_json()
selections = d.get('selections')
title = d.get('title')
title_sort = d.get('title_sort')
author_sort = d.get('author_sortj')
authors = d.get('authors')
categories = d.get('categories')
series = d.get('series')
languages = d.get('languages')
publishers = d.get('publishers')
comments = d.get('comments')



if len(selections) != 0:
for book_id in selections:
vals = {
"pk": book_id,
"value": None,
}
if title:
vals['value'] = title
edit_book_param('title', vals)
if title_sort:
vals['value'] = title_sort
edit_book_param('sort', vals)
if author_sort:
vals['value'] = author_sort
edit_book_param('author_sort', vals)
if authors:
vals['value'] = authors
edit_book_param('authors', vals)
if categories:
vals['value'] = categories
edit_book_param('tags', vals)
if series:
vals['value'] = series
edit_book_param('series', vals)
if languages:
vals['value'] = languages
edit_book_param('languages', vals)
if publishers:
vals['value'] = publishers
edit_book_param('publishers', vals)
if comments:
vals['value'] = comments
edit_book_param('comments', vals)

return json.dumps({'success': True})
return ""

# Separated from /editbooks so that /editselectedbooks can also use this
#
# param: the property of the book to be changed
# vals - JSON Object:
# {
# 'pk': "the book id",
# 'value': "changes value of param to what's passed here"
# 'checkA': "Optional. Used to check if autosort author is enabled. Assumed as true if not passed"
# 'checkT': "Optional. Used to check if autotitle author is enabled. Assumed as true if not passed"
# }
#
@login_required_if_no_ano
@edit_required
def edit_book_param(param, vals):
book = calibre_db.get_book(vals['pk'])
sort_param = ""
ret = ""
Expand Down
84 changes: 34 additions & 50 deletions cps/static/js/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,57 +203,41 @@ $(function() {
});

$("#edit_selected_confirm").click(function(event) {
let title = $("#title_input").val()
let title_sort = $("#title_sort_input").val()
let author_sort = $("#author_sort_input").val()
let authors = $("#authors_input").val()
let categories = $("#categories_input").val()
let series = $("#series_input").val()
let languages = $("#languages_input").val()
let publishers = $("#publishers_input").val()
let comments = $("#comments_input").val().toString()

function loopThrough(param, value)
{
selections.forEach((book_id) => {
$.ajax({
method: "post",
url: getPath() + "/ajax/editbooks/" + param,
data: { pk: book_id, value: value },
error: function (data) {
handleListServerResponse([
{ type: "danger", message: data.responseText },
]);
},
success: function success(booTitles) {
$("#books-table").bootstrapTable("refresh");
$("#books-table").bootstrapTable("uncheckAll");

$("#title_input").val("");
$("#title_sort_input").val("");
$("#author_sort_input").val("");
$("#authors_input").val("");
$("#categories_input").val("");
$("#series_input").val("");
$("#languages_input").val("");
$("#publishers_input").val("");
$("#comments_input").val("");

handleListServerResponse;
},
});
})
}

if (title) loopThrough('title', title);
if (title_sort) loopThrough('sort', title_sort);
if (author_sort) loopThrough('author_sort', author_sort);
if (authors) loopThrough('authors', authors);
if (categories) loopThrough('tags', categories);
if (series) loopThrough('series', series);
if (languages) loopThrough('languages', languages);
if (publishers) loopThrough('publishers', publishers);
if (comments) loopThrough('comments', comments);
$.ajax({
method:"post",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: window.location.pathname + "/../ajax/editselectedbooks",
data: JSON.stringify({
"selections": selections,
"title": $("#title_input").val(),
"title_sort": $("#title_sort_input").val(),
"author_sort": $("#author_sort_input").val(),
"authors": $("#authors_input").val(),
"categories": $("#categories_input").val(),
"series": $("#series_input").val(),
"languages": $("#languages_input").val(),
"publishers": $("#publishers_input").val(),
"comments": $("#comments_input").val().toString(),
}),
success: function success(booTitles) {
$("#books-table").bootstrapTable("refresh");
$("#books-table").bootstrapTable("uncheckAll");

$("#title_input").val("");
$("#title_sort_input").val("");
$("#author_sort_input").val("");
$("#authors_input").val("");
$("#categories_input").val("");
$("#series_input").val("");
$("#languages_input").val("");
$("#publishers_input").val("");
$("#comments_input").val("");

handleListServerResponse;
}
});
});

$(document).on('click', '#archive_selected_books', function(event) {
Expand Down

0 comments on commit a335dd7

Please sign in to comment.