Improvements for delete book

pull/1539/head
Ozzieisaacs 5 years ago
parent 8dde6ba60f
commit 22344a3971

@ -170,21 +170,43 @@ def modify_identifiers(input_identifiers, db_identifiers, db_session):
changed = True changed = True
return changed return changed
@editbook.route("/ajax/delete/<int:book_id>")
@login_required
def delete_book_from_details(book_id):
return delete_book(book_id,"", True)
@editbook.route("/delete/<int:book_id>/", defaults={'book_format': ""}) @editbook.route("/delete/<int:book_id>", defaults={'book_format': ""})
@editbook.route("/delete/<int:book_id>/<string:book_format>/") @editbook.route("/delete/<int:book_id>/<string:book_format>")
@login_required @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(): if current_user.role_delete_books():
book = calibre_db.get_book(book_id) book = calibre_db.get_book(book_id)
if book: if book:
try: try:
result, error = helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper()) result, error = helper.delete_book(book, config.config_calibre_dir, book_format=book_format.upper())
if not result: if not result:
flash(error, category="error") if jsonResponse:
return redirect(url_for('editbook.edit_book', book_id=book_id)) 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: 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: if not book_format:
# delete book from Shelfs, Downloads, Read list # delete book from Shelfs, Downloads, Read list
ub.session.query(ub.BookShelf).filter(ub.BookShelf.book_id == book_id).delete() 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 # book not found
log.error('Book with id "%s" could not be deleted: not found', book_id) log.error('Book with id "%s" could not be deleted: not found', book_id)
if book_format: if book_format:
flash(_('Book Format Successfully Deleted'), category="success") if jsonResponse:
return redirect(url_for('editbook.edit_book', book_id=book_id)) 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: else:
flash(_('Book Successfully Deleted'), category="success") if jsonResponse:
return redirect(url_for('web.index')) 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): 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 json.dumps({'author_sort': book.author_sort})
return "" return ""
@editbook.route("/ajax/deletebooks")
@login_required
def delete_list_book():
return ""
@editbook.route("/ajax/mergebooks", methods=['POST']) @editbook.route("/ajax/mergebooks", methods=['POST'])
@login_required @login_required

