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

Adding the contact link #1650

Merged
merged 12 commits into from
Feb 11, 2025
12 changes: 10 additions & 2 deletions src/onegov/org/assets/js/many.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,17 @@ function extractType(target) {
}

jQuery.fn.many = function() {
// Don't forget to change this file in town6 as well.
return this.each(function() {

var target = $(this);
var type = extractType(target);
var data = JSON.parse(target.val());
try {
var data = JSON.parse(target.val());
} catch (e) {
console.log('Failed to parse JSON:', e);
return; // Skip this iteration if JSON is invalid
}
var label = target.closest('label');
var errors = label.siblings('.error');

Expand All @@ -470,7 +476,9 @@ jQuery.fn.many = function() {
label.hide();
errors.hide();

var el = $('<div class="many-wrapper" />');
// Create a unique wrapper for this instance
let wrapperId = 'many-wrapper-' + Math.random().toString(36).substr(2, 9);
let el = $('<div class="many-wrapper" id="' + wrapperId + '" />');
el.appendTo(label.parent());

// transfer javascript dependencies to the wrapper
Expand Down
42 changes: 19 additions & 23 deletions src/onegov/org/locale/de_CH/LC_MESSAGES/onegov.org.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE 1.0\n"
"POT-Creation-Date: 2025-01-30 15:51+0100\n"
"POT-Creation-Date: 2025-01-28 17:16+0100\n"
"PO-Revision-Date: 2022-03-15 10:21+0100\n"
"Last-Translator: Marc Sommerhalder <[email protected]>\n"
"Language-Team: German\n"
Expand Down Expand Up @@ -2808,6 +2808,24 @@ msgstr ""
msgid "Sidebar links"
msgstr "Links in der Sidebar"

msgid "Contact link"
msgstr "Kontaktlink"

msgid "Sidebar contact"
msgstr "Kontakt in der Sidebar"

msgid "Please provide a URL if contact link text is set"
msgstr "Bitte geben Sie eine URL an, wenn der Kontaktlink-Text gesetzt ist"

msgid "Please provide link text if contact URL is set"
msgstr "Bitte geben Sie einen Link-Text an, wenn die Kontakt-URL gesetzt ist"

msgid "Contact Text"
msgstr "Kontakttext"

msgid "Contact URL"
msgstr "Kontakt-URL"

msgid "Delete content when expired"
msgstr "Inhalt löschen nachdem abgelaufen"

Expand Down Expand Up @@ -6296,25 +6314,3 @@ msgstr "Ein Konto wurde für Sie erstellt"

msgid "The user was created successfully"
msgstr "Der Benutzer wurde erfolgreich erstellt"

#~ msgid "Photo album. Will be shown at the end of content."
#~ msgstr "Fotoalbum. Wird am Ende des Inhalts angezeigt."

#~ msgid "Describes in detail how this form is to be used"
#~ msgstr "Beschreibt detailliert wie dieses Formular benutzt werden soll"

#~ msgid "Include logo in newsletter"
#~ msgstr "Logo im Newsletter anzeigen"

#~ msgid "Newsletter Subscription"
#~ msgstr "Newsletter-Abonnement"

#~ msgid ""
#~ "Thank you for filling out this survey. If there's anything you'd like to "
#~ "change, click on \"Edit\" below."
#~ msgstr ""
#~ "Vielen Dank für Ihre Teilnahme an dieser Umfrage. Falls Sie noch etwas "
#~ "ändern möchten, klicken Sie auf den \"Bearbeiten\"-Button unten."

#~ msgid "Rule updated"
#~ msgstr "Regel aktualisiert"
21 changes: 18 additions & 3 deletions src/onegov/org/locale/fr_CH/LC_MESSAGES/onegov.org.po
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,24 @@ msgstr ""
msgid "Sidebar links"
msgstr "Liens dans la barre latérale"

msgid "Contact link"
msgstr "Lien de contact"

msgid "Sidebar contact"
msgstr "Contact dans la barre latérale"

msgid "Please provide a URL if contact link text is set"
msgstr "Veuillez fournir une URL si le texte du lien de contact est défini"

msgid "Please provide link text if contact URL is set"
msgstr "Veuillez indiquer le texte du lien si l'URL de contact est définie"

msgid "Contact Text"
msgstr "Lien de contact"

msgid "Contact URL"
msgstr "URL de contact"

msgid "Delete content when expired"
msgstr "Supprimer le contenu lorsqu'il est expiré"

Expand Down Expand Up @@ -6312,6 +6330,3 @@ msgstr "Un compte a été créé pour vous"

msgid "The user was created successfully"
msgstr "L'utilisateur a bien été créé"

#~ msgid "Photo album. Will be shown at the end of content."
#~ msgstr "Album photo. Sera affiché à la fin du contenu."
21 changes: 18 additions & 3 deletions src/onegov/org/locale/it_CH/LC_MESSAGES/onegov.org.po
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,24 @@ msgstr ""
msgid "Sidebar links"
msgstr "Collegamenti della barra laterale"

msgid "Contact link"
msgstr "Link di contatto"

msgid "Sidebar contact"
msgstr "Contatto laterale"

msgid "Please provide a URL if contact link text is set"
msgstr "Si prega di fornire un URL se il testo del link di contatto è impostato"

msgid "Please provide link text if contact URL is set"
msgstr "Fornire il testo del link se l'URL di contatto è impostato"

msgid "Contact Text"
msgstr "Testo di contatto"

msgid "Contact URL"
msgstr "URL di contatto"

msgid "Delete content when expired"
msgstr "Eliminare il contenuto quando è scaduto"

Expand Down Expand Up @@ -6300,6 +6318,3 @@ msgstr "È stato creato un account per te"

msgid "The user was created successfully"
msgstr "Utente creato correttamente"

#~ msgid "Photo album. Will be shown at the end of content."
#~ msgstr "Album fotografico. Sarà mostrato alla fine del contenuto."
109 changes: 109 additions & 0 deletions src/onegov/org/models/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,115 @@
return SidebarLinksForm


class SidebarContactLinkExtension(ContentExtension):
""" Like SidebarLinkExtension but the links are shown below the contact
field. We knowingly duplicate some code here .
"""

sidepanel_contact = content_property()

def extend_form(
self,
form_class: type[FormT],
request: OrgRequest
) -> type[FormT]:

class SidebarContactLinkForm(form_class): # type:ignore

sidepanel_contact = StringField(
label=_('Contact link'),
fieldset=_('Sidebar contact'),
render_kw={'class_': 'many many-links'}
)

if TYPE_CHECKING:
contact_errors: dict[int, str]
else:
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.contact_errors = {}

def on_request(self) -> None:
if not self.sidepanel_contact.data:
self.sidepanel_contact.data = self.links_to_json(None)

def process_obj(self, obj: SidebarContactLinkExtension) -> None:
super().process_obj(obj)
if not obj.sidepanel_contact:
self.sidepanel_contact.data = self.links_to_json(None)
else:
self.sidepanel_contact.data = self.links_to_json(

Check warning on line 1127 in src/onegov/org/models/extensions.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/models/extensions.py#L1127

Added line #L1127 was not covered by tests
obj.sidepanel_contact
)

def populate_obj(
self,
obj: SidebarContactLinkExtension,
*args: Any, **kwargs: Any
) -> None:
super().populate_obj(obj, *args, **kwargs)
obj.sidepanel_contact = self.json_to_links(
self.sidepanel_contact.data) or None

def validate_sidepanel_contact(self, field: StringField) -> None:
for text, link in self.json_to_links(
self.sidepanel_contact.data
):
if text and not link:
raise ValidationError(

Check warning on line 1145 in src/onegov/org/models/extensions.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/models/extensions.py#L1144-L1145

Added lines #L1144 - L1145 were not covered by tests
_('Please provide a URL if contact link text is '
'set'))
if link and not text:
raise ValidationError(

Check warning on line 1149 in src/onegov/org/models/extensions.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/models/extensions.py#L1148-L1149

Added lines #L1148 - L1149 were not covered by tests
_('Please provide link text if contact URL is '
'set'))
if link and not re.match(r'^(http://|https://|/)',

Check warning on line 1152 in src/onegov/org/models/extensions.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/models/extensions.py#L1152

Added line #L1152 was not covered by tests
link):
raise ValidationError(

Check warning on line 1154 in src/onegov/org/models/extensions.py

View check run for this annotation

Codecov / codecov/patch

src/onegov/org/models/extensions.py#L1154

Added line #L1154 was not covered by tests
_('Your URLs must start with http://,'
' https:// or /'
' (for internal links)')
)

def json_to_links(
self,
text: str | None = None
) -> list[tuple[str | None, str | None]]:

if not text:
return []

return [
(value['text'], link)
for value in json.loads(text).get('values', [])
if (link := value['link']) or value['text']
]

def links_to_json(
self,
links: Sequence[tuple[str | None, str | None]] | None
) -> str:
contact_links = links or []

return json.dumps({
'labels': {
'text': self.request.translate(_('Contact Text')),
'link': self.request.translate(_('Contact URL')),
'add': self.request.translate(_('Add')),
'remove': self.request.translate(_('Remove')),
},
'values': [
{
'text': l[0],
'link': l[1],
'error': self.contact_errors.get(ix, '')
} for ix, l in enumerate(contact_links)
]
})

return SidebarContactLinkForm


class DeletableContentExtension(ContentExtension):
""" Extends any class that has a meta dictionary field with the ability to
mark the content as deletable after reaching the end date. A cronjob will
Expand Down
4 changes: 2 additions & 2 deletions src/onegov/org/models/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ContactExtension, ContactHiddenOnPageExtension,
PeopleShownOnMainPageExtension, ImageExtension,
NewsletterExtension, PublicationExtension, DeletableContentExtension,
InlinePhotoAlbumExtension
InlinePhotoAlbumExtension, SidebarContactLinkExtension
)
from onegov.org.models.extensions import AccessExtension
from onegov.org.models.extensions import CoordinatesExtension
Expand Down Expand Up @@ -44,7 +44,7 @@ class Topic(Page, TraitInfo, SearchableContent, AccessExtension,
PeopleShownOnMainPageExtension, PersonLinkExtension,
CoordinatesExtension, ImageExtension,
GeneralFileLinkExtension, SidebarLinksExtension,
InlinePhotoAlbumExtension):
SidebarContactLinkExtension, InlinePhotoAlbumExtension):

__mapper_args__ = {'polymorphic_identity': 'topic'}

Expand Down
2 changes: 2 additions & 0 deletions src/onegov/org/theme/styles/org.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6777,3 +6777,5 @@ a#results {
list-style: none;
}
}


15 changes: 13 additions & 2 deletions src/onegov/town6/assets/js/many.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var ManyFields = React.createClass({
onChange={this.props.onChange}
/>
}

{
this.props.type === "opening-hours" &&
<ManyOpeningHours
Expand Down Expand Up @@ -634,11 +635,17 @@ function extractType(target) {
}

jQuery.fn.many = function() {
// Don't forget to change this file in org as well.
return this.each(function() {

var target = $(this);
var type = extractType(target);
var data = JSON.parse(target.val());
try {
var data = JSON.parse(target.val());
} catch (e) {
console.log('Failed to parse JSON:', e);
return; // Skip this iteration if JSON is invalid
}
var label = target.closest('label');
var errors = label.siblings('.error');

Expand All @@ -649,8 +656,11 @@ jQuery.fn.many = function() {
'position': 'absolute'
});
label.hide();
errors.hide();

var el = $('<div class="many-wrapper" />');
// Create a unique wrapper for this instance
let wrapperId = 'many-wrapper-' + Math.random().toString(36).substr(2, 9);
let el = $('<div class="many-wrapper" id="' + wrapperId + '" />');
el.appendTo(label.parent());

// transfer javascript dependencies to the wrapper
Expand All @@ -675,5 +685,6 @@ jQuery.fn.many = function() {
});
};


// since we intercept the dependency setup we need to run before document.ready
$('.many').many();
12 changes: 9 additions & 3 deletions src/onegov/town6/templates/macros.pt
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,20 @@
</metal:empty_lead_hint>

<metal:page_content define-macro="page_content_sidebar" i18n:domain="onegov.town6">
<div class="small-12 medium-4 cell sidebar medium-offset-1 ${'filter-panel' if filter_panel else ''}" tal:condition="people or contact or coordinates or files or show_side_panel or more_entries|False or sidepanel_links|False" tal:define="files files|None; highlight_contacts highlight_contacts|True; filter_panel filter_panel|False">
<div class="small-12 medium-4 cell sidebar medium-offset-1 ${'filter-panel' if filter_panel else ''}" tal:condition="people or contact or coordinates or files or show_side_panel or more_entries|False or sidepanel_links|False" tal:define="files files|None; highlight_contacts highlight_contacts|True; filter_panel filter_panel|False; sidepanel_contact page.sidepanel_contact|None;">
<div class="sidebar-wrapper">
<div metal:define-slot="before-panels"></div>
<div tal:define="hide page.hide_contact|hide_contact|False" class="side-panel contact-panel" tal:condition="not hide and contact or coordinates or location">
<div tal:condition="contact and not hide">
<h3 i18n:translate>Contact</h3>
<tal:b content="contact" />
<div tal:condition="sidepanel_contact|False">
<ul class="sidebar-links">
<li tal:repeat="contact sidepanel_contact" tal:condition="contact">
<a href="${contact[1]}">${contact[0]}</a>
</li>
</ul>
</div>
</div>
<br tal:condition="coordinates and contact" />
<h3 i18n:translate tal:condition="not contact">Map</h3>
Expand Down Expand Up @@ -821,7 +828,7 @@
</metal:page_content>

<metal:page_content define-macro="page_content" i18n:domain="onegov.town6">
<div class="grid-x grid-padding-x wrapper" tal:define="show_side_panel show_side_panel|False; location location|False; show_page_image page.show_page_image|None; files files|None; sidepanel_links sidepanel_links|None; news page.type == 'news'|False; imageset page.photo_album|None; as_content layout.org.theme_options.get('page-image-position', 'as_content') == 'as_content';">
<div class="grid-x grid-padding-x wrapper" tal:define="show_side_panel show_side_panel|False; location location|False; show_page_image page.show_page_image|None; files files|None; sidepanel_links sidepanel_links|None; news page.type == 'news'|False; imageset page.photo_album|None; as_content layout.org.theme_options.get('page-image-position', 'as_content') == 'as_content'; sidepanel_contact page.sidepanel_contact|None;">
<tal:b condition="not: people or files or contact or coordinates or location or show_side_panel or more_entries|False">
<div class="small-12 cell page-content-main">
<img class="page-image" tal:condition="show_page_image and page.page_image and (as_content or news)" src="${page.page_image}">
Expand Down Expand Up @@ -852,7 +859,6 @@

<div metal:define-slot="after-text"></div>


<div class="people-panel" tal:condition="people and show_people_on_main_page" tal:define="show_people_on_main_page page.show_people_on_main_page|resource.show_people_on_main_page|False; western_name_order page.western_name_order|resource.western_name_order|False">
<h3 i18n:translate>People</h3>
<ul data-sortable data-sortable-url="${layout.move_person_url_template}">
Expand Down
2 changes: 1 addition & 1 deletion src/onegov/town6/templates/news.pt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
</div>

<tal:b metal:use-macro="layout.macros.page_content" tal:condition="page.parent"
tal:define="lead page.content.get('lead');text page.text|None; people page.people; contact None if children else layout.contact_html; coordinates page.coordinates; files page.files; sidepanel_links page.sidepanel_links|False; imageset page.photo_album|False;" />
tal:define="lead page.content.get('lead');text page.text|None; people page.people; contact None if children else layout.contact_html; coordinates page.coordinates; files page.files; sidepanel_links page.sidepanel_links|False; imageset page.photo_album|False; sidepanel_contact page.sidepanel_contact|None; " />

</tal:b>

Expand Down
Loading