Skip to content

Commit

Permalink
Merge pull request #1 from ofostier/janeczku#3074-rating-without-editing
Browse files Browse the repository at this point in the history
janeczku#3074 rating without editing
  • Loading branch information
ofostier authored Jul 15, 2024
2 parents 1fe8bd3 + e42ce11 commit 2257ed8
Show file tree
Hide file tree
Showing 10 changed files with 610 additions and 417 deletions.
Binary file added .DS_Store
Binary file not shown.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ gdrive_credentials
client_secrets.json
gmail.json
/.key
library
.env
.github
library
library/metadata.db
3 changes: 3 additions & 0 deletions cps/editbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,12 @@ def edit_book_ratings(to_save, book):
if rating_x2 != old_rating:
changed = True
is_rating = calibre_db.session.query(db.Ratings).filter(db.Ratings.rating == rating_x2).first()

if is_rating:
print("Is rating: " + str(is_rating))
book.ratings.append(is_rating)
else:
print("New Rating")
new_rating = db.Ratings(rating=rating_x2)
book.ratings.append(new_rating)
if old_rating:
Expand Down
25 changes: 25 additions & 0 deletions cps/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,31 @@ def get_sorted_author(value):
value2 = value
return value2

def edit_book_rating(book_id, rating=0):

rating_x2 = int(float(rating) * 2)
book = calibre_db.get_filtered_book(book_id)
is_rating = calibre_db.session.query(db.Ratings).filter(db.Ratings.rating == rating_x2).first()

if not book.rating[0]:
old_rating = 0
else:
old_rating = book.ratings[0].rating

if rating_x2 != old_rating:
is_rating = calibre_db.session.query(db.Ratings).filter(db.Ratings.rating == rating_x2).first()

if is_rating:
book.ratings.append(is_rating)
else:
new_rating = db.Ratings(rating=rating_x2)
book.ratings.append(new_rating)
if old_rating:
book.ratings.remove(book.ratings[0])

calibre_db.session.commit()

return ""

