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