diff --git a/cps/editbooks.py b/cps/editbooks.py index d8dcca74..30cc918b 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -170,21 +170,43 @@ def modify_identifiers(input_identifiers, db_identifiers, db_session): changed = True return changed +@editbook.route("/ajax/delete/") +@login_required +def delete_book_from_details(book_id): + return delete_book(book_id,"", True) + -@editbook.route("/delete//", defaults={'book_format': ""}) -@editbook.route("/delete///") +@editbook.route("/delete/", defaults={'book_format': ""}) +@editbook.route("/delete//") @login_required -def delete_book(book_id, book_format): +def delete_book_ajax(book_id, book_format): + return delete_book(book_id,book_format, False) + +def delete_book(book_id, book_format, jsonResponse): + warning = {} if current_user.role_delete_books(): book = calibre_db.get_book(book_id) if book: try: result, error = helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper()) if not result: - flash(error, category="error") - return redirect(url_for('editbook.edit_book', book_id=book_id)) + if jsonResponse: + return Response(json.dumps({"location": url_for("editbook.edit_book"), + "type": "alert", + "format": "", + "error": error}), + mimetype='application/json') + else: + flash(error, category="error") + return redirect(url_for('editbook.edit_book', book_id=book_id)) if error: - flash(error, category="warning") + if jsonResponse: + warning = {"location": url_for("editbook.edit_book"), + "type": "warning", + "format": "", + "error": error} + else: + flash(error, category="warning") if not book_format: # delete book from Shelfs, Downloads, Read list ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete() @@ -240,11 +262,25 @@ def delete_book(book_id, book_format): # book not found log.error('Book with id "%s" could not be deleted: not found', book_id) if book_format: - flash(_('Book Format Successfully Deleted'), category="success") - return redirect(url_for('editbook.edit_book', book_id=book_id)) + if jsonResponse: + return Response(json.dumps([warning, {"location": url_for("editbook.edit_book", book_id=book_id), + "type": "success", + "format": book_format, + "message": _('Book Format Successfully Deleted')}]), + mimetype='application/json') + else: + flash(_('Book Format Successfully Deleted'), category="success") + return redirect(url_for('editbook.edit_book', book_id=book_id)) else: - flash(_('Book Successfully Deleted'), category="success") - return redirect(url_for('web.index')) + if jsonResponse: + return Response(json.dumps([warning, {"location": url_for('web.index'), + "type": "success", + "format": book_format, + "message": _('Book Successfully Deleted')}]), + mimetype='application/json') + else: + flash(_('Book Successfully Deleted'), category="success") + return redirect(url_for('web.index')) def render_edit_book(book_id): @@ -947,10 +983,7 @@ def get_sorted_entry(field, bookid): return json.dumps({'author_sort': book.author_sort}) return "" -@editbook.route("/ajax/deletebooks") -@login_required -def delete_list_book(): - return "" + @editbook.route("/ajax/mergebooks", methods=['POST']) @login_required diff --git a/cps/static/js/main.js b/cps/static/js/main.js index 6338be0b..c7eaec1b 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -58,6 +58,60 @@ $(document).on("change", "select[data-controlall]", function() { } }); +$("#delete_confirm").click(function() { + //get data-id attribute of the clicked element + var pathname = document.getElementsByTagName("script"), src = pathname[pathname.length - 1].src; + var path = src.substring(0, src.lastIndexOf("/")); + var deleteId = $(this).data("delete-id"); + var bookFormat = $(this).data("delete-format"); + if (bookFormat) { + window.location.href = path + "/../../delete/" + deleteId + "/" + bookFormat; + } else { + if ($(this).data("delete-format")) { + path = path + "/../../ajax/delete/" + deleteId; + $.ajax({ + method:"get", + url: path, + timeout: 900, + success:function(data) { + data.forEach(function(item) { + if (!jQuery.isEmptyObject(item)) { + if (item.format != "") { + $("button[data-delete-format='"+item.format+"']").addClass('hidden'); + } + $( ".navbar" ).after( '
' + + '
'+item.message+'
' + + '
'); + + } + }); + } + }); + } else { + window.location.href = path + "/../../delete/" + deleteId; + + } + } + +}); + +//triggered when modal is about to be shown +$("#deleteModal").on("show.bs.modal", function(e) { + //get data-id attribute of the clicked element and store in button + var bookId = $(e.relatedTarget).data("delete-id"); + var bookfomat = $(e.relatedTarget).data("delete-format"); + if (bookfomat) { + $("#book_format").removeClass('hidden'); + $("#book_complete").addClass('hidden'); + } else { + $("#book_complete").removeClass('hidden'); + $("#book_format").addClass('hidden'); + } + $(e.currentTarget).find("#delete_confirm").data("delete-id", bookId); + $(e.currentTarget).find("#delete_confirm").data("delete-format", bookfomat); +}); + + $(function() { var updateTimerID; diff --git a/cps/static/js/table.js b/cps/static/js/table.js index e310e29c..75d70d8d 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -128,28 +128,14 @@ $(function() { onColumnSwitch: function (field, checked) { var visible = $("#books-table").bootstrapTable('getVisibleColumns'); var hidden = $("#books-table").bootstrapTable('getHiddenColumns'); - // to save current setting - // coresponding event: onColumnSwitch - //$table.bootstrapTable('getVisibleColumns') - //$table.bootstrapTable('getHiddenColumns'). var visibility =[] var st = "" visible.forEach(function(item) { st += "\""+ item.field + "\":\"" +"true"+ "\"," - /*var element = {}; - element[item.field] = "true"; - visibility.push(element);*/ }); hidden.forEach(function(item) { st += "\""+ item.field + "\":\"" +"false"+ "\"," - /*var element = {}; - element[item.field] = "false"; - visibility.push(element);*/ }); - /* - visibility.forEach(function(item) { - st += JSON.stringify(item) + ','; - });*/ st = st.slice(0, -1); $.ajax({ method:"post", @@ -238,29 +224,6 @@ $(function() { $(e.currentTarget).find("#btndeletedomain").data("domainId", domainId); }); - $("#delete_confirm").click(function() { - //get data-id attribute of the clicked element - var deleteId = $(this).data("deleteid"); - $.ajax({ - method:"get", - url: window.location.pathname + "/../../delete/" + deleteId, - }); - }); - - //triggered when modal is about to be shown - $("#deleteModal").on("show.bs.modal", function(e) { - //get data-id attribute of the clicked element and store in button - var bookId = $(e.relatedTarget).data("delete-id"); - $(e.currentTarget).find("#delete_confirm").data("deleteid", bookId); - }); - // receive result from request, dismiss modal dialog, show flash message - // insert after navbar - /*$("#deleteModal").on("hidden.bs.modal", function () { -
-
{{ message[1] }}
-
*/ - - $("#restrictModal").on("hidden.bs.modal", function () { // Destroy table and remove hooks for buttons $("#restrict-elements-table").unbind(); @@ -385,7 +348,7 @@ function RestrictionActions (value, row) { /* Function for deleting books */ function EbookActions (value, row) { return [ - "
", + "
", "", "
" ].join(""); diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 1f7b57b4..3d1d629b 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -7,14 +7,12 @@
{% if g.user.role_delete_books() %}
- +
{% if book.data|length > 1 %}

{{_('Delete formats:')}}

{% for file in book.data %} - + {% endfor %}
{% endif %} diff --git a/cps/templates/book_table.html b/cps/templates/book_table.html index 1ea78e13..3c3f964b 100644 --- a/cps/templates/book_table.html +++ b/cps/templates/book_table.html @@ -51,7 +51,7 @@ {{ text_table_row('series', _('Enter Series'),_('Series'), false) }} {{_('Series Index')}} {{ text_table_row('languages', _('Enter Languages'),_('Languages'), false) }} - {{_('Publishing Date')}} + {{ text_table_row('publishers', _('Enter Publishers'),_('Publishers'), false) }} {% if g.user.role_edit() %} {{_('Delete')}} diff --git a/cps/templates/modal_dialogs.html b/cps/templates/modal_dialogs.html index 9d913b56..1432960f 100644 --- a/cps/templates/modal_dialogs.html +++ b/cps/templates/modal_dialogs.html @@ -47,7 +47,8 @@