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