This commit was manufactured by cvs2svn to create branch
[plcapi.git] / PLC / Methods / UpdateMessage.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Messages import Message, Messages
5 from PLC.Auth import Auth
6
7 can_update = lambda (field, value): field in \
8              ['template', 'enabled']
9
10 class UpdateMessage(Method):
11     """
12     Updates the parameters of an existing message template with the
13     values in message_fields.
14
15     Returns 1 if successful, faults otherwise.
16     """
17
18     roles = ['admin']
19
20     message_fields = dict(filter(can_update, Message.fields.items()))
21
22     accepts = [
23         Auth(),
24         Message.fields['message_id'],
25         message_fields
26         ]
27
28     returns = Parameter(int, '1 if successful')
29
30     def call(self, auth, message_id, message_fields):
31         message_fields = dict(filter(can_update, message_fields.items()))
32
33         # Get message information
34         messages = Messages(self.api, [message_id])
35         if not messages:
36             raise PLCInvalidArgument, "No such message"
37         message = messages[0]
38
39         message.update(message_fields)
40         message.sync()
41         self.object_ids = [message['message_id']]
42
43         return 1