def tp_split_2(config_id):
    """
    Iframed split third party URL which records the second segment of the cookie (used to identify the user)
    Functions similar to tp_split_1 above.
    """
    check_referer(request, URLS['TP_SPLIT_URL_2'])
    config_cookie_length = implicit_user_login(User,
                                               config_id).split_cookie_size

    time.sleep(0.4)

    uuid = get_uuid_from_cookies(request, 'id', config_cookie_length)
    trackable_uuid_2 = SplitTrackableUUID2.get_or_create(
        uuid, config_cookie_length)

    log_site_visit(SplitHistoryBase2, request.referrer, trackable_uuid_2,
                   request.access_route[0])
    redis_register_split('2', request.access_route[0], trackable_uuid_2.uuid,
                         request.referrer)

    response = make_response(
        render_template(THIRD_PARTY_SPLIT_TEMPLATE,
                        current_url=request.url_root,
                        urls=URLS,
                        cookie_id=uuid,
                        safe_referer=request.referrer,
                        index=2,
                        config_id=config_id))
    if uuid is None:
        response = append_cookies(response, 'id', trackable_uuid_2.uuid)
    return response
def tp_split_1(config_id):
    """
    Iframed split third party URL which records the first segment of the cookie (used to identify the user)
    """
    check_referer(request, URLS['TP_SPLIT_URL_1'])

    # Ensures the cookie is of the right length (based on the configuration)
    config_cookie_length = implicit_user_login(User,
                                               config_id).split_cookie_size

    # Gets what the third party perceives to be a first segment of the user (based on the cookie segment it sets)
    uuid = get_uuid_from_cookies(request, 'id', config_cookie_length)
    trackable_uuid_1 = SplitTrackableUUID1.get_or_create(
        uuid, config_cookie_length)

    # Logs the site visited for this segment
    log_site_visit(SplitHistoryBase1, request.referrer, trackable_uuid_1,
                   request.access_route[0])
    redis_register_split('1', request.access_route[0], trackable_uuid_1.uuid,
                         request.referrer)

    response = make_response(
        render_template(THIRD_PARTY_SPLIT_TEMPLATE,
                        current_url=request.url_root,
                        urls=URLS,
                        cookie_id=uuid,
                        safe_referer=request.referrer,
                        index=1,
                        config_id=config_id))
    # Sets a cookie for this segment if not done before
    if uuid is None:
        response = append_cookies(response, 'id', trackable_uuid_1.uuid)
    return response
def tp_split_chain_3(config_id, cookie_id):
    """
    Iframed chained third party URL which records the third segment of the cookie (used to identify the user)
    Functions similar to tp_split_1 above.
    """
    check_referer(request, URLS['TP_SPLIT_URL_3'])
    config_cookie_length = implicit_user_login(User,
                                               config_id).split_cookie_size

    uuid = get_uuid_from_cookies(request, 'id', config_cookie_length)
    trackable_uuid_3 = SplitTrackableUUID3.get_or_create(
        uuid, config_cookie_length)

    # Builds up the cookie by appending the fist and second cookie segments to the third to create a combined string
    if cookie_id != 'None' and uuid is not None:
        cookie_id += uuid

    site = request.args.get('ref', None)

    response = make_response(
        render_template(THIRD_PARTY_SPLIT_CHAIN_TEMPLATE,
                        current_url=request.url_root,
                        urls=URLS,
                        cookie_id=uuid,
                        safe_referer=site,
                        index=3,
                        combined_id=cookie_id,
                        config_id=config_id))
    if uuid is None:
        response = append_cookies(response, 'id', trackable_uuid_3.uuid)
    return response
def tp_split_chain_1(config_id):
    """
    Iframed chained third party URL which records the first segment of the cookie (used to identify the user)
    Functions similar to tp_split_1 above.
    """
    check_referer(request, URLS['TP_SPLIT_URL_1'])
    config_cookie_length = implicit_user_login(User,
                                               config_id).split_cookie_size

    uuid = get_uuid_from_cookies(request, 'id', config_cookie_length)
    trackable_uuid_1 = SplitTrackableUUID1.get_or_create(
        uuid, config_cookie_length)

    response = make_response(
        render_template(THIRD_PARTY_SPLIT_CHAIN_TEMPLATE,
                        current_url=request.url_root,
                        urls=URLS,
                        cookie_id=uuid,
                        safe_referer=request.referrer,
                        index=1,
                        combined_id=uuid,
                        config_id=config_id))
    if uuid is None:
        response = append_cookies(response, 'id', trackable_uuid_1.uuid)
    return response
def tp_split_chain_master(config_id, cookie_id):
    """
    Iframed chained third party URL which records the third segment of the cookie (used to identify the user)
    Functions similar to tp_split_1 above.
    """
    check_referer(request, URLS['TP_SPLIT_URL_4'])
    config_cookie_length = implicit_user_login(User,
                                               config_id).split_cookie_size

    uuid = get_uuid_from_cookies(request, 'id', config_cookie_length)
    trackable_uuid_4 = SplitTrackableUUID4.get_or_create(
        uuid, config_cookie_length)

    trackable = False

    site = unquote(request.args.get('ref', None))

    # Builds up the completed cookie by appending the fist, second and third cookie segments to the fourth to create a
    # unique identifier
    if cookie_id != 'None' and uuid is not None:
        cookie_id += uuid
        trackable = True
        redis_set_benchmark_recent_site(config_id, '4', site)

    # Records the site visited by what the third party perceives as the unique user (from the combined string)
    joined_user = JoinedTrackableUUID.get_or_create(cookie_id)
    log_site_visit(JoinedHistory, site, joined_user, request.referrer)
    recent_history = generate_recent_history(JoinedHistory, joined_user, 10)

    response = make_response(
        render_template(THIRD_PARTY_CHAIN_MASTER_TEMPLATE,
                        current_url=request.url_root,
                        urls=URLS,
                        cookie_id=uuid,
                        history_log=recent_history,
                        index=4,
                        combined_id=cookie_id,
                        trackable=trackable,
                        config_id=config_id))
    # Sets a cookie for this combined string which identifies a user if not done before
    if uuid is None:
        response = append_cookies(response, 'id', trackable_uuid_4.uuid)
    return response