Skip to content

Commit

Permalink
Replace all jQuery ajax requests except student vote
Browse files Browse the repository at this point in the history
  • Loading branch information
richardebeling committed Sep 18, 2023
1 parent 4e9cb1f commit 5b1cf1c
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 131 deletions.
15 changes: 8 additions & 7 deletions evap/grades/templates/grades_course_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ <h3 class="mb-3">{{ course.name }} ({{ semester.name }})</h3>
{% include 'confirmation_modal.html' with modal_id='deleteGradedocumentModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteGradedocumentModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'grades:delete_grades' %}",
data: {"grade_document_id": dataId},
success: function(){ $('#grade_document-row-'+dataId).hide('slow', function(){ $('#grade_document-row-'+dataId).remove(); }); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'grades:delete_grades' %}", {
body: new URLSearchParams({grade_document_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
$('#grade_document-row-'+dataId).hide('slow', function(){ $('#grade_document-row-'+dataId).remove(); });
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% endblock %}
15 changes: 8 additions & 7 deletions evap/grades/templates/grades_semester_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ <h3 class="col-8 mb-0">
{% include 'confirmation_modal.html' with modal_id='confirmNouploadModal' title=title question=question action_text=action_text btn_type='primary' %}
<script type="text/javascript">
function confirmNouploadModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'grades:toggle_no_grades' %}",
data: {"course_id": dataId},
success: function(){ location.reload(); },
error: function(){}
});
fetch("{% url 'grades:toggle_no_grades' %}", {
body: new URLSearchParams({course_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.reload();
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Will final grades be uploaded?' as title %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
{% include 'confirmation_modal.html' with modal_id='deleteEventModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteEventModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'rewards:reward_point_redemption_event_delete' %}",
data: {"event_id": dataId},
success: function(){ $('#event-row-'+dataId).hide('slow', function(){ $('#event-row-'+dataId).remove(); }); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'rewards:reward_point_redemption_event_delete' %}", {
body: new URLSearchParams({event_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
$('#event-row-'+dataId).hide('slow', function(){ $('#event-row-'+dataId).remove(); });
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% endblock %}
55 changes: 28 additions & 27 deletions evap/staff/templates/staff_questionnaire_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@
{% include 'confirmation_modal.html' with modal_id='deleteQuestionnaireModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteQuestionnaireModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:questionnaire_delete' %}",
data: {"questionnaire_id": dataId},
success: function(){ $('#questionnaire-row-'+dataId).hide('slow', function(){ $('#questionnaire-row-'+dataId).remove(); }); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:questionnaire_delete' %}", {
body: new URLSearchParams({questionnaire_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
$('#questionnaire-row-'+dataId).hide('slow', function(){ $('#questionnaire-row-'+dataId).remove(); });
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% endblock %}
Expand All @@ -82,31 +83,31 @@
function changeVisibility(element) {
const questionnaire = element.parents(".questionnaire");
const visibility = element.data("visibility");
$.ajax({
type: "POST",
url: "{% url 'staff:questionnaire_visibility' %}",
data: {"questionnaire_id": questionnaire.data("id"), "visibility": visibility},
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); },
success: function(){
questionnaire.find(`button[data-visibility]`).removeClass("active");
questionnaire.find(`button[data-visibility="${visibility}"]`).addClass("active");
}
});

fetch("{% url 'staff:questionnaire_visibility' %}", {
body: new URLSearchParams({questionnaire_id: questionnaire.data("id"), visibility}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
questionnaire.find(`button[data-visibility]`).removeClass("active");
questionnaire.find(`button[data-visibility="${visibility}"]`).addClass("active");
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
}

function changeLocked(element) {
const questionnaire = element.parents(".questionnaire");
const is_locked = element.data("is-locked");
$.ajax({
type: "POST",
url: "{% url 'staff:questionnaire_set_locked' %}",
data: {"questionnaire_id": questionnaire.data("id"), "is_locked": is_locked},
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); },
success: function(){
questionnaire.find(`button[data-is-locked]`).removeClass("active");
questionnaire.find(`button[data-is-locked="${is_locked}"]`).addClass("active");
}
});

fetch("{% url 'staff:questionnaire_set_locked' %}", {
body: new URLSearchParams({questionnaire_id: questionnaire.data("id"), is_locked}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
questionnaire.find(`button[data-is-locked]`).removeClass("active");
questionnaire.find(`button[data-is-locked="${is_locked}"]`).addClass("active");
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
}
</script>
{% endblock %}
17 changes: 7 additions & 10 deletions evap/staff/templates/staff_semester_preparation_reminder.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@
remindAllButton.prop('disabled', true);
remindAllButton.text('{% trans 'Reminding everyone' %}');

$.ajax({
type: "POST",
url: "{% url 'staff:semester_preparation_reminder' semester_id=semester.id %}",
success: function(){
location.reload();
},
error: function(){
window.alert("{% trans 'The server is not responding.' %}");
}
})
fetch("{% url 'staff:semester_preparation_reminder' semester_id=semester.id %}", {
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.reload();
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
}
</script>
{% endblock %}
106 changes: 57 additions & 49 deletions evap/staff/templates/staff_semester_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,14 @@ <h3>
var makeActiveSemesterButton = $('#makeActiveSemesterButton');
makeActiveSemesterButton.prop('disabled', true);
makeActiveSemesterButton.text("{% trans 'Activating...' %}");
$.ajax({
type: "POST",
url: "{% url 'staff:semester_make_active' %}",
data: {"semester_id": dataId},
success: function() { location.reload(); },
error: function() { window.alert("{% trans 'The server is not responding.' %}") },
})
fetch("{% url 'staff:semester_make_active' %}", {
body: new URLSearchParams({semester_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.reload();
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
}
</script>
{% trans 'Delete semester' as title %}
Expand All @@ -470,13 +471,15 @@ <h3>
var deleteButton = $('#deleteSemesterButton');
deleteButton.prop('disabled', true);
deleteButton.text("{% trans 'Deleting...' %}");
$.ajax({
type: "POST",
url: "{% url 'staff:semester_delete' %}",
data: {"semester_id": dataId},
success: function(){ location.replace("{% url 'staff:index' %}"); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});

fetch("{% url 'staff:semester_delete' %}", {
body: new URLSearchParams({semester_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.replace("{% url 'staff:index' %}");
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Archive participations' as title %}
Expand All @@ -485,13 +488,14 @@ <h3>
{% include 'confirmation_modal.html' with modal_id='archiveParticipationsModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function archiveParticipationsModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:semester_archive_participations' %}",
data: {"semester_id": dataId},
success: function(){ location.reload(); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:semester_archive_participations' %}", {
body: new URLSearchParams({semester_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.reload();
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Delete grade documents' as title %}
Expand All @@ -500,13 +504,14 @@ <h3>
{% include 'confirmation_modal.html' with modal_id='deleteGradeDocumentsModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteGradeDocumentsModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:semester_delete_grade_documents' %}",
data: {"semester_id": dataId},
success: function(){ location.reload(); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:semester_delete_grade_documents' %}", {
body: new URLSearchParams({semester_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.reload();
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Archive results' as title %}
Expand All @@ -515,13 +520,14 @@ <h3>
{% include 'confirmation_modal.html' with modal_id='archiveResultsModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function archiveResultsModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:semester_archive_results' %}",
data: {"semester_id": dataId},
success: function(){ location.reload(); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:semester_archive_results' %}", {
body: new URLSearchParams({semester_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
location.reload();
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Delete evaluation' as title %}
Expand All @@ -530,13 +536,14 @@ <h3>
{% include 'confirmation_modal.html' with modal_id='deleteEvaluationModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteEvaluationModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:evaluation_delete' %}",
data: {"evaluation_id": dataId},
success: function(){ $('#evaluation-row-'+dataId).hide('slow', function(){ $('#evaluation-row-'+dataId).remove(); }); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:evaluation_delete' %}", {
body: new URLSearchParams({evaluation_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
$('#evaluation-row-'+dataId).hide('slow', function(){ $('#evaluation-row-'+dataId).remove(); });
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Delete course' as title %}
Expand All @@ -545,13 +552,14 @@ <h3>
{% include 'confirmation_modal.html' with modal_id='deleteCourseModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteCourseModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:course_delete' %}",
data: {"course_id": dataId},
success: function(){ $('#course-row-'+dataId).hide('slow', function(){ $('#course-row-'+dataId).remove(); }); },
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:course_delete' %}", {
body: new URLSearchParams({course_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
$('#course-row-'+dataId).hide('slow', function(){ $('#course-row-'+dataId).remove(); });
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% trans 'Activate reward points' as title %}
Expand Down
32 changes: 17 additions & 15 deletions evap/staff/templates/staff_user_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ <h5 class="card-title me-auto">{% trans 'Export evaluation results' %}</h5>
{% include 'confirmation_modal.html' with modal_id='deleteModal' title=title question=question action_text=action_text btn_type='danger' %}
<script type="text/javascript">
function deleteModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:user_delete' %}",
data: {"user_id": dataId},
success: function(){window.location="{% url 'staff:user_index' %}"},
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
};
fetch("{% url 'staff:user_delete' %}", {
body: new URLSearchParams({user_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
window.location="{% url 'staff:user_index' %}"
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
}
</script>
{% if form.instance.id %}
{% trans 'Send notification email' as title %}
Expand All @@ -131,13 +132,14 @@ <h5 class="card-title me-auto">{% trans 'Export evaluation results' %}</h5>
{% include 'confirmation_modal.html' with modal_id='resendEmailModal' title=title question=question action_text=action_text btn_type='success' %}
<script type="text/javascript">
function resendEmailModalAction(dataId) {
$.ajax({
type: "POST",
url: "{% url 'staff:user_resend_email' %}",
data: {"user_id": dataId},
success: function(){window.location="{% url 'staff:user_edit' user_id %}"},
error: function(){ window.alert("{% trans 'The server is not responding.' %}"); }
});
fetch("{% url 'staff:user_resend_email' %}", {
body: new URLSearchParams({user_id: dataId}),
headers: CSRF_HEADERS,
method: "POST",
}).then(response => {
assert(response.ok);
window.location="{% url 'staff:user_edit' user_id %}"
}).catch(error => {window.alert("{% trans 'The server is not responding.' %}");});
};
</script>
{% endif %}
Expand Down
11 changes: 2 additions & 9 deletions evap/static/ts/src/csrf-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ function isMethodCsrfSafe(method: string): boolean {
return ["GET", "HEAD", "OPTIONS", "TRACE"].includes(method);
}

// setup ajax sending csrf token
$.ajaxSetup({
beforeSend: function (xhr: JQuery.jqXHR, settings: JQuery.AjaxSettings) {
const isMethodSafe = settings.method && isMethodCsrfSafe(settings.method);
if (!isMethodSafe && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
},
});
// TODO
(globalThis as any).CSRF_HEADERS = CSRF_HEADERS;

export const testable = {
getCookie,
Expand Down
2 changes: 2 additions & 0 deletions evap/static/ts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const selectOrError = <T extends Element>(selector: string, root: Element
export function assert(condition: unknown, message: string = "Assertion Failed"): asserts condition {
if (!condition) throw new Error(message);
}
// TODO
(globalThis as any).assert = assert;

export function assertDefined<T>(val: T): asserts val is NonNullable<T> {
assert(val !== undefined);
Expand Down

0 comments on commit 5b1cf1c

Please sign in to comment.