Skip to content

Commit

Permalink
Merge pull request #34970 from dimagi/ad/GA-embed-tableau-by-user-toggle
Browse files Browse the repository at this point in the history
Migrate "EMBED_TABLEAU_REPORT_BY_USER" toggle to project-level setting
  • Loading branch information
AddisonDunn authored Aug 19, 2024
2 parents f60f9e9 + 9e1785c commit 10485b3
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
14 changes: 14 additions & 0 deletions corehq/apps/reports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ class TableauServerForm(forms.Form):
label=_('Target Site'),
)

get_reports_using_role = forms.BooleanField(
label=_("Get Tableau reports using role name"),
required=False,
help_text=_("If checked, CommCareHQ will request embedded reports from Tableau Server using the user's "
"role name instead of the user's username (e.g. \"HQ/Field Implementer\" instead of "
"\"HQ/[email protected]\")")
)

tableau_groups_allowed = forms.MultipleChoiceField(
label=_("Allowed Tableau Groups"),
choices=[],
Expand All @@ -331,6 +339,7 @@ class Meta:
'server_name',
'validate_hostname',
'target_site',
'get_reports_using_role'
'tableau_groups_allowed'
]

Expand All @@ -355,6 +364,9 @@ def __init__(self, data, user_syncing_config={}, *args, **kwargs):
crispy.Div(
crispy.Field('target_site'),
),
crispy.Div(
crispy.Field('get_reports_using_role'),
),
FormActions(
crispy.Submit('submit_btn', 'Submit')
)
Expand Down Expand Up @@ -395,13 +407,15 @@ def initial_data(self):
'server_name': self._existing_config.server_name,
'validate_hostname': self._existing_config.validate_hostname,
'target_site': self._existing_config.target_site,
'get_reports_using_role': self._existing_config.get_reports_using_role,
'allowed_tableau_groups': self._existing_config.allowed_tableau_groups,
}

def save(self):
self._existing_config.server_type = self.cleaned_data['server_type']
self._existing_config.server_name = self.cleaned_data['server_name']
self._existing_config.validate_hostname = self.cleaned_data['validate_hostname']
self._existing_config.get_reports_using_role = self.cleaned_data['get_reports_using_role']
self._existing_config.target_site = self.cleaned_data['target_site']
if self.add_allowed_tableau_groups_field:
self._existing_config.allowed_tableau_groups = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.14 on 2024-08-13 19:14

from django.db import migrations, models
from corehq.util.django_migrations import skip_on_fresh_install


@skip_on_fresh_install
def _move_ff_to_setting(apps, schema_editor):
try:
from corehq.toggles import EMBED_TABLEAU_REPORT_BY_USER
from corehq.toggles import EMBEDDED_TABLEAU
except ImportError:
return
TableauServer = apps.get_model('reports', 'TableauServer')
for domain in EMBEDDED_TABLEAU.get_enabled_domains():
if not EMBED_TABLEAU_REPORT_BY_USER.enabled(domain):
try:
server = TableauServer.objects.get(domain=domain)
except TableauServer.DoesNotExist:
pass
else:
server.get_reports_using_role = True
server.save()


def noop(*args, **kwargs):
pass


class Migration(migrations.Migration):

dependencies = [
('reports', '0019_tableauvisualization_location_safe'),
]

operations = [
migrations.AddField(
model_name='tableauserver',
name='get_reports_using_role',
field=models.BooleanField(default=False),
),
migrations.RunPython(_move_ff_to_setting, reverse_code=noop),
]
1 change: 1 addition & 0 deletions corehq/apps/reports/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class TableauServer(models.Model):
validate_hostname = models.CharField(max_length=128, default='', blank=True)
target_site = models.CharField(max_length=64, default='Default')
allowed_tableau_groups = ArrayField(models.CharField(max_length=255), null=True, blank=True, default=list)
get_reports_using_role = models.BooleanField(default=False)

def __str__(self):
return '{domain} {server} {server_type} {site}'.format(domain=self.domain,
Expand Down
10 changes: 5 additions & 5 deletions corehq/apps/reports/standard/tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import requests

from corehq import toggles
from corehq.apps.reports.models import TableauVisualization
from corehq.apps.reports.models import TableauServer, TableauVisualization
from corehq.apps.domain.decorators import login_and_domain_required
from corehq.apps.domain.views.base import BaseDomainView
from corehq.apps.locations.permissions import location_safe
Expand Down Expand Up @@ -90,12 +90,12 @@ def tableau_visualization_ajax(request, domain):
id=visualization_data.pop('viz_id'))):
raise Http403

if toggles.EMBED_TABLEAU_REPORT_BY_USER.enabled(domain):
if TableauServer.objects.get(domain=domain).get_reports_using_role:
tableau_username = f"HQ/{request.couch_user.get_role(domain).name}"
else:
# An equivalent Tableau user with the username "HQ/{username}" must exist.
tableau_username = f"HQ/{request.couch_user.username}"
else:
# An equivalent Tableau user with the username "HQ/{role name}" must exist.
tableau_username = f"HQ/{request.couch_user.get_role(domain).name}"

tabserver_url = 'https://{}/trusted/'.format(server_name)
post_arguments = {'username': tableau_username}

Expand Down
8 changes: 3 additions & 5 deletions corehq/toggles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2485,11 +2485,9 @@ def _commtrackify(domain_name, toggle_is_enabled):
EMBED_TABLEAU_REPORT_BY_USER = StaticToggle(
'embed_tableau_report_by_user',
'Use a Tableau username "HQ/{username}" to embed reports instead of "HQ/{role name}"',
TAG_INTERNAL,
TAG_DEPRECATED,
namespaces=[NAMESPACE_DOMAIN],
description='By default, a Tableau username "HQ/{role name}" is sent to Tableau to get the embedded report. '
'Turn on this flag to instead send "HQ/{the user\'s HQ username}", i.e. "HQ/[email protected]", '
'to Tableau to get the embedded report.',
description='This flag is now deprecated and will be removed.',
parent_toggles=[EMBEDDED_TABLEAU]
)

Expand All @@ -2510,7 +2508,7 @@ def _commtrackify(domain_name, toggle_is_enabled):
Each time a user is added/deleted/updated on HQ, an equivalent Tableau user with the username "HQ/{username}"
will be added/deleted/updated on the linked Tableau server.
""",
parent_toggles=[EMBED_TABLEAU_REPORT_BY_USER],
parent_toggles=[EMBEDDED_TABLEAU],
help_link='https://confluence.dimagi.com/display/USH/Tableau+User+Syncing',
)

Expand Down
1 change: 1 addition & 0 deletions migrations.lock
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ reports
0017_alter_tableauuser_tableau_user_id
0018_alter_tableauuser_role
0019_tableauvisualization_location_safe
0020_tableauserver_get_reports_using_role
saved_reports
0001_initial
0002_scheduledreportlog
Expand Down

0 comments on commit 10485b3

Please sign in to comment.