def edit_book_read_status(book_id, read_status=None):
if not config.config_read_column:
Expand Down
4 changes: 4 additions & 0 deletions cps/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,7 @@ div.log {
.error-list {
margin-top: 5px;
}

.rating-input {
padding: 0;
}
244 changes: 168 additions & 76 deletions cps/static/js/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,126 +17,218 @@

/* global _ */

function handleResponse (data) {
function handleResponse(data) {
$(".row-fluid.text-center").remove();
$("#flash_danger").remove();
$("#flash_success").remove();
if (!jQuery.isEmptyObject(data)) {
if($("#bookDetailsModal").is(":visible")) {
if ($("#bookDetailsModal").is(":visible")) {
data.forEach(function (item) {
$(".modal-header").after('<div id="flash_' + item.type +
'" class="text-center alert alert-' + item.type + '">' + item.message + '</div>');
$(".modal-header").after(
'<div id="flash_' +
item.type +
'" class="text-center alert alert-' +
item.type +
'">' +
item.message +
"</div>"
);
});
} else {
data.forEach(function (item) {
$(".navbar").after('<div class="row-fluid text-center">' +
'<div id="flash_' + item.type + '" class="alert alert-' + item.type + '">' + item.message + '</div>' +
'</div>');
$(".navbar").after(
'<div class="row-fluid text-center">' +
'<div id="flash_' +
item.type +
'" class="alert alert-' +
item.type +
'">' +
item.message +
"</div>" +
"</div>"
);
});
}
}
}
$(".sendbtn-form").click(function() {
$(".sendbtn-form").click(function () {
$.ajax({
method: 'post',
url: $(this).data('href'),
data: {csrf_token: $("input[name='csrf_token']").val()},
method: "post",
url: $(this).data("href"),
data: { csrf_token: $("input[name='csrf_token']").val() },
success: function (data) {
handleResponse(data)
}
})
handleResponse(data);
},
});
});

$(function () {
$("#rating_form").ajaxForm();
});

$(function() {
$("#rating").on("change", function () {
//alert("Rating change !" + $(this).closest("form").serialize());
$.ajax({
url: this.closest("form").action,
method: "post",
data: $(this).closest("form").serialize(),
error: function (response) {
var data = [{ type: "danger", message: response.responseText }];
// $("#flash_success").parent().remove();
$("#flash_danger").remove();
$(".row-fluid.text-center").remove();
if (!jQuery.isEmptyObject(data)) {
$("#have_read_cb").prop(
"checked",
!$("#have_read_cb").prop("checked")
);
if ($("#bookDetailsModal").is(":visible")) {
data.forEach(function (item) {
$(".modal-header").after(
'<div id="flash_' +
item.type +
'" class="text-center alert alert-' +
item.type +
'">' +
item.message +
"</div>"
);
});
} else {
data.forEach(function (item) {
$(".navbar").after(
'<div class="row-fluid text-center" >' +
'<div id="flash_' +
item.type +
'" class="alert alert-' +
item.type +
'">' +
item.message +
"</div>" +
"</div>"
);
});
}
}
},
});
});

$(function () {
$("#have_read_form").ajaxForm();
});

$("#have_read_cb").on("change", function() {
$("#have_read_cb").on("change", function () {
$.ajax({
url: this.closest("form").action,
method:"post",
method: "post",
data: $(this).closest("form").serialize(),
error: function(response) {
var data = [{type:"danger", message:response.responseText}]
error: function (response) {
var data = [{ type: "danger", message: response.responseText }];
// $("#flash_success").parent().remove();
$("#flash_danger").remove();
$(".row-fluid.text-center").remove();
if (!jQuery.isEmptyObject(data)) {
$("#have_read_cb").prop("checked", !$("#have_read_cb").prop("checked"));
if($("#bookDetailsModal").is(":visible")) {
$("#have_read_cb").prop(
"checked",
!$("#have_read_cb").prop("checked")
);
if ($("#bookDetailsModal").is(":visible")) {
data.forEach(function (item) {
$(".modal-header").after('<div id="flash_' + item.type +
'" class="text-center alert alert-' + item.type + '">' + item.message + '</div>');
$(".modal-header").after(
'<div id="flash_' +
item.type +
'" class="text-center alert alert-' +
item.type +
'">' +
item.message +
"</div>"
);
});
} else
{
} else {
data.forEach(function (item) {
$(".navbar").after('<div class="row-fluid text-center" >' +
'<div id="flash_' + item.type + '" class="alert alert-' + item.type + '">' + item.message + '</div>' +
'</div>');
$(".navbar").after(
'<div class="row-fluid text-center" >' +
'<div id="flash_' +
item.type +
'" class="alert alert-' +
item.type +
'">' +
item.message +
"</div>" +
"</div>"
);
});
}
}
}
},
});
});

$(function() {
$(function () {
$("#archived_form").ajaxForm();
});

$("#archived_cb").on("change", function() {
$("#archived_cb").on("change", function () {
$(this).closest("form").submit();
});

(function() {
(function () {
var templates = {
add: _.template(
$("#template-shelf-add").html()
),
remove: _.template(
$("#template-shelf-remove").html()
)
add: _.template($("#template-shelf-add").html()),
remove: _.template($("#template-shelf-remove").html()),
};

$("#add-to-shelves, #remove-from-shelves").on("click", "[data-shelf-action]", function (e) {
e.preventDefault();
$.ajax({
url: $(this).data('href'),
method:"post",
data: {csrf_token:$("input[name='csrf_token']").val()},
$("#add-to-shelves, #remove-from-shelves").on(
"click",
"[data-shelf-action]",
function (e) {
e.preventDefault();
$.ajax({
url: $(this).data("href"),
method: "post",
data: { csrf_token: $("input[name='csrf_token']").val() },
})
.done(function() {
var $this = $(this);
switch ($this.data("shelf-action")) {
case "add":
$("#remove-from-shelves").append(
templates.remove({
add: $this.data('href'),
remove: $this.data("remove-href"),
content: $("<div>").text(this.textContent).html()
})
);
break;
case "remove":
$("#add-to-shelves").append(
templates.add({
add: $this.data("add-href"),
remove: $this.data('href'),
content: $("<div>").text(this.textContent).html(),
})
);
break;
}
this.parentNode.removeChild(this);
}.bind(this))
.fail(function(xhr) {
var $msg = $("<span/>", { "class": "text-danger"}).text(xhr.responseText);
$("#shelf-action-status").html($msg);
.done(
function () {
var $this = $(this);
switch ($this.data("shelf-action")) {
case "add":
$("#remove-from-shelves").append(
templates.remove({
add: $this.data("href"),
remove: $this.data("remove-href"),
content: $("<div>")
.text(this.textContent)
.html(),
})
);
break;
case "remove":
$("#add-to-shelves").append(
templates.add({
add: $this.data("add-href"),
remove: $this.data("href"),
content: $("<div>")
.text(this.textContent)
.html(),
})
);
break;
}
this.parentNode.removeChild(this);
}.bind(this)
)
.fail(function (xhr) {
var $msg = $("<span/>", { class: "text-danger" }).text(
xhr.responseText
);
$("#shelf-action-status").html($msg);

setTimeout(function() {
$msg.remove();
}, 10000);
});
});
setTimeout(function () {
$msg.remove();
}, 10000);
});
}
);
})();
Loading

0 comments on commit 2257ed8

Please sign in to comment.