diff --git a/ifcbdb/assets/js/bin.js b/ifcbdb/assets/js/bin.js index 133c0d2c..d3160ac1 100644 --- a/ifcbdb/assets/js/bin.js +++ b/ifcbdb/assets/js/bin.js @@ -195,7 +195,11 @@ function updateBinStats(data) { } function updateBinMetadata() { - $.get("/api/metadata/" + _bin, function(data) { + let payload = { + csrfmiddlewaretoken: _csrf + }; + + $.post("/api/metadata/" + _bin, payload, function(data) { tbody = $("#bin-metadata tbody"); tbody.empty(); @@ -245,7 +249,7 @@ function updateBinDownloadLinks(data) { $("#download-features").attr("href", infix + _bin + "_features.csv"); $("#download-class-scores").attr("href", infix + _bin + "_class_scores.csv"); - $.get('/api/has_products/' + _bin, function(r) { + $.post('/api/has_products/' + _bin, { csrfmiddlewaretoken: _csrf }, function(r) { $("#download-blobs").toggle(r["has_blobs"]); $("#download-blobs-disabled").toggle(!r["has_blobs"]); @@ -529,12 +533,12 @@ function loadMosaic(pageNumber) { // indicate to the user that coordinates are loading $("#mosaic").css("cursor", "wait"); - var binDataUrl = "/api/bin/" + _bin + - "?view_size=" + viewSize + - "&scale_factor=" + scaleFactor + - "&" + buildFilterOptionsQueryString(true); + let binDataPayload = buildFilterOptionsPayload(true); + binDataPayload.view_size = viewSize; + binDataPayload.scale_factor = scaleFactor; + binDataPayload.csrfmiddlewaretoken = _csrf; - $.get(binDataUrl, function(data) { + $.post("/api/bin/" + _bin, binDataPayload, function(data) { // Update the coordinates for the image _coordinates = JSON.parse(data["coordinates"]); @@ -563,12 +567,14 @@ function loadMosaic(pageNumber) { _isMosaicLoading = false; }); - var mosaicUrl = "/api/mosaic/encoded_image/" + _bin + - "?view_size=" + viewSize + - "&scale_factor=" + scaleFactor + - "&page=" + pageNumber; + let imagePayload = { + view_size: viewSize, + scale_factor: scaleFactor, + page: pageNumber, + csrfmiddlewaretoken: _csrf + }; - $.get(mosaicUrl, function(data) { + $.post("/api/mosaic/encoded_image/" + _bin, imagePayload, function(data) { $("#mosaic").attr("src", "data:image/png;base64," + data); $("#mosaic-loading").hide(); $("#mosaic").show(); @@ -688,7 +694,7 @@ function changeMarker(index) { // isn't always accurate. It will be the location of the marker, which is based on the spidering, and we // need the actual location of that bin to plot it correctly. If we use the stored value, the marker will be // put at the edge of the spidering effect - $.get("/api/bin_location?pid=" + _bin, function(resp){ + $.post("/api/bin_location?pid=" + _bin, { csrfmiddlewaretoken: _csrf }, function(resp){ if (resp.lat && resp.lng) { // Create a new marker based on the information on the matched marker from the main list let newMarker = L.marker( @@ -720,7 +726,7 @@ function recenterMap() { if (_map == null) return; - // If the current bin si already selected, nothing more needs to be done + // If the current bin is already selected, nothing more needs to be done if (_selectedMarker != null && _selectedMarker.options.title == _bin) return; @@ -898,7 +904,11 @@ function updatePlotVariables(plotData) { } function initPlotData() { - $.get("/api/plot/" + _bin, function(data) { + let payload = { + csrfmiddlewaretoken: _csrf + }; + + $.post("/api/plot/" + _bin, payload, function(data) { _plotData = data; var plotXAxis = $("#plot-x-axis"); @@ -916,7 +926,11 @@ function initPlotData() { function updatePlotData() { // TODO: The plot container has a hard coded height on it that we should make dynamic. However, doing so causes // the plot, when rendering a second time, to revert back to the minimum height - $.get("/api/plot/" + _bin, function(data) { + let payload = { + csrfmiddlewaretoken: _csrf + }; + + $.post("/api/plot/" + _bin, payload, function(data) { _plotData = data; updatePlotVariables(data); diff --git a/ifcbdb/assets/js/site.js b/ifcbdb/assets/js/site.js index 4c1f46e7..4ead016c 100644 --- a/ifcbdb/assets/js/site.js +++ b/ifcbdb/assets/js/site.js @@ -390,9 +390,10 @@ function updateTimelineFilters(wrapper, initialValues) { applyFilters.prop("disabled", false); } - var qs = buildFilterOptionsQueryString(false, dataset, instrument, tags, cruise, sampleType); + let payload = buildFilterOptionsPayload(false, dataset, instrument, tags, cruise, sampleType); + payload.csrfmiddlewaretoken = _csrf; - $.get("/api/filter_options?" + qs, function(data){ + $.post("/api/filter_options", payload, function(data){ reloadFilterDropdown(datasetFilter, data.dataset_options, dataset); reloadFilterDropdown(instrumentFilter, data.instrument_options, instrument, "IFCB"); reloadFilterDropdown(cruiseFilter, data.cruise_options, cruise); @@ -457,9 +458,10 @@ function applyFilters() { .map(function() {return $(this).val()}).get() .join(); - var qs = buildFilterOptionsQueryString(false, dataset, instrument, tags, cruise, sampleType); + let payload = buildFilterOptionsPayload(false, dataset, instrument, tags, cruise, sampleType); + payload.csrfmiddlewaretoken = _csrf; - $.get("/api/bin_exists?" + qs, function(data) { + $.post("/api/bin_exists", payload, function(data) { if (!data.exists) { alert("No bins were found matching the specified filters. Please update the filters and try again") return; @@ -485,7 +487,12 @@ function goToBin(pid) { if (!pid || pid.trim() == "") return; - $.get("/api/single_bin_exists?pid=" + pid.trim(), function(data){ + let payload = { + pid: pid.trim(), + csrfmiddlewaretoken: _csrf + }; + + $.post("/api/single_bin_exists", payload, function(data){ if (!data.exists) { alert("No matching bin was found. Please check the PID and try again"); return; @@ -529,6 +536,19 @@ function buildFilterOptionsQueryString(fromGlobals, dataset, instrument, tags, c return args.length == 0 ? "" : args.join("&"); } +function buildFilterOptionsPayload(fromGlobals, dataset, instrument, tags, cruise, sampleType) { + let args = buildFilterOptionsArray(fromGlobals, dataset, instrument, tags, cruise, sampleType); + let payload = {}; + + for (let i = 0; i < args.length; i++) { + var parts = args[i].split("="); + + payload[parts[0]] = parts[1]; + } + + return payload; +} + function reloadFilterDropdown(dropdown, options, value, textPrefix) { dropdown.empty(); dropdown.append($("