- value returned is now xml-rpc safe
[plcapi.git] / PLC / Methods / GetKeys.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Keys import Key, Keys
5 from PLC.Auth import PasswordAuth
6
7 class GetKeys(Method):
8     """
9     Get an array of structs containing the keys for the specified
10     key_ids. If key_id_list is not specified, all keys
11     will be queried.
12
13     Admin may get all keys. Non-admins can only get their own keys
14     """
15
16     roles = ['admin', 'pi', 'user', 'tech']
17
18     accepts = [
19         PasswordAuth(),
20         [Key.fields['key_id']]
21         ]
22
23     returns = [Key.fields]
24
25     def call(self, auth, key_id_list = None):
26                 
27         #if we are not admin, make sure to only return our own keys       
28         if 'admin' not in self.caller['roles']:
29                 if key_id_list is None:
30                         key_id_list =  self.caller['key_ids']
31                 else:
32                         valid_keys = lambda x: x in self.caller['key_ids']
33                         key_id_list = filter(valid_keys, key_id_list)
34                 
35         keys = Keys(self.api, key_id_list).values()
36         
37         # Filter out undesired or None fields (XML-RPC cannot marshal
38         # None) and turn each key into a real dict.
39         valid_return_fields_only = lambda (key, value): value is not None
40         keys = [dict(filter(valid_return_fields_only, key.items())) \
41                       for key in keys]          
42         return keys