Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds string field for web statistics url #1638

Merged
merged 4 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
)


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

Check warning on line 527 in src/onegov/form/widgets.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/form/widgets.py#L526-L527

Added lines #L526 - L527 were not covered by tests
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 @@
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 ''

Check warning on line 792 in src/onegov/org/forms/settings.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/forms/settings.py#L792

Added line #L792 was not covered by tests

if 'analytics.seantis.ch' in analytics_code:
data_domain = analytics_code.split(

Check warning on line 795 in src/onegov/org/forms/settings.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/forms/settings.py#L794-L795

Added lines #L794 - L795 were not covered by tests
'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'

Check warning on line 799 in src/onegov/org/forms/settings.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/forms/settings.py#L797-L799

Added lines #L797 - L799 were not covered by tests
else:
return ''

Check warning on line 801 in src/onegov/org/forms/settings.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/forms/settings.py#L801

Added line #L801 was not covered by tests

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

Check warning on line 804 in src/onegov/org/forms/settings.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/forms/settings.py#L804

Added line #L804 was not covered by tests

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

Check warning on line 808 in src/onegov/org/forms/settings.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/forms/settings.py#L807-L808

Added lines #L807 - L808 were not covered by tests


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
Loading