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

Add MOTD about duplication #202

Merged
merged 1 commit into from
May 10, 2023
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
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
8.3.2 (unreleased)
------------------

- Message of the day: Duplication
(`#1137 <https://github.com/syslabcom/scrum/issues/1137>`_).
- Update translations.


Expand Down
108 changes: 90 additions & 18 deletions src/osha/oira/client/browser/dashboard_banner.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,95 @@
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


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'<a class="pat-inject" href="{self.preferences_url}">{link_text}</a>.'
)
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",
},
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, it looks like a dict of dicts would work better:

`duplication-banner` : {...},
`call-for-action-banner` : {...},

This would save you this to cycle over the messages all the time.
E.g.: in [message["disabled_key"] for message in self.all_messages] will become in self.all_messages.

But it looks like also other parts of the code would profit from this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two lines that cycle over the messages like this. One of them is due to the strange situation with the settings. That leaves only one line. I hesitate to restructure the data just for that one line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well as mentioned it is not just that...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You vaguely mentioned it but did not specify anything else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neverm ind :)


@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):
Expand All @@ -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__()
54 changes: 37 additions & 17 deletions src/osha/oira/client/browser/templates/dashboard-banner.pt
Original file line number Diff line number Diff line change
@@ -1,42 +1,62 @@
<div class="canvas-banner"
tal:define="
preferences_url view/preferences_url;
"
id="canvas-banner"
tal:condition="view/available"
i18n:domain="euphorie"
>
<article class="banner-body">
<article class="banner-body"
tal:define="
message view/message;
num_messages python:len(view.messages);
"
>
<a class="pat-inject image-link"
href="${preferences_url}"
href="${message/url}"
>
<img alt="Preferences"
src="${here/portal_url}/++resource++euphorie.resources/oira/style/mail-tunnel.jpg"
<img alt="${message/img_alt}"
src="${here/portal_url}/${message/img_src}"
i18n:attributes="alt"
/>
</a>
<section class="banner-text">
<p i18n:translate="">
Keep updated with the latest developments by signing up for our newsletter on your
<a class="pat-inject"
href="${preferences_url}"
i18n:name="target"
i18n:translate=""
>personal preferences page</a>.
<p class="float-after banner-counter"
tal:condition="python:num_messages &gt; 1"
>
${python:view.message_id + 1}/${num_messages}
</p>
<p tal:content="structure message/text"></p>
<form class="button-bar pat-inject"
action="${here/absolute_url}/@@${view/__name__}#content"
method="post"
>
<a class="pat-inject pat-button default"
href="${preferences_url}"
i18n:translate=""
>Sign up</a>
href="${message/url}"
>${message/button_text}</a>
<button class="pat-button"
name="hide_banner"
type="submit"
value="1"
i18n:translate=""
>Hide this message</button>
<div class="float-after button-set clustered"
tal:define="
previous_id python: (view.message_id - 1) % num_messages;
next_id python: (view.message_id + 1) % num_messages;
"
tal:condition="python:num_messages &gt; 1"
>
<a class="pat-inject pat-button no-label icon icon-angle-left"
href="${here/absolute_url}/@@${view/__name__}?message=${previous_id}#canvas-banner"
i18n:translate="label_previous_tip"
>Previous tip</a>
<a class="pat-inject pat-button no-label icon icon-angle-right"
href="${here/absolute_url}/@@${view/__name__}?message=${next_id}#canvas-banner"
i18n:translate="label_next_tip"
>Next tip</a>
</div>
<input name="value"
type="hidden"
value="${message/disabled_key}"
/>
</form>
</section>
</article>
Expand Down