-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from euphorie/scrum-1621-mailing-lists-langua…
…ge-specific Mailing lists: Language specific lists for countries
- Loading branch information
Showing
7 changed files
with
268 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Make sure sqlalchemy subscribers are initialized | ||
from .subscribers import update_user_languages_subscriber # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from euphorie.client.model import get_current_account | ||
from euphorie.client.model import SurveySession | ||
from osha.oira.client.model import NewsletterSetting | ||
from plone import api | ||
from sqlalchemy import event | ||
from z3c.saconfig import Session | ||
|
||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def update_user_languages(account_id, tool): | ||
if not tool: | ||
return | ||
if not tool.language: | ||
logger.info("No language for tool %s", "/".join(tool.getPhysicalPath())) | ||
return | ||
language = tool.language.split("-")[0] | ||
if ( | ||
Session.query(NewsletterSetting.value) | ||
.filter(NewsletterSetting.account_id == account_id) | ||
.filter(NewsletterSetting.value == f"language:{language}") | ||
).count() == 0: | ||
Session.add( | ||
NewsletterSetting( | ||
account_id=account_id, | ||
value=f"language:{language}", | ||
) | ||
) | ||
|
||
|
||
@event.listens_for(SurveySession, "init") | ||
def update_user_languages_subscriber(target, args, kwargs): | ||
if "zodb_path" not in kwargs: | ||
return | ||
# kwargs["account_id"] can refer to the account of the session being cloned | ||
account = get_current_account() | ||
if not account: | ||
return | ||
client = api.portal.get().client | ||
tool = client.restrictedTraverse(str(kwargs["zodb_path"]), None) | ||
update_user_languages(account.id, tool) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from euphorie.client.interfaces import IClientSkinLayer | ||
from euphorie.client.model import Account | ||
from euphorie.client.model import SurveySession | ||
from euphorie.client.tests.utils import addSurvey | ||
from euphorie.content.tests.utils import BASIC_SURVEY | ||
from euphorie.testing import EuphorieIntegrationTestCase | ||
from osha.oira.client.browser.client import GroupToAddresses | ||
from osha.oira.client.browser.client import MailingListsJson | ||
from osha.oira.client.model import NewsletterSubscription | ||
from plone import api | ||
from z3c.saconfig import Session | ||
from zope.interface import alsoProvides | ||
|
||
|
||
class TestMailingLists(EuphorieIntegrationTestCase): | ||
def setUp(self): | ||
super().setUp() | ||
survey = """<sector xmlns="http://xml.simplon.biz/euphorie/survey/1.0"> | ||
<title>Test</title> | ||
<survey> | ||
<title>Second Survey</title> | ||
<language>fr</language> | ||
</survey> | ||
</sector>""" | ||
with api.env.adopt_user("admin"): | ||
addSurvey(self.portal, BASIC_SURVEY) | ||
addSurvey(self.portal, survey) | ||
self.portal.client.nl.ict["software-development"].reindexObject() | ||
self.portal.client.nl.test["second-survey"].reindexObject() | ||
|
||
def test_mailing_lists(self): | ||
request = self.request.clone() | ||
request.form = {"user_id": "admin"} | ||
view = MailingListsJson(context=self.portal.client, request=request) | ||
results = view.results | ||
self.assertIn({"id": "general|QWxsIHVzZXJz", "text": "All users"}, results) | ||
self.assertIn( | ||
{ | ||
"id": "nl-nl|VGhlIE5ldGhlcmxhbmRzIChubCk=", | ||
"text": "The Netherlands (nl)", | ||
}, | ||
results, | ||
) | ||
self.assertIn( | ||
{ | ||
"id": "nl-fr|VGhlIE5ldGhlcmxhbmRzIChmcik=", | ||
"text": "The Netherlands (fr)", | ||
}, | ||
results, | ||
) | ||
self.assertIn( | ||
{ | ||
"id": "nl/ict/software-development|U29mdHdhcmUgZGV2ZWxvcG1lbnQ=", | ||
"text": "Software development (nl/ict/software-development)", | ||
}, | ||
results, | ||
) | ||
|
||
def test_group_to_addresses(self): | ||
user = Account(loginname="[email protected]", password="secret") | ||
Session.add(user) | ||
alsoProvides(self.request, IClientSkinLayer) | ||
with api.env.adopt_user("[email protected]"): | ||
survey_session = SurveySession( | ||
id=1, | ||
title="Software", | ||
zodb_path="nl/ict/software-development", | ||
account=user, | ||
) | ||
Session.add(survey_session) | ||
Session.add( | ||
NewsletterSubscription( | ||
account_id=user.id, | ||
zodb_path="nl", | ||
) | ||
) | ||
Session.add( | ||
NewsletterSubscription( | ||
account_id=user.id, | ||
zodb_path="nl/ict/software-development", | ||
) | ||
) | ||
Session.flush() | ||
|
||
request = self.request.clone() | ||
request.form = {"groups": "nl-nl"} | ||
view = GroupToAddresses(context=self.portal.client, request=request) | ||
results = view.results | ||
self.assertIn("[email protected]", results) | ||
|
||
request = self.request.clone() | ||
request.form = {"groups": "nl-fr"} | ||
view = GroupToAddresses(context=self.portal.client, request=request) | ||
results = view.results | ||
self.assertNotIn("[email protected]", results) | ||
|
||
request = self.request.clone() | ||
request.form = {"groups": "nl/ict/software-development"} | ||
view = GroupToAddresses(context=self.portal.client, request=request) | ||
results = view.results | ||
self.assertIn("[email protected]", results) |
Empty file.
49 changes: 49 additions & 0 deletions
49
src/osha/oira/upgrade/v1/20240111103145_set_newsletter_languages_per_user/upgrade.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from collections import defaultdict | ||
from euphorie.client.model import Account | ||
from euphorie.client.model import SurveySession | ||
from ftw.upgrade import UpgradeStep | ||
from osha.oira.client.subscribers import update_user_languages | ||
from plone import api | ||
from plone.memoize.view import memoize | ||
from z3c.saconfig import Session | ||
|
||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SetNewsletterLanguagesPerUser(UpgradeStep): | ||
"""Set newsletter languages per user.""" | ||
|
||
@property | ||
@memoize | ||
def client(self): | ||
return api.portal.get().client | ||
|
||
@memoize | ||
def get_tool(self, zodb_path): | ||
tool = self.client.restrictedTraverse(zodb_path, None) | ||
if not tool: | ||
return None | ||
if not tool.language: | ||
logger.info("No language for tool %s", zodb_path) | ||
return None | ||
return tool | ||
|
||
def __call__(self): | ||
sessions = ( | ||
Session.query(SurveySession, Account) | ||
.filter(SurveySession.account_id == Account.id) | ||
.filter(Account.account_type != "guest") | ||
.filter(SurveySession.archived.is_(None)) | ||
) | ||
done = defaultdict(list) | ||
for session, account in sessions: | ||
if session.zodb_path in done[account.id]: | ||
continue | ||
tool = self.get_tool(session.zodb_path) | ||
if not tool: | ||
continue | ||
update_user_languages(account.id, tool) | ||
done[account.id].append(session.zodb_path) |