From 321db4d712d919005bded9fc3c97892a2967db89 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 25 Mar 2023 12:34:16 +0100 Subject: [PATCH] Refactored send email by make use of ajax calls instead of posting the page Always use getPath instead of pathname --- cps/helper.py | 10 +- cps/static/js/caliBlur.js | 30 +- cps/static/js/details.js | 31 +- cps/static/js/logviewer.js | 2 +- cps/static/js/main.js | 22 +- cps/static/js/table.js | 2 +- cps/templates/detail.html | 660 ++++++++++++++++++++----------------- cps/web.py | 18 +- 8 files changed, 403 insertions(+), 372 deletions(-) mode change 100644 => 100755 cps/templates/detail.html mode change 100644 => 100755 cps/web.py diff --git a/cps/helper.py b/cps/helper.py index 85d71122..9871af0c 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -172,10 +172,6 @@ def check_send_to_ereader(entry): book_formats.append({'format': 'Epub', 'convert': 0, 'text': _('Send %(format)s to eReader', format='Epub')}) - if 'MOBI' in formats: - book_formats.append({'format': 'Mobi', - 'convert': 0, - 'text': _('Send %(format)s to eReader', format='Mobi')}) if 'PDF' in formats: book_formats.append({'format': 'Pdf', 'convert': 0, @@ -205,8 +201,8 @@ def check_read_formats(entry): # Files are processed in the following order/priority: -# 1: If Mobi file is existing, it's directly send to eReader email, -# 2: If Epub file is existing, it's converted and send to eReader email, +# 1: If epub file is existing, it's directly send to eReader email, +# 2: If mobi file is existing, it's converted and send to eReader email, # 3: If Pdf file is existing, it's directly send to eReader email def send_mail(book_id, book_format, convert, ereader_mail, calibrepath, user_id): """Send email with attachments""" @@ -214,7 +210,7 @@ def send_mail(book_id, book_format, convert, ereader_mail, calibrepath, user_id) if convert == 1: # returns None if success, otherwise errormessage - return convert_book_format(book_id, calibrepath, 'epub', book_format.lower(), user_id, ereader_mail) + return convert_book_format(book_id, calibrepath, 'mobi', book_format.lower(), user_id, ereader_mail) if convert == 2: # returns None if success, otherwise errormessage return convert_book_format(book_id, calibrepath, 'azw3', book_format.lower(), user_id, ereader_mail) diff --git a/cps/static/js/caliBlur.js b/cps/static/js/caliBlur.js index ec394d0b..62d34e40 100755 --- a/cps/static/js/caliBlur.js +++ b/cps/static/js/caliBlur.js @@ -314,9 +314,6 @@ $(document).mouseup(function (e) { }); }); -// Split path name to array and remove blanks -url = window.location.pathname - // Move create shelf $("#nav_createshelf").prependTo(".your-shelves"); @@ -360,31 +357,6 @@ $(document).on("click", ".dropdown-toggle", function () { }); }); -// Fade out content on page unload -// delegate all clicks on "a" tag (links) -/*$(document).on("click", "a:not(.btn-toolbar a, a[href*='shelf/remove'], .identifiers a, .bookinfo , .btn-group > a, #add-to-shelves a, #book-list a, .stat.blur a )", function () { - - // get the href attribute - var newUrl = $(this).attr("href"); - - // veryfy if the new url exists or is a hash - if (!newUrl || newUrl[0] === "#") { - // set that hash - location.hash = newUrl; - return; - } - - now, fadeout the html (whole page) - $( '.blur-wrapper' ).fadeOut(250); - $(".row-fluid .col-sm-10").fadeOut(500,function () { - // when the animation is complete, set the new location - location = newUrl; - }); - - // prevent the default browser behavior. - return false; -});*/ - // Collapse long text into read-more $("div.comments").readmore({ collapsedHeight: 134, @@ -447,6 +419,8 @@ if ($("body.author").length > 0) { } } +// Split path name to array and remove blanks +url = window.location.pathname // Ereader Page - add class to iframe body on ereader page after it loads. backurl = "../../book/" + url[2] $("body.epub #title-controls") diff --git a/cps/static/js/details.js b/cps/static/js/details.js index f0259f8c..24b98437 100644 --- a/cps/static/js/details.js +++ b/cps/static/js/details.js @@ -1,5 +1,5 @@ /* This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web) - * Copyright (C) 2018 jkrehm + * Copyright (C) 2018-2023 jkrehm, OzzieIsaacs * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +17,35 @@ /* global _ */ +function handleResponse (data) { + $(".row-fluid.text-center").remove(); + $("#flash_danger").remove(); + $("#flash_success").remove(); + if (!jQuery.isEmptyObject(data)) { + if($("#bookDetailsModal").is(":visible")) { + data.forEach(function (item) { + $(".modal-header").after('
' + item.message + '
'); + }); + } else { + data.forEach(function (item) { + $(".navbar").after('
' + + '
' + item.message + '
' + + '
'); + }); + } + } +} +$(".sendbtn-form").click(function() { + $.ajax({ + method: 'post', + url: $(this).data('href'), + success: function (data) { + handleResponse(data) + } + }) +}); + $(function() { $("#have_read_form").ajaxForm(); }); diff --git a/cps/static/js/logviewer.js b/cps/static/js/logviewer.js index 7b93f30f..b9fbabf9 100644 --- a/cps/static/js/logviewer.js +++ b/cps/static/js/logviewer.js @@ -36,7 +36,7 @@ function init(logType) { d.innerHTML = "loading ..."; $.ajax({ - url: window.location.pathname + "/../../ajax/log/" + logType, + url: getPath() + "/../../ajax/log/" + logType, datatype: "text", cache: false }) diff --git a/cps/static/js/main.js b/cps/static/js/main.js index de656bd6..efe4e7c3 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -85,14 +85,6 @@ $(document).on("change", "select[data-controlall]", function() { } }); -/*$(document).on("click", "#sendbtn", function (event) { - postButton(event, $(this).data('action')); -}); - -$(document).on("click", ".sendbutton", function (event) { - // $(".sendbutton").on("click", "body", function(event) { - postButton(event, $(this).data('action')); -});*/ $(document).on("click", ".postAction", function (event) { // $(".sendbutton").on("click", "body", function(event) { @@ -100,7 +92,6 @@ $(document).on("click", ".postAction", function (event) { }); - // Syntax has to be bind not on, otherwise problems with firefox $(".container-fluid").bind("dragenter dragover", function () { if($("#btn-upload").length && !$('body').hasClass('shelforder')) { @@ -321,7 +312,7 @@ $(function() { folder: folder, filter: filt }, - url: window.location.pathname + request_path, + url: getPath() + request_path, success: function success(data) { if ($("#element_selected").text() ==="") { $("#element_selected").text(data.cwd); @@ -434,7 +425,7 @@ $(function() { } $.ajax({ dataType: "json", - url: window.location.pathname + "/../../get_update_status", + url: getPath() + "/../../get_update_status", success: function success(data) { $this.html(buttonText); @@ -538,6 +529,7 @@ $(function() { $("#bookDetailsModal") .on("show.bs.modal", function(e) { $("#flash_danger").remove(); + $("#flash_success").remove(); var $modalBody = $(this).find(".modal-body"); // Prevent static assets from loading multiple times @@ -650,7 +642,6 @@ $(function() { ); }); - $("#user_submit").click(function() { this.closest("form").submit(); }); @@ -682,7 +673,7 @@ $(function() { $.ajax({ method:"post", dataType: "json", - url: window.location.pathname + "/../../ajax/simulatedbchange", + url: getPath() + "/../../ajax/simulatedbchange", data: {config_calibre_dir: $("#config_calibre_dir").val(), csrf_token: $("input[name='csrf_token']").val()}, success: function success(data) { if ( data.change ) { @@ -713,13 +704,13 @@ $(function() { var loader = "/../.."; $("#flash_success").remove(); $("#flash_danger").remove(); - $.post(window.location.pathname + request_path, $(this).closest("form").serialize(), function(data) { + $.post(getPath() + request_path, $(this).closest("form").serialize(), function(data) { $('#config_upload_formats').val(data.config_upload); if(data.reboot) { $("#spinning_success").show(); var rebootInterval = setInterval(function(){ $.get({ - url:window.location.pathname + "/../../admin/alive", + url:getPath() + "/../../admin/alive", success: function (d, statusText, xhr) { if (xhr.status < 400) { $("#spinning_success").hide(); @@ -745,7 +736,6 @@ $(function() { $(this).data('value'), function(value){ postButton(event, $("#delete_shelf").data("action")); - // $("#delete_shelf").closest("form").submit() } ); diff --git a/cps/static/js/table.js b/cps/static/js/table.js index 833f1a13..2d62e848 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -49,7 +49,7 @@ $(function() { method: "post", contentType: "application/json; charset=utf-8", dataType: "json", - url: window.location.pathname + "/../ajax/canceltask", + url: getPath() + "/../ajax/canceltask", data: JSON.stringify({"task_id": taskId}), }); }); diff --git a/cps/templates/detail.html b/cps/templates/detail.html old mode 100644 new mode 100755 index c9cb8143..1d27d122 --- a/cps/templates/detail.html +++ b/cps/templates/detail.html @@ -1,326 +1,370 @@ {% extends is_xhr|yesno("fragment.html", "layout.html") %} {% block body %}
-
-
-
- - -
-
-
- {% endblock %} {% block js %} + + {% endblock %} + diff --git a/cps/web.py b/cps/web.py old mode 100644 new mode 100755 index 528e1db4..7c3a1477 --- a/cps/web.py +++ b/cps/web.py @@ -25,7 +25,7 @@ import chardet # dependency of requests import copy from flask import Blueprint, jsonify -from flask import request, redirect, send_from_directory, make_response, flash, abort, url_for +from flask import request, redirect, send_from_directory, make_response, flash, abort, url_for, Response from flask import session as flask_session from flask_babel import gettext as _ from flask_babel import get_locale @@ -1214,22 +1214,20 @@ def download_link(book_id, book_format, anyname): @download_required def send_to_ereader(book_id, book_format, convert): if not config.get_mail_server_configured(): - flash(_("Please configure the SMTP mail settings first."), category="error") + response = [{'type': "danger", 'message': _("Please configure the SMTP mail settings first...")}] + return Response(json.dumps(response), mimetype='application/json') elif current_user.kindle_mail: result = send_mail(book_id, book_format, convert, current_user.kindle_mail, config.config_calibre_dir, current_user.name) if result is None: - flash(_("Success! Book queued for sending to %(eReadermail)s", eReadermail=current_user.kindle_mail), - category="success") ub.update_download(book_id, int(current_user.id)) + response = [{'type': "success", 'message': _("Success! Book queued for sending to %(eReadermail)s", + eReadermail=current_user.kindle_mail)}] else: - flash(_("Oops! There was an error sending book: %(res)s", res=result), category="error") + response = [{'type': "danger", 'message': _("Oops! There was an error sending book: %(res)s", res=result)}] else: - flash(_("Oops! Please update your profile with a valid eReader Email."), category="error") - if "HTTP_REFERER" in request.environ: - return redirect(request.environ["HTTP_REFERER"]) - else: - return redirect(url_for('web.index')) + response = [{'type': "danger", 'message': _("Oops! Please update your profile with a valid eReader Email.")}] + return Response(json.dumps(response), mimetype='application/json') # ################################### Login Logout ##################################################################