diff src/plugins/plugin_misc_invitations.py @ 2291:c05000d00dbb

plugin events, invitations + jp (event/create, invitation/invitee/invite): several emails addresses can now be specified for a single invitation: if several addresses are specified, the same invitation (same ID and data) is sent to all the addresses
author Goffi <goffi@goffi.org>
date Fri, 30 Jun 2017 00:04:47 +0200
parents 61e836cc9512
children 310a454c8657
line wrap: on
line diff
--- a/src/plugins/plugin_misc_invitations.py	Fri Jun 30 00:02:16 2017 +0200
+++ b/src/plugins/plugin_misc_invitations.py	Fri Jun 30 00:04:47 2017 +0200
@@ -24,6 +24,7 @@
 log = getLogger(__name__)
 import shortuuid
 from sat.tools import utils
+from sat.tools.common import data_format
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber import error
@@ -51,7 +52,8 @@
 KEY_LAST_CONNECTION = u'last_connection'
 KEY_GUEST_PROFILE = u'guest_profile'
 KEY_PASSWORD = u'password'
-EXTRA_RESERVED = {KEY_ID, KEY_JID, KEY_CREATED, u'jid_', u'jid', KEY_LAST_CONNECTION, KEY_GUEST_PROFILE, KEY_PASSWORD}
+KEY_EMAILS_EXTRA = u'emails_extra'
+EXTRA_RESERVED = {KEY_ID, KEY_JID, KEY_CREATED, u'jid_', u'jid', KEY_LAST_CONNECTION, KEY_GUEST_PROFILE, KEY_PASSWORD, KEY_EMAILS_EXTRA}
 DEFAULT_SUBJECT = D_(u"You have been invited by {host_name} to {app_name}")
 DEFAULT_BODY = D_(u"""Hello {name}!
 
@@ -72,7 +74,7 @@
         log.info(_(u"plugin Invitations initialization"))
         self.host = host
         self.invitations = persistent.LazyPersistentBinaryDict(u'invitations')
-        host.bridge.addMethod("invitationCreate", ".plugin", in_sign='sssssssssa{ss}s', out_sign='a{ss}',
+        host.bridge.addMethod("invitationCreate", ".plugin", in_sign='sasssssssssa{ss}s', out_sign='a{ss}',
                               method=self._create,
                               async=True)
         host.bridge.addMethod("invitationGet", ".plugin", in_sign='s', out_sign='a{ss}',
@@ -90,16 +92,21 @@
             raise ValueError(_(u"You can't use following key(s) in extra, they are reserved: {}").format(
                 u', '.join(EXTRA_RESERVED.intersection(extra))))
 
-    def _create(self, jid_=u'', password=u'', name=u'', host_name=u'', email=u'', language=u'', url_template=u'', message_subject=u'', message_body=u'', extra=None, profile=u''):
+    def _create(self, email=u'', emails_extra=None, jid_=u'', password=u'', name=u'', host_name=u'', language=u'', url_template=u'', message_subject=u'', message_body=u'', extra=None, profile=u''):
         # XXX: we don't use **kwargs here to keep arguments name for introspection with D-Bus bridge
+        if emails_extra is None:
+            emails_extra = []
 
         if extra is None:
             extra = {}
         else:
             extra = {unicode(k): unicode(v) for k,v in extra.iteritems()}
 
+        kwargs = {"extra": extra,
+                  KEY_EMAILS_EXTRA: [unicode(e) for e in emails_extra]
+                  }
+
         # we need to be sure that values are unicode, else they won't be pickled correctly with D-Bus
-        kwargs = {"extra": extra}
         for key in ("jid_", "password", "name", "host_name", "email", "language", "url_template", "message_subject", "message_body", "profile"):
             value = locals()[key]
             if value:
@@ -168,6 +175,9 @@
         self.checkExtra(extra)
 
         email = kwargs.pop(u'email', None)
+        emails_extra = kwargs.pop(u'emails_extra', [])
+        if not email and emails_extra:
+            raise ValueError(_(u'You need to provide a main email address before using emails_extra'))
 
         if email is not None and not 'url_template' in kwargs and not 'message_body' in kwargs:
             raise ValueError(_(u"You need to provide url_template if you use default message body"))
@@ -248,6 +258,7 @@
 
         if email is not None:
             extra[u'email'] = email
+            data_format.iter2dict(KEY_EMAILS_EXTRA, extra)
             url_template = kwargs.pop(u'url_template', '')
             format_args = {
                 u'uuid': id_,
@@ -276,7 +287,7 @@
 
             yield sat_email.sendEmail(
                 self.host,
-                [email],
+                [email] + emails_extra,
                 (kwargs.pop(u'message_subject', None) or DEFAULT_SUBJECT).format(**format_args),
                 (kwargs.pop(u'message_body', None) or DEFAULT_BODY).format(**format_args),
             )