diff gpp/core/functions.py @ 227:423c39ee44e0

Rework the who's online middleware and template tag for #87.
author Brian Neal <bgneal@gmail.com>
date Tue, 06 Jul 2010 03:02:20 +0000
parents 500e5875a306
children 27bee3ac85e6
line wrap: on
line diff
--- a/gpp/core/functions.py	Thu Jun 10 03:31:57 2010 +0000
+++ b/gpp/core/functions.py	Tue Jul 06 03:02:20 2010 +0000
@@ -1,5 +1,6 @@
 """This file houses various core utility functions for GPP"""
 import datetime
+import re
 
 import django.core.mail
 from django.contrib.sites.models import Site
@@ -65,3 +66,17 @@
         year_range = "%d - %d" % (BASE_YEAR, curr_year)
 
     return 'Copyright (C) %s, SurfGuitar101.com' % year_range
+
+
+IP_PAT = re.compile('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
+
+def get_ip(request):
+    """Returns the IP from the request or None if it cannot be retrieved."""
+    ip = request.META.get('HTTP_X_FORWARDED_FOR',
+            request.META.get('REMOTE_ADDR'))
+
+    if ip:
+        match = IP_PAT.match(ip)
+        ip = match.group(1) if match else None
+
+    return ip