backoffice: add feature flag to hide statistics links (#74759) #180

Merged
fpeters merged 1 commits from wip/74759-feature-flag-no-stats into main 2023-03-24 09:11:48 +01:00
2 changed files with 43 additions and 8 deletions

View File

@ -50,6 +50,27 @@ def test_backoffice_statistics_with_no_formdefs(pub):
assert 'This site is currently empty.' in resp
def test_backoffice_statistics_feature_flag(pub):
create_superuser(pub)
FormDef.wipe()
formdef = FormDef()
formdef.name = 'form title'
formdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/management/form-title/')
assert 'Statistics' in resp.text
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'disable-internal-statistics', 'true')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
resp = app.get('/backoffice/management/form-title/')
assert 'Statistics' not in resp.text
def test_backoffice_statistics_status_filter(pub):
create_superuser(pub)
create_environment(pub)
@ -156,6 +177,15 @@ def test_global_statistics(pub):
resp = resp.forms[0].submit()
assert 'Total count: 20' in resp.text
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'disable-internal-statistics', 'true')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
resp = app.get('/backoffice/management/forms')
assert 'Global statistics' not in resp
def test_backoffice_statistics(pub):
create_superuser(pub)

View File

@ -251,11 +251,12 @@ class ManagementDirectory(Directory):
def get_sidebar(self, formdefs):
r = TemplateIO(html=True)
r += self.get_lookup_sidebox()
r += htmltext('<div class="bo-block">')
r += htmltext('<ul id="sidebar-actions">')
r += htmltext('<li class="stats"><a href="statistics">%s</a></li>') % _('Global statistics')
r += htmltext('</ul>')
r += htmltext('</div>')
if not get_publisher().has_site_option('disable-internal-statistics'):
r += htmltext('<div class="bo-block">')
r += htmltext('<ul id="sidebar-actions">')
r += htmltext('<li class="stats"><a href="statistics">%s</a></li>') % _('Global statistics')
r += htmltext('</ul>')
r += htmltext('</div>')
return r.getvalue()
def lookup(self):
@ -867,9 +868,13 @@ class FormPage(FormdefDirectoryBase):
) % (qs, self.export_data_label)
if self.formdef.geolocations:
r += htmltext(' <li><a data-base-href="map" href="map%s">%s</a></li>') % (qs, _('Plot on a Map'))
if 'stats' in self._q_exports and (
not self.formdef.category
or self.formdef.category.has_permission('statistics', get_request().user)
if (
'stats' in self._q_exports
and not get_publisher().has_site_option('disable-internal-statistics')
and (
not self.formdef.category
or self.formdef.category.has_permission('statistics', get_request().user)
)
):
r += htmltext(' <li class="stats"><a href="stats">%s</a></li>') % _('Statistics')