comparison roundup/rest.py @ 5576:77d11a24f718 REST-rebased

Exceptions Handle a case where KeyError exception raise uncaught, Changed some UsageError exception to ValueError exception to handle 409 Conflicted error. committer: Ralf Schlatterbeck <rsc@runtux.com>
author Chau Nguyen <dangchau1991@yahoo.com>
date Wed, 30 Jan 2019 10:26:34 +0100
parents 7a7927643357
children 11e75fbb9edc
comparison
equal deleted inserted replaced
5575:7a7927643357 5576:77d11a24f718
123 props = props_from_args(self.db, class_obj, input.value) 123 props = props_from_args(self.db, class_obj, input.value)
124 124
125 # check for the key property 125 # check for the key property
126 key = class_obj.getkey() 126 key = class_obj.getkey()
127 if key and key not in props: 127 if key and key not in props:
128 raise UsageError('Must provide the "%s" property.' % key) 128 raise UsageError("Must provide the '%s' property." % key)
129 129
130 for key in props: 130 for key in props:
131 if not self.db.security.hasPermission('Create', self.db.getuid(), 131 if not self.db.security.hasPermission('Create', self.db.getuid(),
132 class_name, property=key): 132 class_name, property=key):
133 raise Unauthorised('Permission to create %s.%s denied' % 133 raise Unauthorised('Permission to create %s.%s denied' %
136 # do the actual create 136 # do the actual create
137 try: 137 try:
138 item_id = class_obj.create(**props) 138 item_id = class_obj.create(**props)
139 self.db.commit() 139 self.db.commit()
140 except (TypeError, IndexError, ValueError), message: 140 except (TypeError, IndexError, ValueError), message:
141 raise UsageError(message) 141 raise ValueError(message)
142 except KeyError, msg:
143 raise UsageError("Must provide the %s property." % msg)
142 144
143 # set the header Location 145 # set the header Location
144 link = self.base_path + class_name + item_id 146 link = self.base_path + class_name + item_id
145 self.client.setHeader("Location", link) 147 self.client.setHeader("Location", link)
146 148
150 'link': link 152 'link': link
151 } 153 }
152 return 201, result 154 return 201, result
153 155
154 def post_element(self, class_name, item_id, input): 156 def post_element(self, class_name, item_id, input):
155 raise Reject('Invalid request') 157 raise Reject('POST to an item is not allowed')
156 158
157 def put_collection(self, class_name, input): 159 def put_collection(self, class_name, input):
158 raise Reject('Invalid request') 160 raise Reject('PUT a class is not allowed')
159 161
160 def put_element(self, class_name, item_id, input): 162 def put_element(self, class_name, item_id, input):
161 class_obj = self.db.getclass(class_name) 163 class_obj = self.db.getclass(class_name)
162 164
163 props = props_from_args(self.db, class_obj, input.value, item_id) 165 props = props_from_args(self.db, class_obj, input.value, item_id)
168 (p, class_name, item_id)) 170 (p, class_name, item_id))
169 try: 171 try:
170 result = class_obj.set(item_id, **props) 172 result = class_obj.set(item_id, **props)
171 self.db.commit() 173 self.db.commit()
172 except (TypeError, IndexError, ValueError), message: 174 except (TypeError, IndexError, ValueError), message:
173 raise UsageError(message) 175 raise ValueError(message)
174 176
175 result = { 177 result = {
176 'id': item_id, 178 'id': item_id,
177 'type': class_name, 179 'type': class_name,
178 'link': self.base_path + class_name + item_id, 180 'link': self.base_path + class_name + item_id,
217 } 219 }
218 220
219 return 200, result 221 return 200, result
220 222
221 def patch_collection(self, class_name, input): 223 def patch_collection(self, class_name, input):
222 raise Reject('Invalid request') 224 raise Reject('PATCH a class is not allowed')
223 225
224 def patch_element(self, class_name, item_id, input): 226 def patch_element(self, class_name, item_id, input):
225 raise NotImplementedError 227 raise NotImplementedError
226 228
227 def options_collection(self, class_name, input): 229 def options_collection(self, class_name, input):
286 self.client.response_code = 403 288 self.client.response_code = 403
287 except (hyperdb.DesignatorError, UsageError), msg: 289 except (hyperdb.DesignatorError, UsageError), msg:
288 output = error_obj(400, msg) 290 output = error_obj(400, msg)
289 self.client.response_code = 400 291 self.client.response_code = 400
290 except (AttributeError, Reject), msg: 292 except (AttributeError, Reject), msg:
291 output = error_obj(405, 'Method Not Allowed. ' + str(msg)) 293 output = error_obj(405, msg)
292 self.client.response_code = 405 294 self.client.response_code = 405
295 except ValueError, msg:
296 output = error_obj(409, msg)
297 self.client.response_code = 409
293 except NotImplementedError: 298 except NotImplementedError:
294 output = error_obj(402, 'Method is under development') 299 output = error_obj(402, 'Method is under development')
295 self.client.response_code = 402 300 self.client.response_code = 402
296 # nothing to pay, just a mark for debugging purpose 301 # nothing to pay, just a mark for debugging purpose
297 except: 302 except:

Roundup Issue Tracker: http://roundup-tracker.org/