]> git.parisson.com Git - teleforma.git/commitdiff
fix HOST context
authorGuillaume Pellerin <yomguy@parisson.com>
Thu, 11 Jul 2013 16:24:38 +0000 (18:24 +0200)
committerGuillaume Pellerin <yomguy@parisson.com>
Thu, 11 Jul 2013 16:24:38 +0000 (18:24 +0200)
teleforma/context_processors.py
teleforma/templates/teleforma/course_media.html

index 94fadbbb1a7c8687f2c20a53fcee6bd8ed272110..8d044d3431f4a0fcb39c42134387e02b143dee64 100644 (file)
 
 from teleforma.views.core import *
 
+from django.conf import settings
+import socket
+import fcntl
+import struct
+
+interfaces = ['eth0', 'eth1', 'eth2', 'eth0-eth2', 'eth3', 'eth4',
+                  'wlan0', 'wlan1', 'wlan2', 'wlan3', 'wlan4']
+
 
 def periods(request):
     """return the periods assigned to the user """
@@ -45,3 +53,47 @@ def periods(request):
         return {'periods': None}
     else:
         return {'periods': get_periods(user)}
+
+
+
+def get_ip_address(ifname):
+    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    ip = socket.inet_ntoa(fcntl.ioctl(
+            s.fileno(),
+            0x8915,  # SIOCGIFADDR
+            struct.pack('256s', ifname[:15])
+            )[20:24])
+    return ip
+
+def get_local_host():
+    ip = ''
+    for interface in interfaces:
+        try:
+            ip = get_ip_address(interface)
+            if ip:
+                local_ip = ip
+                break
+        except:
+            local_ip = '127.0.0.1'
+    return local_ip
+
+
+def get_http_host(request):
+    host = request.META['REMOTE_ADDR']
+    if ':' in host:
+        host = host.split(':')[0]
+    return host
+
+
+def host(request):
+    request_host = get_http_host(request)
+    local_host = get_local_host()
+
+    if request_host.split('.')[0] == local_host.split('.')[0] or \
+                                 request_host == '127.0.0.1' or request_host == 'localhost':
+        # LAN access
+        ip = local_host
+    else:
+        ip = settings.ROUTER_IP
+
+    return {'HOST': ip }
index b021665ee244725f6700f535f2f2a172dbfe1ec2..845a5fd33a539393e73db39dbd3ee4a6cc7e00cd 100644 (file)
@@ -109,7 +109,7 @@ $(document).ready(function(){
  {% for related in media.item.related.all %}
   {% if related.title == "preview" %}
    {% thumbnail related.file "300" as im %}
-    <img src="{{ im.url }}" width="300px" alt="preview" />
+    <img src="{{ im.url|set_host:HOST }}" width="300px" alt="preview" />
    {% endthumbnail %}
   {% endif %}
  {% endfor %}