Ejemplo n.º 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.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
Ejemplo n.º 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.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
Ejemplo n.º 3
0
    def process_response(self, request, response):
        """ Do nothing, if process_request never completed (redirect)"""
        if not hasattr(request, 'location'):
            return response

        storage = storage_class(request=request, response=response)
        try:
            storage.set(location=request.location)
        except ValueError:
            # bad location_id
            pass
        return response
Ejemplo n.º 4
0
    def process_response(self, request, response):
        """ Do nothing, if process_request never completed (redirect)"""
        if not hasattr(request, 'location'):
            return response

        storage = storage_class(request=request, response=response)
        try:
            storage.set(location=request.location)
        except ValueError:
            # bad location_id
            pass
        return response