Skip to content

Commit

Permalink
Merge pull request #1754 from MTG/solr-maps
Browse files Browse the repository at this point in the history
Add an advanced search option to "display results in a map"
  • Loading branch information
ffont authored Feb 9, 2024
2 parents 20d47d9 + ef508e1 commit 755456c
Show file tree
Hide file tree
Showing 22 changed files with 400 additions and 251 deletions.
9 changes: 2 additions & 7 deletions accounts/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,9 @@ def test_sound_search_response(self):
resp = self.client.get(reverse('sounds-search'))
self.assertEqual(resp.status_code, 200)

def test_geotags_box_response(self):
# 200 response on geotag box page access
resp = self.client.get(reverse('geotags-box'))
self.assertEqual(resp.status_code, 200)

def test_geotags_box_iframe_response(self):
def test_geotags_embed_response(self):
# 200 response on geotag box iframe
resp = self.client.get(reverse('embed-geotags-box-iframe'))
resp = self.client.get(reverse('embed-geotags'))
self.assertEqual(resp.status_code, 200)

def test_accounts_manage_pages(self):
Expand Down
27 changes: 27 additions & 0 deletions clustering/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,32 @@ def cluster_sound_results(request, features=DEFAULT_FEATURES):
return {'finished': False, 'error': False}


def get_ids_in_cluster(request, requested_cluster_id):
"""Get the sound ids in the requested cluster. Used for applying a filter by id when using a cluster facet.
"""
try:
requested_cluster_id = int(requested_cluster_id) - 1

# results are cached in clustering_utilities, available features are defined in the clustering settings file.
result = cluster_sound_results(request, features=DEFAULT_FEATURES)
results = result['result']

sounds_from_requested_cluster = results[int(requested_cluster_id)]

except ValueError:
return []
except IndexError:
return []
except KeyError:
# If the clustering is not in cache the 'result' key won't exist
# This means that the clustering computation will be triggered asynchronously.
# Moreover, the applied clustering filter will have no effect.
# Somehow, we should inform the user that the clustering results were not available yet, and that
# he should try again later to use a clustering facet.
return []

return sounds_from_requested_cluster


def hash_cache_key(key):
return create_hash(key, limit=32)
3 changes: 3 additions & 0 deletions freesound/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@
SEARCH_ENGINE_NUM_SIMILAR_SOUNDS_PER_QUERY = 500
USE_SEARCH_ENGINE_SIMILARITY = False

SEARCH_ALLOW_DISPLAY_RESULTS_IN_MAP = True
MAX_SEARCH_RESULTS_IN_MAP_DISPLAY = 10000 # This is the maximum number of sounds that will be shown when using "display results in map" mode

