Пример #1
0
def set_location(request):
    """
    Redirect to a given url while setting the chosen location in the
    cookie. The url and the location_id need to be
    specified in the request parameters.

    Since this view changes how the user will see the rest of the site, it must
    only be accessed as a POST request. If called as a GET request, it will
    redirect to the page in the request (the 'next' parameter) without changing
    any state.
    """
    next = request.POST.get('next', None)
    if not next:
        next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = '/'
    response = http.HttpResponseRedirect(next)
    if request.method == 'POST':
        location_id = request.POST.get(
            'location_id', None) or request.POST.get('location', None)
        if location_id:
            try:
                location = get_class(
                    settings.GEOIP_LOCATION_MODEL).objects.get(pk=location_id)
                storage_class(request=request,
                              response=response).set(location=location,
                                                     force=True)
            except (ValueError, ObjectDoesNotExist):
                pass
    return response
Пример #2
0
def set_location(request):
    """
    Redirect to a given url while setting the chosen location in the
    cookie. The url and the location_id need to be
    specified in the request parameters.

    Since this view changes how the user will see the rest of the site, it must
    only be accessed as a POST request. If called as a GET request, it will
    redirect to the page in the request (the 'next' parameter) without changing
    any state.
    """
    next = request.REQUEST.get('next', None)
    if not next:
        next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = '/'
    response = http.HttpResponseRedirect(next)
    if request.method == 'POST':
        location_id = request.POST.get('location_id', None) or request.POST.get('location', None)
        if location_id:
            try:
                location = get_class(settings.GEOIP_LOCATION_MODEL).objects.get(pk=location_id)
                storage_class(request=request, response=response).set(location=location, force=True)
            except (ValueError, ObjectDoesNotExist):
                pass
    return response
Пример #3
0
    def test_get_class(self, SessionBase):
        """ FIXME: change to fake class"""
        test_hash = {
            'django.contrib.sessions.backends.base.SessionBase': SessionBase,
        }

        for class_string, expected_class_instance in test_hash.items():
            self.assertEqual(get_class(class_string), expected_class_instance)

        self.assertRaises(ImportError, get_class, 'django_geoip.fake')
Пример #4
0
    def test_get_class(self, SessionBase):
        """ FIXME: change to fake class"""
        test_hash = {
            'django.contrib.sessions.backends.base.SessionBase': SessionBase,
        }

        for class_string, expected_class_instance in test_hash.items():
            self.assertEqual(get_class(class_string), expected_class_instance)

        self.assertRaises(ImportError, get_class, 'django_geoip.fake')
Пример #5
0
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import SimpleLazyObject
from django_geoip.models import IpRange
from django_geoip.utils import get_class


location_model = SimpleLazyObject(
    lambda: get_class(settings.GEOIP_LOCATION_MODEL))


storage_class = get_class(settings.GEOIP_STORAGE_CLASS)


class Locator(object):
    """ A helper class that automates user location detection.
    """

    def __init__(self, request):
        self.request = request

    def locate(self):
        """ Find out what is user location (either from his IP or cookie).

        :return: :ref:`Custom location model <location_model>`
        """
        stored_location = self._get_stored_location()
        if not stored_location:
            ip_range = self._get_ip_range()
            stored_location = self._get_corresponding_location(ip_range)
Пример #6
0
 def _get_by_id(self, location_id):
     return get_class(settings.GEOIP_LOCATION_MODEL).objects.get(pk=location_id)
Пример #7
0
 def __init__(self, request, response):
     self.request = request
     self.response = response
     self.location_model = get_class(settings.GEOIP_LOCATION_MODEL)