Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 3
0
def create_randomization(request, d):
    # input schema:
    # {'experiment_name': str, 'targets': ['url1', 'url2', 'url3', ...], 'stratify': bool}
    

    if len(d['targets']) == 0:
        return HttpResponseNotFound('You must enter at least one URL.')
        
    redirection = Redirection(experiment_name = d['experiment_name'],
            landing_hash = create_hash(),
            admin_hash = create_hash(),
            is_stratified = d.get('stratify') == True)
    redirection.save()
    for url in d["targets"]:
        try:
            target = Target(url = url, redirection = redirection)
            target.save()
        except forms.ValidationError:
            return HttpResponseNotFound('One or more of the target URLs you entered were malformed.')
    return {'landing_url': make_landing_url(redirection.landing_hash),
            'admin_url': make_admin_url(redirection.admin_hash)}
Exemplo n.º 4
0
def admin(request, admin_hash):
    redirection = Redirection.all().filter('admin_hash =', admin_hash).get()
    if redirection:
        # targets should be a list of dicts with stats on the different targets.
        if isinstance(redirection, StratifiedRedirection):
            method = 'Stratify'
        else:
            method = 'Randomize'
        targets = []
        for target in list(redirection.target_set):
            num_visits = Visit.all().filter('redirection =', redirection).filter('target =', target).count()
            targets.append({'url': target.url, 'num_visits': num_visits})
        
        return render_to_response('admin.html',
            {'method': method,
            'targets': targets,
            'landing_url': make_landing_url(redirection.landing_hash)})
Exemplo n.º 5
0
def admin(request, admin_hash):
    redirection = Redirection.all().filter('admin_hash =', admin_hash).get()
    if redirection:
        # targets should be a list of dicts with stats on the different targets.
        if isinstance(redirection, StratifiedRedirection):
            method = 'Stratify'
        else:
            method = 'Randomize'
        targets = []
        for target in list(redirection.target_set):
            num_visits = Visit.all().filter('redirection =',
                                            redirection).filter(
                                                'target =', target).count()
            targets.append({'url': target.url, 'num_visits': num_visits})

        return render_to_response(
            'admin.html', {
                'method': method,
                'targets': targets,
                'landing_url': make_landing_url(redirection.landing_hash)
            })