Code

Fix issue2550510
[roundup.git] / roundup / xmlrpc.py
1 #
2 # Copyright (C) 2007 Stefan Seefeld
3 # All rights reserved.
4 # For license terms see the file COPYING.txt.
5 #
7 from roundup import hyperdb
8 from roundup.cgi.exceptions import *
9 from roundup.exceptions import UsageError
10 from roundup.date import Date, Range, Interval
11 from roundup import actions
12 from SimpleXMLRPCServer import *
14 def translate(value):
15     """Translate value to becomes valid for XMLRPC transmission."""
17     if isinstance(value, (Date, Range, Interval)):
18         return repr(value)
19     elif type(value) is list:
20         return [translate(v) for v in value]
21     elif type(value) is tuple:
22         return tuple([translate(v) for v in value])
23     elif type(value) is dict:
24         return dict([[translate(k), translate(value[k])] for k in value])
25     else:
26         return value
29 def props_from_args(db, cl, args, itemid=None):
30     """Construct a list of properties from the given arguments,
31     and return them after validation."""
33     props = {}
34     for arg in args:
35         if arg.find('=') == -1:
36             raise UsageError, 'argument "%s" not propname=value'%arg
37         l = arg.split('=')
38         if len(l) < 2:
39             raise UsageError, 'argument "%s" not propname=value'%arg
40         key, value = l[0], '='.join(l[1:])
41         if value:
42             try:
43                 props[key] = hyperdb.rawToHyperdb(db, cl, itemid,
44                                                   key, value)
45             except hyperdb.HyperdbValueError, message:
46                 raise UsageError, message
47         else:
48             props[key] = None
50     return props
52 class RoundupInstance:
53     """The RoundupInstance provides the interface accessible through
54     the Python XMLRPC mapping."""
56     def __init__(self, db, actions, translator):
58         self.db = db
59         self.actions = actions
60         self.translator = translator
62     def list(self, classname, propname=None):
63         cl = self.db.getclass(classname)
64         if not propname:
65             propname = cl.labelprop()
66         result = [cl.get(itemid, propname)
67                   for itemid in cl.list()
68                   if self.db.security.hasPermission('View', self.db.getuid(),
69                                                     classname, propname, itemid)
70                   ]
71         return result
73     def filter(self, classname, search_matches, filterspec,
74                sort=[], group=[]):
75         cl = self.db.getclass(classname)
76         result = cl.filter(search_matches, filterspec, sort=sort, group=group)
77         return result
79     def display(self, designator, *properties):
80         classname, itemid = hyperdb.splitDesignator(designator)
81         cl = self.db.getclass(classname)
82         props = properties and list(properties) or cl.properties.keys()
83         props.sort()
84         for p in props:
85             if not self.db.security.hasPermission('View', self.db.getuid(),
86                                                   classname, p, itemid):
87                 raise Unauthorised('Permission to view %s of %s denied'%
88                                    (p, designator))
89             result = [(prop, cl.get(itemid, prop)) for prop in props]
90         return dict(result)
92     def create(self, classname, *args):
93         if not self.db.security.hasPermission('Create', self.db.getuid(), classname):
94             raise Unauthorised('Permission to create %s denied'%classname)
96         cl = self.db.getclass(classname)
98         # convert types
99         props = props_from_args(self.db, cl, args)
101         # check for the key property
102         key = cl.getkey()
103         if key and not props.has_key(key):
104             raise UsageError, 'you must provide the "%s" property.'%key
106         # do the actual create
107         try:
108             result = cl.create(**props)
109         except (TypeError, IndexError, ValueError), message:
110             raise UsageError, message
111         return result
113     def set(self, designator, *args):
115         classname, itemid = hyperdb.splitDesignator(designator)
116         cl = self.db.getclass(classname)
117         props = props_from_args(self.db, cl, args, itemid) # convert types
118         for p in props.iterkeys():
119             if not self.db.security.hasPermission('Edit', self.db.getuid(),
120                                                   classname, p, itemid):
121                 raise Unauthorised('Permission to edit %s of %s denied'%
122                                    (p, designator))
123         try:
124             return cl.set(itemid, **props)
125         except (TypeError, IndexError, ValueError), message:
126             raise UsageError, message
129     builtin_actions = {'retire': actions.Retire}
131     def action(self, name, *args):
132         """"""
133         
134         if name in self.actions:
135             action_type = self.actions[name]
136         elif name in self.builtin_actions:
137             action_type = self.builtin_actions[name]
138         else:
139             raise Exception('action "%s" is not supported %s' % (name, ','.join(self.actions.keys())))
140         action = action_type(self.db, self.translator)
141         return action.execute(*args)
144 class RoundupDispatcher(SimpleXMLRPCDispatcher):
145     """RoundupDispatcher bridges from cgi.client to RoundupInstance.
146     It expects user authentication to be done."""
148     def __init__(self, db, actions, translator,
149                  allow_none=False, encoding=None):
151         SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
152         self.register_instance(RoundupInstance(db, actions, translator))
153                  
155     def dispatch(self, input):
156         return self._marshaled_dispatch(input)
158     def _dispatch(self, method, params):
160         retn = SimpleXMLRPCDispatcher._dispatch(self, method, params)
161         retn = translate(retn)
162         return retn
163