From 3bdab238ae0b3ba0136c799ba031ddd190abe6f2 Mon Sep 17 00:00:00 2001 From: Rapolas K Date: Wed, 25 Jun 2014 16:21:44 +0100 Subject: [PATCH] added optional message field to send together with invitation --- src/ploneintranet/workspace/browser/forms.py | 46 +++++++++++++--- .../workspace/tests/test_forms.py | 55 ++++++++++++++++++- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/ploneintranet/workspace/browser/forms.py b/src/ploneintranet/workspace/browser/forms.py index ce63c88..d67f153 100644 --- a/src/ploneintranet/workspace/browser/forms.py +++ b/src/ploneintranet/workspace/browser/forms.py @@ -172,6 +172,12 @@ class IInviteForm(form.Schema): constraint=user_has_email, ) + message = schema.Text( + title=_(u"Message"), + default=u"", + required=False, + ) + class InviteForm(form.SchemaForm): schema = IInviteForm @@ -183,6 +189,8 @@ class InviteForm(form.SchemaForm): def handleApply(self, action): data = self.extractData()[0] given_username = data.get("user", "").strip() + given_message = data.get("message", "") or "" + given_message = given_message.strip() if not given_username: return @@ -203,12 +211,27 @@ def handleApply(self, action): redirect_path="resolveuid/%s" % (ws.context.UID(),)) storage = get_storage() storage[token_id] = (ws.context.UID(), given_username) - message = """Congratulations! You've been invited to join %s -The following is a unique URL tied to your email address (%s). -Clicking the link will make you a member of a %s workspace automatically. + current_user = api.user.get_current() + inviter = current_user.getProperty("fullname", None) + if not inviter: + inviter = current_user.getUserName() + + msg_header = "You've been invited to join %s by %s" % ( + self.context.title, inviter) -%s + if given_message: + optional = "Here is the message from %s\n\n" % inviter + optional = "%s%s\n\n" % (optional, given_message) + given_message = optional + + msg_footer = """ +The following is a unique URL tied to your email address ({email}). + +Clicking the link will make you a member of a {workspace} workspace +automatically. + +{token_url} Good luck, Yours @@ -216,9 +239,18 @@ def handleApply(self, action): ***** Email confidentiality notice ***** This message is private and confidential. If you have received this message in error, please notify us and UNREAD it. -""" % (self.context.title, email, self.context.title, token_url) - - subject = 'You are invited to "%s"' % self.context.title +""".format( + email=email, + workspace=self.context.title, + token_url=token_url) + + message = "{header}\n\n{optional}{footer}".format( + header=msg_header, + optional=given_message, + footer=msg_footer, + ) + + subject = 'You are invited to join "%s"' % self.context.title send_email(email, subject, message) api.portal.show_message( diff --git a/src/ploneintranet/workspace/tests/test_forms.py b/src/ploneintranet/workspace/tests/test_forms.py index 97f4a34..6955b90 100644 --- a/src/ploneintranet/workspace/tests/test_forms.py +++ b/src/ploneintranet/workspace/tests/test_forms.py @@ -372,16 +372,18 @@ def setUp(self): self.portal._updateProperty('email_from_name', 'Portal Owner') self.portal._updateProperty('email_from_address', 'sender@example.org') - def make_request(self, username): + def make_request(self, username, message=""): """ Creates a request :param bool empty: if true, request will be empty, any other given \ parameters will be ignored :param str username: username to enter + :param str message: optional message to send to the user :return: ready to submit request. """ form = { 'form.widgets.user': username, + 'form.widgets.message': message, 'form.buttons.ok': 'OK', } @@ -435,6 +437,57 @@ def test_invitation_send_if_all_above_conditions_met(self): # check that user is added to workspace self.assertEqual(1, len(list(IWorkspace(self.ws).members))-1) + def test_invitation_message_is_sent(self): + email = "vlad@example.org" + username = 'vladislav' + api.user.create( + email=email, + username=username, + password='whatever', + ) + + message = "Hello and join my workspace" + request = self.make_request(username=username, message=message) + form = api.content.get_view( + 'invite', + context=self.ws, + request=request, + ) + + form.update() + data, errors = form.extractData() + self.assertEqual(len(self.mailhost.messages), 1) + msg = message_from_string(self.mailhost.messages[0]) + # mail is actually received by correct recipient + self.assertEqual(msg['To'], email) + body = msg.get_payload() + self.assertIn(message, body) + + def test_if_empty_message_no_text_is_included(self): + email = "vlad@example.org" + username = 'vladislav' + api.user.create( + email=email, + username=username, + password='whatever', + ) + + request = self.make_request(username=username, message=None) + form = api.content.get_view( + 'invite', + context=self.ws, + request=request, + ) + + optional = "Here is the message from %s\n\n" % username + form.update() + self.assertEqual(len(self.mailhost.messages), 1) + msg = message_from_string(self.mailhost.messages[0]) + # mail is actually received by correct recipient + self.assertEqual(msg['To'], email) + body = msg.get_payload() + self.assertNotIn(optional, body) + def test_invitation_send_but_user_became_a_member_not_via_link(self): email = "vlad@example.org" username = 'vladislav'