f9933bf2beb197619569a20b64807a10e93fab50
[plcapi.git] / PLC / Methods / NotifyPersons.py
1 # $Id$
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Filter import Filter
5 from PLC.Auth import Auth
6 from PLC.Persons import Person, Persons
7 from PLC.sendmail import sendmail
8
9 class NotifyPersons(Method):
10     """
11     Sends an e-mail message to the specified users. If person_filter
12     is specified and is an array of user identifiers or usernames, or
13     a struct of user attributes, only users matching the filter will
14     receive the message.
15
16     Returns 1 if successful.
17     """
18
19     roles = ['admin', 'node']
20
21     accepts = [
22         Auth(),
23         Mixed([Mixed(Person.fields['person_id'],
24                      Person.fields['email'])],
25               Filter(Person.fields)),
26         Parameter(str, "E-mail subject"),
27         Parameter(str, "E-mail body")
28         ]
29
30     returns = Parameter(int, '1 if successful')
31
32     def call(self, auth, person_filter, subject, body):
33         persons = Persons(self.api, person_filter,
34                           ['person_id', 'first_name', 'last_name', 'email'])
35         if not persons:
36             raise PLCInvalidArgument, "No such user(s)"
37
38         # Send email
39         sendmail(self.api,
40                  To = [("%s %s" % (person['first_name'], person['last_name']),
41                         person['email']) for person in persons],
42                  Subject = subject,
43                  Body = body)
44
45         # Logging variables
46         self.event_objects = {'Person': [person['person_id'] for person in persons]}
47         self.message = subject
48
49         return 1