~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to tracking/utils.py

  • Committer: franku
  • Date: 2018-04-26 20:18:55 UTC
  • mfrom: (489.1.28 widelands)
  • Revision ID: somal@arcor.de-20180426201855-uwt3b8gptpav6wrm
updated code base to fit with django 1.11.12; replaced app tracking with a new middleware

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.conf import settings
2
 
import re
3
 
import unicodedata
4
 
 
5
 
# this is not intended to be an all-knowing IP address regex
6
 
IP_RE = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
7
 
 
8
 
 
9
 
def get_ip(request):
10
 
    """Retrieves the remote IP address from the request data.
11
 
 
12
 
    If the user is
13
 
    behind a proxy, they may have a comma-separated list of IP addresses, so
14
 
    we need to account for that.  In such a case, only the first IP in the
15
 
    list will be retrieved.  Also, some hosts that use a proxy will put the
16
 
    REMOTE_ADDR into HTTP_X_FORWARDED_FOR.  This will handle pulling back the
17
 
    IP from the proper place.
18
 
 
19
 
    """
20
 
 
21
 
    # if neither header contain a value, just use local loopback
22
 
    ip_address = request.META.get('HTTP_X_FORWARDED_FOR',
23
 
                                  request.META.get('REMOTE_ADDR', '127.0.0.1'))
24
 
    if ip_address:
25
 
        # make sure we have one and only one IP
26
 
        try:
27
 
            ip_address = IP_RE.match(ip_address)
28
 
            if ip_address:
29
 
                ip_address = ip_address.group(0)
30
 
            else:
31
 
                # no IP, probably from some dirty proxy or other device
32
 
                # throw in some bogus IP
33
 
                ip_address = '10.0.0.1'
34
 
        except IndexError:
35
 
            pass
36
 
 
37
 
    return ip_address
38
 
 
39
 
 
40
 
def get_timeout():
41
 
    """Gets any specified timeout from the settings file, or use 10 minutes by
42
 
    default."""
43
 
    return getattr(settings, 'TRACKING_TIMEOUT', 10)
44
 
 
45
 
 
46
 
def get_cleanup_timeout():
47
 
    """
48
 
    Gets any specified visitor clean-up timeout from the settings file, or
49
 
    use 24 hours by default
50
 
    """
51
 
    return getattr(settings, 'TRACKING_CLEANUP_TIMEOUT', 24)
52
 
 
53
 
 
54
 
def u_clean(s):
55
 
    """A strange attempt at cleaning up unicode."""
56
 
 
57
 
    uni = ''
58
 
    try:
59
 
        # try this first
60
 
        uni = str(s).decode('iso-8859-1')
61
 
    except UnicodeDecodeError:
62
 
        try:
63
 
            # try utf-8 next
64
 
            uni = str(s).decode('utf-8')
65
 
        except UnicodeDecodeError:
66
 
            # last resort method... one character at a time (ugh)
67
 
            if s and type(s) in (str, unicode):
68
 
                for c in s:
69
 
                    try:
70
 
                        uni += unicodedata.normalize('NFKC', unicode(c))
71
 
                    except UnicodeDecodeError:
72
 
                        uni += '-'
73
 
 
74
 
    return uni.encode('ascii', 'xmlcharrefreplace')