Skip to content

Commit

Permalink
Use a url panel field to make url clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschuppi81 committed Dec 31, 2024
1 parent 8aaa2d5 commit 77935dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 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

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
10 changes: 6 additions & 4 deletions 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 @@ -778,11 +778,14 @@ class AnalyticsSettingsForm(Form):
render_kw={'rows': 10, 'data-editor': 'html'})

# Points the user to the analytics url e.g. matomo or plausible
analytics_url = StringField(
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:
Expand All @@ -802,8 +805,7 @@ def populate_obj(self, model: 'Organisation') -> None: # type:ignore

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

self.analytics_url.data = self.derive_analytics_url()
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

0 comments on commit 77935dc

Please sign in to comment.