Skip to content

Commit

Permalink
added optional message field to send together with invitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapolas K committed Jun 25, 2014
1 parent ad012c5 commit 3bdab23
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
46 changes: 39 additions & 7 deletions src/ploneintranet/workspace/browser/forms.py
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -203,22 +211,46 @@ 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
***** 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(
Expand Down
55 changes: 54 additions & 1 deletion src/ploneintranet/workspace/tests/test_forms.py
Expand Up @@ -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',
}

Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 3bdab23

Please sign in to comment.