Merge from trunk
[plcapi.git] / trunk / PLC / Methods / NotifyPersons.py
diff --git a/trunk/PLC/Methods/NotifyPersons.py b/trunk/PLC/Methods/NotifyPersons.py
new file mode 100644 (file)
index 0000000..70c273d
--- /dev/null
@@ -0,0 +1,48 @@
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.Filter import Filter
+from PLC.Auth import Auth
+from PLC.Persons import Person, Persons
+from PLC.sendmail import sendmail
+
+class NotifyPersons(Method):
+    """
+    Sends an e-mail message to the specified users. If person_filter
+    is specified and is an array of user identifiers or usernames, or
+    a struct of user attributes, only users matching the filter will
+    receive the message.
+
+    Returns 1 if successful.
+    """
+
+    roles = ['admin', 'node']
+
+    accepts = [
+        Auth(),
+        Mixed([Mixed(Person.fields['person_id'],
+                     Person.fields['email'])],
+              Filter(Person.fields)),
+        Parameter(str, "E-mail subject"),
+        Parameter(str, "E-mail body")
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, person_filter, subject, body):
+        persons = Persons(self.api, person_filter,
+                          ['person_id', 'first_name', 'last_name', 'email'])
+        if not persons:
+            raise PLCInvalidArgument, "No such user(s)"
+
+        # Send email
+        sendmail(self.api,
+                 To = [("%s %s" % (person['first_name'], person['last_name']),
+                        person['email']) for person in persons],
+                 Subject = subject,
+                 Body = body)
+
+        # Logging variables
+        self.event_objects = {'Person': [person['person_id'] for person in persons]}
+        self.message = subject
+
+        return 1