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