예제 #1
0
def visit(request, landing_hash):
    redirection = Redirection.all().filter('landing_hash =',
                                           landing_hash).get()
    if redirection:
        # check if the person has already visited
        previous_visit = Visit.all().filter(
            'ip =',
            request.META["REMOTE_ADDR"]).filter('redirection =',
                                                redirection).get()
        if previous_visit and ip_checking:
            # they're going to the place they went last time
            target = previous_visit.target
        else:
            if isinstance(redirection, StratifiedRedirection):
                # find the last visited URL
                this_target_index = (redirection.last_target_index + 1) % len(
                    list(redirection.target_set))
                target = list(
                    redirection.target_set
                )[this_target_index]  # TODO: is order preserved? also, is there a more efficient way?
                redirection.last_target_index = this_target_index
                redirection.put()
            else:
                target = random.choice(list(redirection.target_set))

        # append any URL parameters to the URL
        reconstructed_url = combine_queries(str(target.url), request.GET)

        # record visit
        visit = Visit(redirection=redirection,
                      target=target,
                      ip=request.META["REMOTE_ADDR"])
        visit.put()

        return HttpResponseRedirect(reconstructed_url)
예제 #2
0
def visit(request, landing_hash):
    redirection = Redirection.all().filter('landing_hash =', landing_hash).get()
    if redirection:
        # check if the person has already visited
        previous_visit = Visit.all().filter('ip =', request.META["REMOTE_ADDR"]).filter('redirection =', redirection).get()
        if previous_visit and ip_checking:
            # they're going to the place they went last time
            target = previous_visit.target
        else:
            if isinstance(redirection, StratifiedRedirection):
                # find the last visited URL
                this_target_index = (redirection.last_target_index + 1) % len(list(redirection.target_set))
                target = list(redirection.target_set)[this_target_index] # TODO: is order preserved? also, is there a more efficient way?
                redirection.last_target_index = this_target_index
                redirection.put()
            else:
                target = random.choice(list(redirection.target_set))
        
        # append any URL parameters to the URL
        reconstructed_url = combine_queries(str(target.url), request.GET)
        
        # record visit
        visit = Visit(redirection = redirection, target = target, ip = request.META["REMOTE_ADDR"])
        visit.put()
        
        return HttpResponseRedirect(reconstructed_url)