# -------------------------------------------------------------------------------
# Similarity client settings
SIMILARITY_ADDRESS = 'similarity'
Expand Down
2 changes: 1 addition & 1 deletion freesound/static/bw-frontend/src/components/mapsMapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function makeSoundsMap(geotags_url, map_element_id, on_built_callback, on_bounds
if (nSounds > 1){
// The padding and offset "manual" adjustments of bounds below are to make the boudns more similar to
// those created in the mapbox static maps
map.fitBounds(bounds, {duration:0, offset:[-10, 0], padding: {top:60, right:60, left:0, bottom:50}});
map.fitBounds(bounds, {duration:0, offset:[0, 0], padding: {top:60, right:60, left:60, bottom:60}});
} else {
map.setZoom(3);
if (nSounds > 0){
Expand Down
45 changes: 20 additions & 25 deletions freesound/static/bw-frontend/src/pages/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ const tagFilterInput = document.getElementById("tagFilter");
let currentLat;
let currentLon;
let currentZoom;
let currentBoxBlLa;
let currentBoxBlLon;
let currentBoxTrLat;
let currentBoxTrLon;

const toggleEmbedControls = () => {
if (embedControls.classList.contains('display-none')){
Expand All @@ -42,22 +38,21 @@ const updateQueryStringParameter = (uri, key, value) => {
}
}

const updateEmbedCode = (mapElementId, lat, lon, zoom, boxBlLat, boxBlLon, boxTrLat, boxTrLon) => {
const updateEmbedCode = (mapElementId, lat, lon, zoom) => {
if (embedCodeElement === null){ return; }

const mapCanvas = document.getElementById(mapElementId);
let mapCanvas;
if (mapElementId === undefined){
mapCanvas = document.getElementsByClassName('main-map')[0];
} else {
mapCanvas = document.getElementById(mapElementId);
}

// Store lat, lon and zoom globally so we can use them later to call updateEmbedCode without accessing map
currentLat = lat;
currentLon = lon;
currentZoom = zoom;
currentBoxBlLa = boxBlLat;
currentBoxBlLon = boxBlLon;
currentBoxTrLat = boxTrLat;
currentBoxTrLon = boxTrLon;

// Generate embed code
const box = "#box=" + boxBlLat + "," + boxBlLon+"," + boxTrLat+"," + boxTrLon;
const width = parseInt(embedWidthInputElement.value, 10);
const height = parseInt(embedHeightInputElement.value, 10);
let cluster = 'on';
Expand All @@ -66,16 +61,19 @@ const updateEmbedCode = (mapElementId, lat, lon, zoom, boxBlLat, boxBlLon, boxTr
}
let embedCode = "<iframe frameborder=\"0\" scrolling=\"no\" src=\"" + mapCanvas.dataset.geotagsEmbedBaseUrl
+ "?c_lat=" + lat + "&c_lon=" + lon + "&z=" + zoom + "&c=" + cluster + "&w=" + width + "&h=" + height;
if (mapCanvas.dataset.mapUsername !== "None"){
if (mapCanvas.dataset.mapUsername !== ""){
embedCode += "&username=" + mapCanvas.dataset.mapUsername;
}
if (mapCanvas.dataset.mapTag !== "None"){
if (mapCanvas.dataset.mapTag !== ""){
embedCode += "&tag=" + mapCanvas.dataset.mapTag;
}
if (mapCanvas.dataset.mapPackId !== "None"){
if (mapCanvas.dataset.mapPackId !== ""){
embedCode += "&pack=" + mapCanvas.dataset.mapPackId;
}
embedCode += box + "\" width=\"" + width + "\" height=\"" + height + "\"></iframe>";
if (mapCanvas.dataset.mapQp !== ""){
embedCode += "&qp=" + mapCanvas.dataset.mapQp;
}
embedCode += "\" width=\"" + width + "\" height=\"" + height + "\"></iframe>";
embedCodeElement.innerText = embedCode;

// Update page URL so it can directly be used to share the map
Expand All @@ -87,7 +85,7 @@ const updateEmbedCode = (mapElementId, lat, lon, zoom, boxBlLat, boxBlLon, boxTr
}

const changeEmbedWidthHeightCluster = () => {
updateEmbedCode(undefined, currentLat, currentLon, currentZoom, currentBoxBlLa, currentBoxBlLon, currentBoxTrLat, currentBoxTrLon);
updateEmbedCode(undefined, currentLat, currentLon, currentZoom);
}

const initMap = (mapCanvas) => {
Expand All @@ -107,7 +105,7 @@ const initMap = (mapCanvas) => {
[embedWidthInputElement, embedHeightInputElement, embedClusterCheckElement].forEach(element => {
if (element !== null){
element.addEventListener('change', () => {
changeEmbedWidthHeightCluster();
changeEmbedWidthHeightCluster();
});
}
});
Expand Down Expand Up @@ -140,13 +138,6 @@ const initMap = (mapCanvas) => {
zoom = mapCanvas.dataset.mapZoom;
}
let url = mapCanvas.dataset.geotagsUrl;
const urlBox = mapCanvas.dataset.geotagsUrlBox;
const box = document.location.hash.slice(5, document.location.hash.length);
if (box !== ''){
// If box is given, get the geotags only from that box
url = `${urlBox}?box=${box}`;
}

const showSearch = (mapCanvas.dataset.mapShowSearch !== undefined && mapCanvas.dataset.mapShowSearch === 'true');
const showStyleSelector = true;
const clusterGeotags = true;
Expand All @@ -156,7 +147,11 @@ const initMap = (mapCanvas) => {
if (loadingIndicator !== null){
loadingIndicator.innerText = `${numLoadedSounds} sound${ numLoadedSounds === 1 ? '': 's'}`;
}
embedWidthInputElement.value = mapCanvas.offsetWidth;
embedHeightInputElement.value = mapCanvas.offsetHeight;
}, updateEmbedCode, centerLat, centerLon, zoom, showSearch, showStyleSelector, clusterGeotags, showMapEvenIfNoGeotags);


}

export { initMap };
17 changes: 16 additions & 1 deletion freesound/static/bw-frontend/src/pages/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ var filter_in_remix_group_element = document.getElementById('filter_in_remix_gro
var sort_by_element = document.getElementById('sort-by');
var group_by_pack_element = document.getElementById('group_by_pack');
var only_sounds_with_pack_element = document.getElementById('only_sounds_with_pack');
var use_compact_mode_element = document.getElementById('use_compact_mode');
var use_compact_mode_element = document.getElementById('use_compact_mode');
var use_map_mode_element = document.getElementById('use_map_mode');

function update_hidden_compact_mode_element() {
var hiddenElement = document.getElementById('use_compact_mode_hidden');
Expand All @@ -112,6 +113,20 @@ use_compact_mode_element.addEventListener('change', function() {
update_hidden_compact_mode_element()
})

function update_hidden_map_mode_element() {
var hiddenElement = document.getElementById('use_map_mode_hidden');
if (use_map_mode_element.checked) {
hiddenElement.value = "1";
} else {
hiddenElement.value = "0";
}
}

update_hidden_map_mode_element()
use_map_mode_element.addEventListener('change', function() {
update_hidden_map_mode_element()
})

function advancedSearchOptionsIsVisible()
{
return advanced_search_hidden_field.value === "1";
Expand Down
1 change: 1 addition & 0 deletions freesound/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'--with-xunit',
]

AKISMET_KEY = '' # Avoid making requests to "real" Akismet server if running
SECRET_KEY = "testsecretwhichhastobeatleast16characterslong"
SUPPORT = (('Name Surname', '[email protected]'),)
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
Expand Down
4 changes: 2 additions & 2 deletions freesound/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
path('charts/', accounts.views.charts, name="charts"),

path('embed/sound/iframe/<int:sound_id>/simple/<player_size>/', sounds.views.embed_iframe, name="embed-simple-sound-iframe"),
path('embed/geotags_box/iframe/', geotags.views.embed_iframe, name="embed-geotags-box-iframe"),
path('embed/geotags_box/iframe/', geotags.views.embed_iframe, name="embed-geotags"),
path('oembed/', sounds.views.oembed, name="oembed-sound"),

path('after-download-modal/', sounds.views.after_download_modal, name="after-download-modal"),
Expand All @@ -93,7 +93,7 @@
path('browse/packs/', sounds.views.packs, name="packs"),
path('browse/random/', sounds.views.random, name="sounds-random"),
re_path(r'^browse/geotags/(?P<tag>[\w-]+)?/?$', geotags.views.geotags, name="geotags"),
path('browse/geotags_box/', geotags.views.geotags_box, name="geotags-box"),
path('browse/query/', geotags.views.for_query, name="geotags-query"),

path('contact/', support.views.contact, name="contact"),

Expand Down
14 changes: 7 additions & 7 deletions geotags/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ def test_browse_geotags(self):
check_values = {'tag': 'soundscape', 'username': None}
self.check_context(resp.context, check_values)

def test_browse_geotags_box(self):
resp = self.client.get(reverse('geotags-box'))
check_values = {'center_lat': None, 'center_lon': None, 'zoom': None, 'username': None}
self.check_context(resp.context, check_values)

def test_geotags_box_iframe(self):
resp = self.client.get(reverse('embed-geotags-box-iframe'))
def test_geotags_embed(self):
resp = self.client.get(reverse('embed-geotags'))
check_values = {'m_width': 942, 'm_height': 600, 'cluster': True, 'center_lat': None, 'center_lon': None,
'zoom': None, 'username': None}
self.check_context(resp.context, check_values)
Expand Down Expand Up @@ -96,3 +91,8 @@ def test_browse_geotags_case_insensitive(self):
# Response contains 3 int32 objects per sound: id, lat and lng. Total size = 3 * 4 bytes = 12 bytes
n_sounds = len(resp.content) // 12
self.assertEqual(n_sounds, 2)

def test_browse_geotags_for_query(self):
resp = self.client.get(reverse('geotags-query') + f'?q=barcelona')
check_values = {'query_description': 'barcelona'}
self.check_context(resp.context, check_values)
2 changes: 1 addition & 1 deletion geotags/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
path('sounds_barray/user_latest/<username>/', geotags.geotags_for_user_latest_barray, name="geotags-for-user-latest-barray"),
path('sounds_barray/pack/<int:pack_id>/', geotags.geotags_for_pack_barray, name="geotags-for-pack-barray"),
path('sounds_barray/sound/<int:sound_id>/', geotags.geotag_for_sound_barray, name="geotags-for-sound-barray"),
path('sounds_barray/query/', geotags.geotags_for_query_barray, name="geotags-for-query-barray"),
re_path(r'^sounds_barray/(?P<tag>[\w-]+)?/?$', geotags.geotags_barray, name="geotags-barray"),
path('geotags_box_barray/', geotags.geotags_box_barray, name="geotags-box-barray"),
path('infowindow/<int:sound_id>/', geotags.infowindow, name="geotags-infowindow"),
]
Loading

0 comments on commit 755456c

Please sign in to comment.