Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert API calls from GET to POST #326

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions ifcbdb/assets/js/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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"]);

Expand Down Expand Up @@ -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"]);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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);
Expand Down
30 changes: 25 additions & 5 deletions ifcbdb/assets/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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($("<option value='' />"));
Expand Down
24 changes: 24 additions & 0 deletions ifcbdb/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,33 @@ def to_url(self, value):
##################################
# Paths used for API/Ajax requests
##################################

# TODO: Done
path('api/time-series/<slug:metric>', views.generate_time_series, name='generate_time_series'),
path('api/bin/<slug:bin_id>', views.bin_data, name='bin_data'),
path('api/bin/<slug:bin_id>', views.bin_data),
path('api/closest_bin', views.closest_bin, name='closest_bin'), # closest bin in time

## TODO: Not used (called in changeToNearestBin, which is not called)? Already a post
path('api/nearest_bin', views.nearest_bin, name='nearest_bin'),

## TODO: Neither of these are used?
path('api/mosaic/coordinates/<slug:bin_id>', views.mosaic_coordinates, name='mosaic_coordintes'),
path('api/mosaic/encoded_image/<slug:bin_id>', views.mosaic_page_encoded_image, name='mosaic_page_encoded_image'),

# TODO: Done
path('api/mosaic/image/<slug:bin_id>.png', views.mosaic_page_image, name='mosaic_page_image'),

## TODO: All of these should remain a get call?
path('api/image/<slug:bin_id>/<int:target>', views.image_metadata, name='image_metadata'),
path('api/image_data/<slug:bin_id>/<int:target>', views.image_data, name='image_data'),
path('api/blob/<slug:bin_id>/<int:target>', views.image_blob, name='image_blob'),
path('api/outline/<slug:bin_id>/<int:target>', views.image_outline, name='image_outline'),

## TODO: Done; also found and fixed in one unused method: initPlotData. Deprecate/remove?
path('api/plot/<slug:bin_id>', views.plot_data, name='plot_data'),

# TODO: Done
path('api/metadata/<slug:bin_id>', views.bin_metadata, name='bin_metadata'),
path('api/bin_exists', views.bin_exists, name='bin_exists'),
path('api/single_bin_exists', views.single_bin_exists, name='single_bin_exists'),
Expand All @@ -122,11 +136,21 @@ def to_url(self, value):
path('api/search_bin_locations', views.search_bin_locations, name='search_bin_locations'),
path('api/search_timeline_locations', views.search_timeline_locations, name='search_timeline_locations'),
path('api/search_comments', views.search_comments, name='search_comments'),

## TODO: Deprecated? Nothing is calling this endpoint
path('api/tags', views.tags, name='tags'),

# TODO: Done
path('api/timeline_info', views.timeline_info, name='timeline_info'),
path('api/list_bins', views.list_bins, name='list_bins'),

# TODO: Nothing is calling this one? Might be used externally?
path('api/list_images/<slug:pid>', views.list_images, name='list_images'),

# TODO: Done
path('api/update_skip', views.update_skip, name='update_skip'),

# TODO: Nothing is calling these? Might be used externally?
path('api/export_metadata/<slug:dataset_name>', views.export_metadata_view, name='export_metadata'),
path('api/sync_bin', views.sync_bin, name='sync_bin'),
]
Loading