<commit_before>import requests

from django.http import HttpResponse
from django.template.loader import render_to_string
from corehq import toggles
from corehq.apps.domain.decorators import login_and_domain_required
from corehq.apps.locations.models import SQLLocation
from . import settings
from .exceptions import TableauTokenException


@toggles.ICDS_REPORTS.required_decorator()
@login_and_domain_required
def tableau(request, domain, workbook, worksheet):
    # TODO: In production we should limit this to only the actual workbook, but this makes iteration much easier
    context = {
        'report_workbook': workbook,
        'report_worksheet': worksheet,
        'debug': request.GET.get('debug', False),
    }

    # set report view-by level based on user's location level
    couch_user = getattr(request, 'couch_user', None)
    location_type_name, location_name = _get_user_location(couch_user, domain)
    context.update({
        'view_by': location_type_name,
        'view_by_value': location_name,
    })

    # set Tableau access URL
    if request.GET.get('with_login', False):
        # TABLEAU_VIEW_URL requires user to login before accessing views
        tableau_access_url = settings.TABLEAU_VIEW_URL
    else:
        # generate a login free access URL
        # This doesn't work on local as this header is added only by ngnix proxy
        client_ip = request.META.get('X-Forwarded-For', '')
        tableau_access_url = get_tableau_trusted_url(client_ip)
    context.update({
        'tableau_access_url': tableau_access_url,
    })

    response = render_to_string('tableau.html', context)
    return HttpResponse(response)


def _get_user_location(user, domain):
    '''
    Takes a couch_user and returns that users level in the heirarchy and the location name
    '''
    # TODO: This needs to handle the top level where someone can see everything
    try:<commit_msg>flip tableau URL behaviour<commit_after>import requests

from django.http import HttpResponse
from django.template.loader import render_to_string
from corehq import toggles
from corehq.apps.domain.decorators import login_and_domain_required
from corehq.apps.locations.models import SQLLocation
from . import settings
from .exceptions import TableauTokenException


@toggles.ICDS_REPORTS.required_decorator()
@login_and_domain_required
def tableau(request, domain, workbook, worksheet):
    # TODO: In production we should limit this to only the actual workbook, but this makes iteration much easier
    context = {
        'report_workbook': workbook,
        'report_worksheet': worksheet,
        'debug': request.GET.get('debug', False),
    }

    # set report view-by level based on user's location level
    couch_user = getattr(request, 'couch_user', None)
    location_type_name, location_name = _get_user_location(couch_user, domain)
    context.update({
        'view_by': location_type_name,
        'view_by_value': location_name,
    })

    # set Tableau access URL
    if request.GET.get('login_less', False):
        # generate a login free access URL
        # This doesn't work on local as this header is added only by ngnix proxy
        client_ip = request.META.get('X-Forwarded-For', '')
        tableau_access_url = get_tableau_trusted_url(client_ip)
    else:
        # TABLEAU_VIEW_URL requires user to login before accessing views
        tableau_access_url = settings.TABLEAU_VIEW_URL

    context.update({
        'tableau_access_url': tableau_access_url,
    })

    response = render_to_string('tableau.html', context)
    return HttpResponse(response)


def _get_user_location(user, domain):
    '''
    Takes a couch_user and returns that users level in the heirarchy and the location name
    '''
    # TODO: This needs to handle the top level where someone can see everything
    try:
{'fraction_non_alphanumeric': 0.07382198952879582, 'fraction_numerical': 0.0, 'mean_word_length': 3.782227784730914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4398929', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1098, 'n_words': 334}