@ -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( '<div class="row-fluid text-center" style="margin-top: -20px;">' +
'<div id="flash_'+item.type+'" class="alert alert-'+item.type+'">'+item.message+'</div>' +
'</div>');
}
});
}
});
} 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() { $(function() {
var updateTimerID; var updateTimerID;

@ -128,28 +128,14 @@ $(function() {
onColumnSwitch: function (field, checked) { onColumnSwitch: function (field, checked) {
var visible = $("#books-table").bootstrapTable('getVisibleColumns'); var visible = $("#books-table").bootstrapTable('getVisibleColumns');
var hidden = $("#books-table").bootstrapTable('getHiddenColumns'); var hidden = $("#books-table").bootstrapTable('getHiddenColumns');
// to save current setting
// coresponding event: onColumnSwitch
//$table.bootstrapTable('getVisibleColumns')
//$table.bootstrapTable('getHiddenColumns').
var visibility =[] var visibility =[]
var st = "" var st = ""
visible.forEach(function(item) { visible.forEach(function(item) {
st += "\""+ item.field + "\":\"" +"true"+ "\"," st += "\""+ item.field + "\":\"" +"true"+ "\","
/*var element = {};
element[item.field] = "true";
visibility.push(element);*/
}); });
hidden.forEach(function(item) { hidden.forEach(function(item) {
st += "\""+ item.field + "\":\"" +"false"+ "\"," 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); st = st.slice(0, -1);
$.ajax({ $.ajax({
method:"post", method:"post",
@ -238,29 +224,6 @@ $(function() {
$(e.currentTarget).find("#btndeletedomain").data("domainId", domainId); $(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 () {
<div class="row-fluid text-center" style="margin-top: -20px;">
<div id="flash_success" class="alert alert-success">{{ message[1] }}</div>
</div>*/
$("#restrictModal").on("hidden.bs.modal", function () { $("#restrictModal").on("hidden.bs.modal", function () {
// Destroy table and remove hooks for buttons // Destroy table and remove hooks for buttons
$("#restrict-elements-table").unbind(); $("#restrict-elements-table").unbind();
@ -385,7 +348,7 @@ function RestrictionActions (value, row) {
/* Function for deleting books */ /* Function for deleting books */
function EbookActions (value, row) { function EbookActions (value, row) {
return [ return [
"<div class=\"book-remove\" data-toggle=\"modal\" data-target=\"#deleteModal\" data-delete-id=\"" + row.id + "\" title=\"Remove\">", "<div class=\"book-remove\" data-toggle=\"modal\" data-target=\"#deleteModal\" data-ajax=\"1\" data-delete-id=\"" + row.id + "\" title=\"Remove\">",
"<i class=\"glyphicon glyphicon-trash\"></i>", "<i class=\"glyphicon glyphicon-trash\"></i>",
"</div>" "</div>"
].join(""); ].join("");

@ -7,14 +7,12 @@
</div> </div>
{% if g.user.role_delete_books() %} {% if g.user.role_delete_books() %}
<div class="text-center"> <div class="text-center">
<button type="button" class="btn btn-danger" id="delete" data-toggle="modal" data-target="#deleteModal">{{_("Delete Book")}}</button> <button type="button" class="btn btn-danger" id="delete" data-toggle="modal" data-delete-id="{{ book.id }}" data-target="#deleteModal">{{_("Delete Book")}}</button>
</div> </div>
{% if book.data|length > 1 %} {% if book.data|length > 1 %}
<div class="text-center more-stuff"><h4>{{_('Delete formats:')}}</h4> <div class="text-center more-stuff"><h4>{{_('Delete formats:')}}</h4>
{% for file in book.data %} {% for file in book.data %}
<div class="form-group"> <button type="button" class="btn btn-danger" id="delete_format" data-toggle="modal" data-delete-id="{{ book.id }}" data-delete-format="{{ file.format }}" data-target="#deleteModal">{{_('Delete')}} - {{file.format}}</button>
<a href="{{ url_for('editbook.delete_book', book_id=book.id, book_format=file.format) }}" class="btn btn-danger" type="button">{{_('Delete')}} - {{file.format}}</a>
</div>
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}

@ -51,7 +51,7 @@
{{ text_table_row('series', _('Enter Series'),_('Series'), false) }} {{ text_table_row('series', _('Enter Series'),_('Series'), false) }}
<th data-field="series_index" id="series_index" data-visible="{{visiblility.get('series_index')}}" data-edit-validate="{{ _('This Field is Required') }}" data-sortable="true" {% if g.user.role_edit() %} data-editable-type="number" data-editable-placeholder="1" data-editable-step="0.01" data-editable-min="0" data-editable-url="{{ url_for('editbook.edit_list_book', param='series_index')}}" data-edit="true" data-editable-title="{{_('Enter title')}}"{% endif %}>{{_('Series Index')}}</th> <th data-field="series_index" id="series_index" data-visible="{{visiblility.get('series_index')}}" data-edit-validate="{{ _('This Field is Required') }}" data-sortable="true" {% if g.user.role_edit() %} data-editable-type="number" data-editable-placeholder="1" data-editable-step="0.01" data-editable-min="0" data-editable-url="{{ url_for('editbook.edit_list_book', param='series_index')}}" data-edit="true" data-editable-title="{{_('Enter title')}}"{% endif %}>{{_('Series Index')}}</th>
{{ text_table_row('languages', _('Enter Languages'),_('Languages'), false) }} {{ text_table_row('languages', _('Enter Languages'),_('Languages'), false) }}
<th data-field="pubdate" data-type="date" data-visible="{{visiblility.get('pubdate')}}" data-viewformat="dd.mm.yyyy" id="pubdate" data-sortable="true">{{_('Publishing Date')}}</th> <!--th data-field="pubdate" data-type="date" data-visible="{{visiblility.get('pubdate')}}" data-viewformat="dd.mm.yyyy" id="pubdate" data-sortable="true">{{_('Publishing Date')}}</th-->
{{ text_table_row('publishers', _('Enter Publishers'),_('Publishers'), false) }} {{ text_table_row('publishers', _('Enter Publishers'),_('Publishers'), false) }}
{% if g.user.role_edit() %} {% if g.user.role_edit() %}
<th data-align="right" data-formatter="EbookActions" data-switchable="false">{{_('Delete')}}</th> <th data-align="right" data-formatter="EbookActions" data-switchable="false">{{_('Delete')}}</th>

@ -47,7 +47,8 @@
</div> </div>
<div class="modal-body text-center"> <div class="modal-body text-center">
<p> <p>
<span>{{_('This book will be permanently erased from database')}}</span> <span class="hidden" id="book_format">{{_('This book format will be permanently erased from database')}}</span>
<span class="hidden" id="book_complete">{{_('This book will be permanently erased from database')}}</span>
<span>{{_('and hard disk')}}</span> <span>{{_('and hard disk')}}</span>
</p> </p>
{% if config.config_kobo_sync %} {% if config.config_kobo_sync %}
@ -60,7 +61,6 @@
<div class="modal-footer"> <div class="modal-footer">
<input type="button" class="btn btn-danger" value="{{_('Delete')}}" name="delete_confirm" id="delete_confirm" data-dismiss="modal"> <input type="button" class="btn btn-danger" value="{{_('Delete')}}" name="delete_confirm" id="delete_confirm" data-dismiss="modal">
<!--a href="{{ url_for('editbook.delete_book', book_id=bookid) }}" id="delete_confirm" class="btn btn-danger">{{_('Delete')}}</a-->
<button type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button> <button type="button" class="btn btn-default" data-dismiss="modal">{{_('Cancel')}}</button>
</div> </div>
</div> </div>

Loading…
Cancel
Save