Parcourir la source

altitude: Proxify requests to api.geonames.org (fixes #23)

This fixes issue #23 related to mixed HTTP/HTTPS content, because
api.geonames.org is not available over HTTPS for free-of-charge usage.

This also has the advantage of defining a generic interface, so that we
can support other sources of altitude data with different formatting
(e.g. IGN) without changing the javascript code.

While we are at it, also cache the result of API calls in Django.  This
will avoid hammering on the real API server if our javascript code is too
enthusiastic.

Proxying has a slight disadvantage: if the API server performs
rate-limiting based on IP address, then we will reach the limit much more
quickly.  This is because all requests will now come from the celutz
server, instead of from each individual client broswer.
Baptiste Jonglez il y a 8 ans
Parent
commit
455b941eb1

+ 0 - 0
altitude/__init__.py


+ 5 - 0
altitude/apps.py

@@ -0,0 +1,5 @@
+from django.apps import AppConfig
+
+
+class AltitudeConfig(AppConfig):
+    name = 'altitude'

+ 8 - 0
altitude/urls.py

@@ -0,0 +1,8 @@
+from django.conf.urls import url
+from django.views.decorators.cache import cache_page
+
+from altitude.views import geonames_altitude
+
+urlpatterns = [
+    url(r'^(?P<lat>-?\d+(?:\.\d+)?)/(?P<lon>-?\d+(?:\.\d+)?)/$', cache_page(7*24*60*60)(geonames_altitude)),
+]

+ 11 - 0
altitude/views.py

@@ -0,0 +1,11 @@
+import urllib
+
+from django.http import HttpResponse
+from django.conf import settings
+
+
+def geonames_altitude(request, lat, lon):
+    lat = float(lat)
+    lon = float(lon)
+    url = settings.GEONAMES_ASTERGDEM.format(lat=lat, lon=lon)
+    return HttpResponse(urllib.request.urlopen(url))

+ 3 - 0
celutz/settings.py

@@ -38,6 +38,7 @@ INSTALLED_APPS = (
     'kombu.transport.django',
     'kombu.transport.django',
     'rest_framework',
     'rest_framework',
     'panorama.apps.PanoramaConfig',
     'panorama.apps.PanoramaConfig',
+    'altitude.apps.AltitudeConfig',
     'api'
     'api'
 )
 )
 
 
@@ -119,6 +120,8 @@ STATIC_URL = '/static/'
 LOGIN_REQUIRED = False
 LOGIN_REQUIRED = False
 
 
 
 
+GEONAMES_ASTERGDEM = "http://api.geonames.org/astergdem?lat={lat}&lng={lon}&username=celutz&style=full"
+
 # For uploaded panorama
 # For uploaded panorama
 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
 MEDIA_URL = '/media/'
 MEDIA_URL = '/media/'

+ 1 - 0
celutz/urls.py

@@ -7,6 +7,7 @@ urlpatterns = [
     # Examples:
     # Examples:
     # url(r'^$', 'celutz.views.home', name='home'),
     # url(r'^$', 'celutz.views.home', name='home'),
     url(r'^admin/', include(admin.site.urls)),
     url(r'^admin/', include(admin.site.urls)),
+    url(r'^altitude/', include('altitude.urls', namespace="altitude")),
     url(r'^', include('panorama.urls', namespace="panorama")),
     url(r'^', include('panorama.urls', namespace="panorama")),
     url(r'^api/v1/', include('api.urls')),
     url(r'^api/v1/', include('api.urls')),
     url(r'^i18n/', include('django.conf.urls.i18n')),
     url(r'^i18n/', include('django.conf.urls.i18n')),

+ 1 - 1
panorama/templates/panorama/main.html

@@ -124,7 +124,7 @@
             $('#id_custompoint-longitude').val(lng);
             $('#id_custompoint-longitude').val(lng);
             $('#id_newpano-longitude').val(lng);
             $('#id_newpano-longitude').val(lng);
             $('#id_newrefpoint-longitude').val(lng);
             $('#id_newrefpoint-longitude').val(lng);
-            $.get("http://api.geonames.org/astergdem?lat="+lat+"&lng="+lng+"&username=celutz&style=full" ).done(
+            $.get("/altitude/" + lat + "/" + lng + "/").done(
                 function(data) {
                 function(data) {
                     $('#id_custompoint-altitude').val(parseInt(data));
                     $('#id_custompoint-altitude').val(parseInt(data));
                     $('#id_newpano-altitude').val(parseInt(data));
                     $('#id_newpano-altitude').val(parseInt(data));