This commit is contained in:
Ross
2022-06-07 22:37:11 +01:00
parent 70b7c4befe
commit 3a542231f0
3 changed files with 225 additions and 176 deletions
+3 -1
View File
@@ -35,7 +35,9 @@
{% endfor %} {% endfor %}
</div> </div>
{% if can_edit %} {% if can_edit %}
<p><button id='button-edit-order' title='click and drag questions to change order' data-posturl="{% url 'anatomy:exam_json_edit' pk=exam.pk %}">Edit question order / Delete questions</button></p> <p>
<button id='button-edit-order' title='click and drag questions to change order' data-posturl="{% url 'anatomy:exam_json_edit' pk=exam.pk %}">Edit question order / Delete questions</button>
</p>
{% endif %} {% endif %}
<div> <div>
JSON creation time: {{exam.json_creation_time}} ({{exam.json_creation_time|date:"c"}}), JSON creation time: {{exam.json_creation_time}} ({{exam.json_creation_time|date:"c"}}),
+53 -14
View File
@@ -3,6 +3,7 @@ import django_filters
from .models import Rapid, CidUserAnswer, Exam from .models import Rapid, CidUserAnswer, Exam
from django.contrib.auth.models import User from django.contrib.auth.models import User
def get_exams(request): def get_exams(request):
if request is None: if request is None:
return Exam.objects.none() return Exam.objects.none()
@@ -14,6 +15,7 @@ def get_exams(request):
return queryset return queryset
def get_authors(request): def get_authors(request):
if request is None: if request is None:
return User.objects.none() return User.objects.none()
@@ -25,22 +27,47 @@ def get_authors(request):
return queryset return queryset
class RapidFilter(django_filters.FilterSet): class RapidFilter(django_filters.FilterSet):
exams = django_filters.ModelMultipleChoiceFilter(queryset=get_exams, null_label="No exam") exams = django_filters.ModelMultipleChoiceFilter(
author = django_filters.ModelMultipleChoiceFilter(queryset=get_authors, null_label="No author") queryset=get_exams, null_label="No exam"
)
author = django_filters.ModelMultipleChoiceFilter(
queryset=get_authors, null_label="No author"
)
class Meta: class Meta:
model = Rapid model = Rapid
fields = ("normal", "abnormality", "region", "examination", fields = {
"laterality", "normal": ["exact"],
#"site", "abnormality": ["exact"],
"created_date", "open_access") "region": ["exact"],
"examination": ["exact"],
"laterality": ["exact"],
# "site",
"created_date": ["exact"],
"open_access": ["exact"],
}
def __init__(self, data=None, queryset=None, prefix=None, strict=None, user=None, request=None): def __init__(
self,
data=None,
queryset=None,
prefix=None,
strict=None,
user=None,
request=None,
):
if not request.user.groups.filter(name="rapid_checker").exists(): if not request.user.groups.filter(name="rapid_checker").exists():
queryset = queryset.filter(open_access=True) | queryset.filter(author__id=request.user.id) queryset = queryset.filter(open_access=True) | queryset.filter(
super(RapidFilter, self).__init__(data=data, queryset=queryset, prefix=prefix, request=request) author__id=request.user.id
)
super(RapidFilter, self).__init__(
data=data, queryset=queryset, prefix=prefix, request=request
)
pass pass
class RapidUserAnswerFilter(django_filters.FilterSet): class RapidUserAnswerFilter(django_filters.FilterSet):
class Meta: class Meta:
model = CidUserAnswer model = CidUserAnswer
@@ -49,14 +76,26 @@ class RapidUserAnswerFilter(django_filters.FilterSet):
"user", "user",
"normal", "normal",
"answer", "answer",
#"answer_compare", # "answer_compare",
"exam", "exam",
"created", "created",
"updated", "updated",
) )
def __init__(self, data=None, queryset=None, prefix=None, strict=None, user=None, request=None): def __init__(
self,
data=None,
queryset=None,
prefix=None,
strict=None,
user=None,
request=None,
):
if not request.user.groups.filter(name="rapid_checker").exists(): if not request.user.groups.filter(name="rapid_checker").exists():
queryset = queryset.filter(open_access=True) | queryset.filter(author__id=request.user.id) queryset = queryset.filter(open_access=True) | queryset.filter(
super(RapidUserAnswerFilter, self).__init__(data=data, queryset=queryset, prefix=prefix, request=request) author__id=request.user.id
pass )
super(RapidUserAnswerFilter, self).__init__(
data=data, queryset=queryset, prefix=prefix, request=request
)
pass
+169 -161
View File
@@ -1,162 +1,170 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function () { $(document).ready(function () {
// send request to change the is_private state on customSwitches toggle // send request to change the is_private state on customSwitches toggle
$("#exam-active-switch, .exam-active-switch").on("change", function (evt) { $("#exam-active-switch, .exam-active-switch").on("change", function (evt) {
$.ajax({ $.ajax({
url: evt.currentTarget.dataset.posturl, url: evt.currentTarget.dataset.posturl,
data: { data: {
csrfmiddlewaretoken: "{{ csrf_token }}", csrfmiddlewaretoken: "{{ csrf_token }}",
active: this.checked // true if checked else false active: this.checked // true if checked else false
}, },
type: "POST", type: "POST",
dataType: "json", dataType: "json",
}) })
// $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
.done(function (data) { .done(function (data) {
console.log(data); console.log(data);
if (data.status == "success") { if (data.status == "success") {
toastr.info(`Exam: ${data.name} [${data.id}] toastr.info(`Exam: ${data.name} [${data.id}]
Active state changed to: ${data.active}.`) Active state changed to: ${data.active}.`)
} }
// show some message according to the response. // show some message according to the response.
// For eg. A message box showing that the status has been changed // For eg. A message box showing that the status has been changed
}) })
.fail(function (data) { .fail(function (data) {
console.log(data) console.log(data)
toastr.error(`Failed to change state<br/>${data.responseJSON.status}`); toastr.error(`Failed to change state<br/>${data.responseJSON.status}`);
}) })
.always(function () { .always(function () {
console.log('[Done]'); console.log('[Done]');
}) })
}) })
$("#exam-publish-results-switch, .exam-publish-results-switch").on("change", function (evt) { $("#exam-publish-results-switch, .exam-publish-results-switch").on("change", function (evt) {
$.ajax({ $.ajax({
url: evt.currentTarget.dataset.posturl, url: evt.currentTarget.dataset.posturl,
data: { data: {
csrfmiddlewaretoken: "{{ csrf_token }}", csrfmiddlewaretoken: "{{ csrf_token }}",
publish_results: this.checked // true if checked else false publish_results: this.checked // true if checked else false
}, },
type: "POST", type: "POST",
dataType: "json", dataType: "json",
}) })
// $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
.done(function (data) { .done(function (data) {
console.log(data); console.log(data);
if (data.status == "success") { if (data.status == "success") {
toastr.info(`Exam: ${data.name} [${data.id}] toastr.info(`Exam: ${data.name} [${data.id}]
Published state changed to: ${data.publish_results}.`) Published state changed to: ${data.publish_results}.`)
} }
// show some message according to the response. // show some message according to the response.
// For eg. A message box showing that the status has been changed // For eg. A message box showing that the status has been changed
}) })
.fail(function (data) { .fail(function (data) {
toastr.error(`Failed to change state`); toastr.error(`Failed to change state`);
}) })
.always(function () { .always(function () {
console.log('[Done]'); console.log('[Done]');
}) })
}) })
$("#button-open-access").click(function (evt) { $("#button-open-access").click(function (evt) {
$.ajax({ $.ajax({
url: evt.currentTarget.dataset.posturl, url: evt.currentTarget.dataset.posturl,
data: { data: {
csrfmiddlewaretoken: "{{ csrf_token }}", csrfmiddlewaretoken: "{{ csrf_token }}",
set_open_access: true, set_open_access: true,
}, },
type: "POST", type: "POST",
dataType: "json", dataType: "json",
}) })
// $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
.done(function (data) { .done(function (data) {
console.log(data); console.log(data);
if (data.status == "success") { if (data.status == "success") {
toastr.info('Question access state changed.'); toastr.info('Question access state changed.');
} else { } else {
toastr.warning("Error"); toastr.warning("Error");
toastr.info(data.status); toastr.info(data.status);
} }
}) })
.fail((e) => { .fail((e) => {
toastr.warning(`Error, ${e}`); toastr.warning(`Error, ${e}`);
}) })
.always(function () { .always(function () {
console.log('[Done]'); console.log('[Done]');
}) })
}) })
$("#button-closed-access").click(function (evt) { $("#button-closed-access").click(function (evt) {
$.ajax({ $.ajax({
url: evt.currentTarget.dataset.posturl, url: evt.currentTarget.dataset.posturl,
data: { data: {
csrfmiddlewaretoken: "{{ csrf_token }}", csrfmiddlewaretoken: "{{ csrf_token }}",
set_open_access: false, set_open_access: false,
}, },
type: "POST", type: "POST",
dataType: "json", dataType: "json",
}) })
// $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
.done(function (data) { .done(function (data) {
console.log(data); console.log(data);
if (data.status == "success") { if (data.status == "success") {
toastr.info('Question access state changed.'); toastr.info('Question access state changed.');
} else { } else {
toastr.warning("Error"); toastr.warning("Error");
toastr.info(data.status); toastr.info(data.status);
} }
}) })
.fail((e) => { .fail((e) => {
toastr.warning(`Error, ${e}`); toastr.warning(`Error, ${e}`);
}) })
.always(function () { .always(function () {
console.log('[Done]'); console.log('[Done]');
}) })
}) })
$("#button-edit-order").click(function (evt) { $("#button-edit-order").click(function (evt) {
$(this).remove(); $(this).remove();
sortable('.sortable'); sortable('.sortable');
$("#full-question-list li").each((n, el) => { $("#full-question-list").after($("<button id='random-order-button'>Randomise order</button>").click(() =>{
$(el).append($( var ul = document.getElementById("full-question-list");
"<span class='exam-question-delete flex-col'><button>DELETE</button></span>" for (var i = ul.children.length; i >= 0; i--) {
).click(() => { ul.appendChild(ul.children[Math.random() * i | 0]);
el.remove(); }
})); }))
})
$("#full-question-list li").each((n, el) => {
$("#full-question-list").append($( $(el).append($(
"<button title='click and drag questions to change order'>Save exam order</button>" "<span class='exam-question-delete flex-col'><button>DELETE</button></span>"
).click(() => { ).click(() => {
new_order = []; el.remove();
$("#full-question-list li").each((n, el) => { }));
new_order.push(el.dataset.question_pk) })
})
$.ajax({
url: evt.currentTarget.dataset.posturl, $("#full-question-list").after($(
data: { "<button title='click and drag questions to change order'>Save exam order</button>"
csrfmiddlewaretoken: "{{ csrf_token }}", ).click(() => {
set_exam_order: JSON.stringify(new_order), new_order = [];
}, $("#full-question-list li").each((n, el) => {
type: "POST", new_order.push(el.dataset.question_pk)
dataType: "json", })
}) $.ajax({
// $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly url: evt.currentTarget.dataset.posturl,
.done(function (data) { data: {
console.log(data); csrfmiddlewaretoken: "{{ csrf_token }}",
set_exam_order: JSON.stringify(new_order),
if (data.status == "success") { },
toastr.info('Exam order changed.') type: "POST",
} dataType: "json",
}) })
.always(function () { // $.ajax().done(), $.ajax().fail(), $ajax().always() are upto you. Add/change accordingly
console.log('[Done]'); .done(function (data) {
}) console.log(data);
}));
}); if (data.status == "success") {
toastr.info('Exam order changed.')
}
}); })
.always(function () {
console.log('[Done]');
})
}));
});
});
</script> </script>