Fix review voting JS not showing removals

Fixes #474
This commit is contained in:
rubenwardy 2023-10-31 23:28:20 +00:00
parent 3279e00aa4
commit d4b1344f6a
7 changed files with 42 additions and 21 deletions

@ -16,7 +16,8 @@
from collections import namedtuple
from flask import render_template, request, redirect, flash, url_for, abort
import typing
from flask import render_template, request, redirect, flash, url_for, abort, jsonify
from flask_babel import gettext, lazy_gettext
from flask_login import current_user, login_required
from flask_wtf import FlaskForm
@ -27,7 +28,7 @@ from app.models import db, PackageReview, Thread, ThreadReply, NotificationType,
Permission, AuditSeverity, PackageState
from app.tasks.webhooktasks import post_discord_webhook
from app.utils import is_package_page, add_notification, get_int_or_abort, is_yes, is_safe_url, rank_required, \
add_audit_log, has_blocked_domains
add_audit_log, has_blocked_domains, should_return_json
from . import bp
@ -178,18 +179,16 @@ def delete_review(package, reviewer):
return redirect(thread.get_view_url())
def handle_review_vote(package: Package, review_id: int):
def handle_review_vote(package: Package, review_id: int) -> typing.Optional[str]:
if current_user in package.maintainers:
flash(gettext("You can't vote on the reviews on your own package!"), "danger")
return
return gettext("You can't vote on the reviews on your own package!")
review: PackageReview = PackageReview.query.get(review_id)
if review is None or review.package != package:
abort(404)
if review.author == current_user:
flash(gettext("You can't vote on your own reviews!"), "danger")
return
return gettext("You can't vote on your own reviews!")
is_positive = is_yes(request.form["is_positive"])
@ -213,7 +212,15 @@ def handle_review_vote(package: Package, review_id: int):
@login_required
@is_package_page
def review_vote(package, review_id):
handle_review_vote(package, review_id)
msg = handle_review_vote(package, review_id)
if should_return_json():
if msg:
return jsonify({"success": False, "error": msg}), 403
else:
return jsonify({"success": True})
if msg:
flash(msg, "danger")
next_url = request.args.get("r")
if next_url and is_safe_url(next_url):

@ -41,11 +41,24 @@ async function submitForm(form, is_helpful) {
body: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
},
});
if (!res.ok) {
console.error(await res.text());
const json = await res.json();
alert(json.error ?? "Unknown server error");
}
}
function setButtonSelected(ele, isSelected) {
if (isSelected) {
ele.classList.add("btn-primary");
ele.classList.remove("btn-secondary");
} else {
ele.classList.add("btn-secondary");
ele.classList.remove("btn-primary");
}
}
@ -61,15 +74,16 @@ window.addEventListener("load", () => {
if (not_selected.classList.contains("btn-primary")) {
setVoteCount(not_selected, Math.max(getVoteCount(not_selected) - 1, 0));
}
if (selected.classList.contains("btn-secondary")) {
setVoteCount(selected, getVoteCount(selected) + 1);
setButtonSelected(not_selected, false);
}
selected.classList.add("btn-primary");
selected.classList.remove("btn-secondary");
not_selected.classList.add("btn-secondary");
not_selected.classList.remove("btn-primary");
if (selected.classList.contains("btn-secondary")) {
setVoteCount(selected, getVoteCount(selected) + 1);
setButtonSelected(selected, true);
} else if (selected.classList.contains("btn-primary")) {
setVoteCount(selected, Math.max(getVoteCount(selected) - 1, 0));
setButtonSelected(selected, false);
}
submitForm(helpful_form, is_helpful).catch(console.error);
}

@ -22,7 +22,7 @@
}
</script>
{% if current_user.is_authenticated %}
<script src="/static/js/quick_review_voting.js"></script>
<script src="/static/js/quick_review_voting.js?v=2"></script>
{% endif %}
{% endblock %}

@ -6,7 +6,7 @@
{% block scriptextra %}
{% if current_user.is_authenticated %}
<script src="/static/js/quick_review_voting.js"></script>
<script src="/static/js/quick_review_voting.js?v=2"></script>
{% endif %}
{% endblock %}

@ -21,7 +21,7 @@
<script src="/static/js/video_embed.js"></script>
<script src="/static/js/gallery_carousel.js"></script>
{% if current_user.is_authenticated %}
<script src="/static/js/quick_review_voting.js"></script>
<script src="/static/js/quick_review_voting.js?v=2"></script>
{% endif %}
{% endblock %}

@ -27,7 +27,7 @@
{% block scriptextra %}
{% if current_user.is_authenticated %}
<script src="/static/js/quick_review_voting.js"></script>
<script src="/static/js/quick_review_voting.js?v=2"></script>
{% endif %}
{% endblock %}

@ -10,7 +10,7 @@
{% block scriptextra %}
{% if current_user.is_authenticated %}
<script src="/static/js/quick_review_voting.js"></script>
<script src="/static/js/quick_review_voting.js?v=2"></script>
{% endif %}
{% endblock %}