Skip to content

Commit

Permalink
Settings: Displays the web statistics url
Browse files Browse the repository at this point in the history
TYPE: Feature
LINK: ogc-1639
  • Loading branch information
Tschuppi81 authored Dec 31, 2024
1 parent 95e4fb1 commit 13baf03
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/onegov/form/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from onegov.form import log, _
from onegov.form.utils import path_to_filename
from onegov.form.validators import ValidPhoneNumber
from onegov.form.widgets import ChosenSelectWidget
from onegov.form.widgets import ChosenSelectWidget, LinkPanelWidget
from onegov.form.widgets import HoneyPotWidget
from onegov.form.widgets import IconWidget
from onegov.form.widgets import MultiCheckboxWidget
Expand Down Expand Up @@ -676,6 +676,11 @@ def populate_obj(self, obj: object, name: str) -> None:
pass


class URLPanelField(PanelField):

widget = LinkPanelWidget()


class DateTimeLocalField(DateTimeLocalFieldBase):
""" A custom implementation of the DateTimeLocalField to fix issues with
the format and the datetimepicker plugin.
Expand Down
15 changes: 15 additions & 0 deletions src/onegov/form/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,21 @@ def __call__(self, field: 'PanelField', **kwargs: Any) -> Markup:
)


class LinkPanelWidget(PanelWidget):
""" A widget that displays a clickable link as panel (no input). """

def __call__(self, field: 'PanelField', **kwargs: Any) -> Markup:
text = escape(field.meta.request.translate(field.text))
return Markup( # noqa: RUF035
f'<div class="panel {{kind}}" {html_params(**kwargs)}>'
'<a href="{link}">{text}</a></div>'
).format(
kind=field.kind,
text=text.replace('\n', Markup('<br>')),
link=field.text
)


class HoneyPotWidget(TextInput):
""" A widget that displays the input normally not visible to the user. """

Expand Down
32 changes: 31 additions & 1 deletion src/onegov/org/forms/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from onegov.core.widgets import transform_structure
from onegov.core.widgets import XML_LINE_OFFSET
from onegov.form import Form
from onegov.form.fields import ChosenSelectField
from onegov.form.fields import ChosenSelectField, URLPanelField
from onegov.form.fields import ColorField
from onegov.form.fields import CssField
from onegov.form.fields import MarkupField
Expand Down Expand Up @@ -777,6 +777,36 @@ class AnalyticsSettingsForm(Form):
description=_('JavaScript for web statistics support'),
render_kw={'rows': 10, 'data-editor': 'html'})

# Points the user to the analytics url e.g. matomo or plausible
analytics_url = URLPanelField(
label=_('Analytics URL'),
description=_('URL pointing to the analytics page'),
render_kw={'readonly': True},
validators=[UrlRequired(), Optional()],
text='',
kind='panel',
hide_label=False
)

def derive_analytics_url(self) -> str:
analytics_code = self.analytics_code.data or ''

if 'analytics.seantis.ch' in analytics_code:
data_domain = analytics_code.split(
'data-domain="', 1)[1].split('"', 1)[0]
return f'https://analytics.seantis.ch/{data_domain}'
elif 'matomo' in analytics_code:
return 'https://stats.seantis.ch'
else:
return ''

def populate_obj(self, model: 'Organisation') -> None: # type:ignore
super().populate_obj(model)

def process_obj(self, model: 'Organisation') -> None: # type:ignore
super().process_obj(model)
self.analytics_url.text = self.derive_analytics_url()


class HolidaySettingsForm(Form):

Expand Down
39 changes: 39 additions & 0 deletions tests/onegov/town6/test_views_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,42 @@ def test_general_settings(client):
assert '<style>h2 { text-decoration: underline; }</style>' in page

assert 'class="header-image"' in page


def test_analytics_settings(client):
# plausible
client.login_admin()

code = ('<script defer data-domain="govikon.ch" '
'src="https://analytics.seantis.ch/js/script.js"></script>')
settings = client.get('/analytics-settings')
settings.form['analytics_code'] = code
settings.form.submit()

settings = client.get('/analytics-settings')
assert 'https://analytics.seantis.ch/govikon.ch' in settings

# matomo
code = """
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//stats.seantis.ch/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '28']);
var d=document, g=d.createElement('script');
var s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
"""
settings = client.get('/analytics-settings')
settings.form['analytics_code'] = code
settings.form.submit()

settings = client.get('/analytics-settings')
assert 'https://stats.seantis.ch' in settings

0 comments on commit 13baf03

Please sign in to comment.