Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import re
  4. import warnings
  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. def get_ip(request):
  8. """
  9. Retrieves the remote IP address from the request data. If the user is
  10. behind a proxy, they may have a comma-separated list of IP addresses, so
  11. we need to account for that. In such a case, only the first IP in the
  12. list will be retrieved. Also, some hosts that use a proxy will put the
  13. REMOTE_ADDR into HTTP_X_FORWARDED_FOR. This will handle pulling back the
  14. IP from the proper place.
  15. **NOTE** This function was taken from django-tracking (MIT LICENSE)
  16. http://code.google.com/p/django-tracking/
  17. """
  18. # if neither header contain a value, just use local loopback
  19. ip_address = request.META.get('HTTP_X_FORWARDED_FOR',
  20. request.META.get('REMOTE_ADDR', '127.0.0.1'))
  21. if ip_address:
  22. # make sure we have one and only one IP
  23. try:
  24. ip_address = IP_RE.match(ip_address)
  25. if ip_address:
  26. ip_address = ip_address.group(0)
  27. else:
  28. # no IP, probably from some dirty proxy or other device
  29. # throw in some bogus IP
  30. ip_address = '10.0.0.1'
  31. except IndexError:
  32. pass
  33. return ip_address
  34. class RemovedInHitCount13Warning(DeprecationWarning):
  35. pass
  36. # enable warnings by default for our deprecated
  37. warnings.simplefilter("default", RemovedInHitCount13Warning)