changeset 138:2ec5653b190a

add url "whereami" to get the client IP address
author souliane <souliane@mailoo.org>
date Tue, 26 Apr 2016 19:32:44 +0200
parents 759f7cd2bc6a
children 8aeaa651a8ac
files sat_website/urls.py sat_website/views.py
diffstat 2 files changed, 14 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/sat_website/urls.py	Tue Mar 08 08:59:13 2016 +0100
+++ b/sat_website/urls.py	Tue Apr 26 19:32:44 2016 +0200
@@ -25,5 +25,6 @@
 urlpatterns = patterns('sat_website.views',
     url(r'^(?P<category>\w*)(.html)?$', 'generic_cat'),
     (r'^i18n/', include('django.conf.urls.i18n')),
+    url(r'^whereami/?$', 'whereami')
 )
 
--- a/sat_website/views.py	Tue Mar 08 08:59:13 2016 +0100
+++ b/sat_website/views.py	Tue Apr 26 19:32:44 2016 +0200
@@ -20,7 +20,7 @@
 You should have received a copy of the GNU Affero General Public License
 along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 """
-from django.http import Http404
+from django.http import Http404, HttpResponse
 from django.shortcuts import render_to_response
 from django.core.context_processors import csrf
 from django.utils.translation import ugettext_lazy as _
@@ -68,9 +68,11 @@
                   'libervia': _(u'Libervia frontend (web server and client)'),
                   }
 
+
 def overview(request):
     return render_to_response('sat_website/overview.html')
 
+
 def generic_cat(request, category):
     if category in CATEGORIES_ALIASES:
         category = CATEGORIES_ALIASES[category]
@@ -120,3 +122,13 @@
         return render_to_response('sat_website/%s.html' % (category,), context)
     else:
         raise Http404
+
+
+def whereami(request):
+    """Return the client IP address"""
+    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
+    if x_forwarded_for:
+        ip = x_forwarded_for.split(',')[0]
+    else:
+        ip = request.META.get('REMOTE_ADDR')
+    return HttpResponse(ip)