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 *
13 from xmlrpclib import Binary
15 def translate(value):
16 """Translate value to becomes valid for XMLRPC transmission."""
18 if isinstance(value, (Date, Range, Interval)):
19 return repr(value)
20 elif type(value) is list:
21 return [translate(v) for v in value]
22 elif type(value) is tuple:
23 return tuple([translate(v) for v in value])
24 elif type(value) is dict:
25 return dict([[translate(k), translate(value[k])] for k in value])
26 else:
27 return value
30 def props_from_args(db, cl, args, itemid=None):
31 """Construct a list of properties from the given arguments,
32 and return them after validation."""
34 props = {}
35 for arg in args:
36 if isinstance(arg, Binary):
37 arg = arg.data
38 try :
39 key, value = arg.split('=', 1)
40 except ValueError :
41 raise UsageError, 'argument "%s" not propname=value'%arg
42 if isinstance(key, unicode):
43 try:
44 key = key.encode ('ascii')
45 except UnicodeEncodeError:
46 raise UsageError, 'argument %r is no valid ascii keyword'%key
47 if isinstance(value, unicode):
48 value = value.encode('utf-8')
49 if value:
50 try:
51 props[key] = hyperdb.rawToHyperdb(db, cl, itemid,
52 key, value)
53 except hyperdb.HyperdbValueError, message:
54 raise UsageError, message
55 else:
56 props[key] = None
58 return props
60 class RoundupInstance:
61 """The RoundupInstance provides the interface accessible through
62 the Python XMLRPC mapping."""
64 def __init__(self, db, actions, translator):
66 self.db = db
67 self.actions = actions
68 self.translator = translator
70 def schema(self):
71 s = {}
72 for c in self.db.classes:
73 cls = self.db.classes[c]
74 props = [(n,repr(v)) for n,v in cls.properties.items()]
75 s[c] = props
76 return s
78 def list(self, classname, propname=None):
79 cl = self.db.getclass(classname)
80 if not propname:
81 propname = cl.labelprop()
82 result = [cl.get(itemid, propname)
83 for itemid in cl.list()
84 if self.db.security.hasPermission('View', self.db.getuid(),
85 classname, propname, itemid)
86 ]
87 return result
89 def filter(self, classname, search_matches, filterspec,
90 sort=[], group=[]):
91 cl = self.db.getclass(classname)
92 uid = self.db.getuid()
93 security = self.db.security
94 filterspec = security.filterFilterspec (uid, classname, filterspec)
95 sort = security.filterSortspec (uid, classname, sort)
96 group = security.filterSortspec (uid, classname, group)
97 result = cl.filter(search_matches, filterspec, sort=sort, group=group)
98 check = security.hasPermission
99 x = [id for id in result if check('View', uid, classname, itemid=id)]
100 return x
102 def display(self, designator, *properties):
103 classname, itemid = hyperdb.splitDesignator(designator)
104 cl = self.db.getclass(classname)
105 props = properties and list(properties) or cl.properties.keys()
106 props.sort()
107 for p in props:
108 if not self.db.security.hasPermission('View', self.db.getuid(),
109 classname, p, itemid):
110 raise Unauthorised('Permission to view %s of %s denied'%
111 (p, designator))
112 result = [(prop, cl.get(itemid, prop)) for prop in props]
113 return dict(result)
115 def create(self, classname, *args):
117 if not self.db.security.hasPermission('Create', self.db.getuid(), classname):
118 raise Unauthorised('Permission to create %s denied'%classname)
120 cl = self.db.getclass(classname)
122 # convert types
123 props = props_from_args(self.db, cl, args)
125 # check for the key property
126 key = cl.getkey()
127 if key and not props.has_key(key):
128 raise UsageError, 'you must provide the "%s" property.'%key
130 for key in props:
131 if not self.db.security.hasPermission('Create', self.db.getuid(),
132 classname, property=key):
133 raise Unauthorised('Permission to create %s.%s denied'%(classname, key))
135 # do the actual create
136 try:
137 result = cl.create(**props)
138 self.db.commit()
139 except (TypeError, IndexError, ValueError), message:
140 raise UsageError, message
141 return result
143 def set(self, designator, *args):
145 classname, itemid = hyperdb.splitDesignator(designator)
146 cl = self.db.getclass(classname)
147 props = props_from_args(self.db, cl, args, itemid) # convert types
148 for p in props.iterkeys():
149 if not self.db.security.hasPermission('Edit', self.db.getuid(),
150 classname, p, itemid):
151 raise Unauthorised('Permission to edit %s of %s denied'%
152 (p, designator))
153 try:
154 result = cl.set(itemid, **props)
155 self.db.commit()
156 except (TypeError, IndexError, ValueError), message:
157 raise UsageError, message
158 return result
161 builtin_actions = {'retire': actions.Retire}
163 def action(self, name, *args):
164 """Execute a named action."""
166 if name in self.actions:
167 action_type = self.actions[name]
168 elif name in self.builtin_actions:
169 action_type = self.builtin_actions[name]
170 else:
171 raise Exception('action "%s" is not supported %s' % (name, ','.join(self.actions.keys())))
172 action = action_type(self.db, self.translator)
173 return action.execute(*args)
176 class RoundupDispatcher(SimpleXMLRPCDispatcher):
177 """RoundupDispatcher bridges from cgi.client to RoundupInstance.
178 It expects user authentication to be done."""
180 def __init__(self, db, actions, translator,
181 allow_none=False, encoding=None):
183 try:
184 # python2.5 and beyond
185 SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding)
186 except TypeError:
187 # python2.4
188 SimpleXMLRPCDispatcher.__init__(self)
189 self.register_instance(RoundupInstance(db, actions, translator))
192 def dispatch(self, input):
193 return self._marshaled_dispatch(input)
195 def _dispatch(self, method, params):
197 retn = SimpleXMLRPCDispatcher._dispatch(self, method, params)
198 retn = translate(retn)
199 return retn