diff --git a/docs/changes.rst b/docs/changes.rst index 99e1b535..c0e7ccae 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -4,6 +4,8 @@ Changelog 8.3.2 (unreleased) ------------------ +- Message of the day: Duplication + (`#1137 `_). - Update translations. diff --git a/src/osha/oira/client/browser/dashboard_banner.py b/src/osha/oira/client/browser/dashboard_banner.py index 52227e38..6cced2b1 100644 --- a/src/osha/oira/client/browser/dashboard_banner.py +++ b/src/osha/oira/client/browser/dashboard_banner.py @@ -1,6 +1,9 @@ from euphorie.client.country import IClientCountry from euphorie.client.model import get_current_account +from osha.oira import _ from osha.oira.client.model import NewsletterSetting +from plone import api +from plone.memoize.view import memoize from Products.Five import BrowserView from z3c.saconfig import Session @@ -8,7 +11,85 @@ class View(BrowserView): """View for the dashboard banner.""" - _value = "call-for-action-banner-disabled" + @property + @memoize + def webhelpers(self): + return api.content.get_view("webhelpers", self.context, self.request) + + @property + @memoize + def all_messages(self): + country_url = self.webhelpers.country_url + help_language = self.webhelpers.help_language + link_text = api.portal.translate(_("personal preferences page")) + preferences_link = ( + f'{link_text}.' + ) + return [ + { + "img_src": ( + "++resource++euphorie.resources/oira/style/" + "andrea-piacquadio-copier.jpg" + ), + "img_alt": api.portal.translate(_("Photocopier")), + "url": ( + f"{country_url}/++resource++euphorie.resources/oira/help/" + f"{help_language}/pages/3-carrying-out-a-risk-assessment.html" + ), + "button_text": api.portal.translate(_("Learn moreā€¦")), + "text": api.portal.translate( + _( + "Tip: Re-use existing risk assessments with the duplication " + "feature." + ) + ), + "disabled_key": "duplication-banner-disabled", + }, + { + "img_src": "++resource++euphorie.resources/oira/style/mail-tunnel.jpg", + "img_alt": api.portal.translate(_("Preferences")), + "url": self.preferences_url, + "button_text": api.portal.translate(_("Sign up")), + "text": api.portal.translate( + _( + "Keep updated with the latest developments by signing up for " + "our newsletter on your ${target}", + mapping={"target": preferences_link}, + ) + ), + "disabled_key": "call-for-action-banner-disabled", + }, + ] + + @property + @memoize + def messages(self): + account = get_current_account() + if not account: + return [] + + hidden_keys = [ + message.value + for message in Session.query(NewsletterSetting.value).filter( + NewsletterSetting.account_id == account.id, + NewsletterSetting.value.in_( + [message["disabled_key"] for message in self.all_messages] + ), + ) + ] + return [ + message + for message in self.all_messages + if message["disabled_key"] not in hidden_keys + ] + + @property + def message_id(self): + return int(self.request.form.get("message", 0)) + + @property + def message(self): + return self.messages[self.message_id] @property def preferences_url(self): @@ -19,29 +100,20 @@ def preferences_url(self): def available(self): """Check if the user already closed the banner in the past.""" - account = get_current_account() - if not account: - return False - return ( - Session.query(NewsletterSetting) - .filter( - NewsletterSetting.account_id == account.id, - NewsletterSetting.value == self._value, - ) - .count() - == 0 - ) + return self.messages def __call__(self): if ( self.request.method == "POST" and self.request.form.get("hide_banner") == "1" ): - Session.add( - NewsletterSetting( - account_id=get_current_account().getId(), - value=self._value, + value = self.request.form.get("value") + if value in [message["disabled_key"] for message in self.all_messages]: + Session.add( + NewsletterSetting( + account_id=get_current_account().getId(), + value=value, + ) ) - ) self.request.response.redirect(self.context.absolute_url()) return super().__call__() diff --git a/src/osha/oira/client/browser/templates/dashboard-banner.pt b/src/osha/oira/client/browser/templates/dashboard-banner.pt index b6b49fb6..1d5f6dcd 100644 --- a/src/osha/oira/client/browser/templates/dashboard-banner.pt +++ b/src/osha/oira/client/browser/templates/dashboard-banner.pt @@ -1,42 +1,62 @@
-