|
15 | 15 | # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
16 | 16 | # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
17 | 17 | # |
18 | | -# $Id: cgi_client.py,v 1.20 2001-08-12 06:32:36 richard Exp $ |
| 18 | +# $Id: cgi_client.py,v 1.21 2001-08-15 23:43:18 richard Exp $ |
19 | 19 |
|
20 | 20 | import os, cgi, pprint, StringIO, urlparse, re, traceback, mimetypes |
21 | 21 |
|
22 | | -import roundupdb, htmltemplate, date |
| 22 | +import roundupdb, htmltemplate, date, hyperdb |
23 | 23 |
|
24 | 24 | class Unauthorised(ValueError): |
25 | 25 | pass |
@@ -124,7 +124,8 @@ def index_filterspec(self): |
124 | 124 | if key[0] == ':': continue |
125 | 125 | prop = props[key] |
126 | 126 | value = self.form[key] |
127 | | - if isinstance(prop.isLinkType or prop, hyperdb.Multilink): |
| 127 | + if (isinstance(prop, hyperdb.Link) or |
| 128 | + isinstance(prop, hyperdb.Multilink)): |
128 | 129 | if type(value) == type([]): |
129 | 130 | value = [arg.value for arg in value] |
130 | 131 | else: |
@@ -197,55 +198,9 @@ def shownode(self, message=None): |
197 | 198 | keys = self.form.keys() |
198 | 199 | num_re = re.compile('^\d+$') |
199 | 200 | if keys: |
200 | | - changed = [] |
201 | | - props = {} |
202 | 201 | try: |
203 | | - keys = self.form.keys() |
204 | | - for key in keys: |
205 | | - if not cl.properties.has_key(key): |
206 | | - continue |
207 | | - proptype = cl.properties[key] |
208 | | - if isinstance(proptype, hyperdb.String): |
209 | | - value = str(self.form[key].value).strip() |
210 | | - elif isinstance(proptype, hyperdb.Date): |
211 | | - value = date.Date(str(self.form[key].value)) |
212 | | - elif isinstance(proptype, hyperdb.Interval): |
213 | | - value = date.Interval(str(self.form[key].value)) |
214 | | - elif isinstance(proptype, hyperdb.Link): |
215 | | - value = str(self.form[key].value).strip() |
216 | | - # handle key values |
217 | | - link = cl.properties[key].classname |
218 | | - if not num_re.match(value): |
219 | | - try: |
220 | | - value = self.db.classes[link].lookup(value) |
221 | | - except: |
222 | | - raise ValueError, 'property "%s": %s not a %s'%( |
223 | | - key, value, link) |
224 | | - elif isinstance(proptype, hyperdb.Multilink): |
225 | | - value = self.form[key] |
226 | | - if type(value) != type([]): |
227 | | - value = [i.strip() for i in str(value.value).split(',')] |
228 | | - else: |
229 | | - value = [str(i.value).strip() for i in value] |
230 | | - link = cl.properties[key].classname |
231 | | - l = [] |
232 | | - for entry in map(str, value): |
233 | | - if not num_re.match(entry): |
234 | | - try: |
235 | | - entry = self.db.classes[link].lookup(entry) |
236 | | - except: |
237 | | - raise ValueError, \ |
238 | | - 'property "%s": %s not a %s'%(key, |
239 | | - entry, link) |
240 | | - l.append(entry) |
241 | | - l.sort() |
242 | | - value = l |
243 | | - # if changed, set it |
244 | | - if value != cl.get(self.nodeid, key): |
245 | | - changed.append(key) |
246 | | - props[key] = value |
| 202 | + props, changed = parsePropsFromForm(cl, self.form) |
247 | 203 | cl.set(self.nodeid, **props) |
248 | | - |
249 | 204 | self._post_editnode(self.nodeid, changed) |
250 | 205 | # and some nice feedback for the user |
251 | 206 | message = '%s edited ok'%', '.join(changed) |
@@ -290,51 +245,8 @@ def showfile(self): |
290 | 245 | def _createnode(self): |
291 | 246 | ''' create a node based on the contents of the form |
292 | 247 | ''' |
293 | | - cn = self.classname |
294 | | - cl = self.db.classes[cn] |
295 | | - props = {} |
296 | | - keys = self.form.keys() |
297 | | - num_re = re.compile('^\d+$') |
298 | | - for key in keys: |
299 | | - if not cl.properties.has_key(key): |
300 | | - continue |
301 | | - proptype = cl.properties[key] |
302 | | - if isinstance(proptype, hyperdb.String): |
303 | | - value = self.form[key].value.strip() |
304 | | - elif isinstance(proptype, hyperdb.Date): |
305 | | - value = date.Date(self.form[key].value.strip()) |
306 | | - elif isinstance(proptype, hyperdb.Interval): |
307 | | - value = date.Interval(self.form[key].value.strip()) |
308 | | - elif isinstance(proptype, hyperdb.Link): |
309 | | - value = self.form[key].value.strip() |
310 | | - # handle key values |
311 | | - link = cl.properties[key].classname |
312 | | - if not num_re.match(value): |
313 | | - try: |
314 | | - value = self.db.classes[link].lookup(value) |
315 | | - except: |
316 | | - raise ValueError, 'property "%s": %s not a %s'%( |
317 | | - key, value, link) |
318 | | - elif isinstance(proptype, hyperdb.Multilink): |
319 | | - value = self.form[key] |
320 | | - if type(value) != type([]): |
321 | | - value = [i.strip() for i in value.value.split(',')] |
322 | | - else: |
323 | | - value = [i.value.strip() for i in value] |
324 | | - link = cl.properties[key].classname |
325 | | - l = [] |
326 | | - for entry in map(str, value): |
327 | | - if not num_re.match(entry): |
328 | | - try: |
329 | | - entry = self.db.classes[link].lookup(entry) |
330 | | - except: |
331 | | - raise ValueError, \ |
332 | | - 'property "%s": %s not a %s'%(key, |
333 | | - entry, link) |
334 | | - l.append(entry) |
335 | | - l.sort() |
336 | | - value = l |
337 | | - props[key] = value |
| 248 | + cl = self.db.classes[self.classname] |
| 249 | + props, dummy = parsePropsFromForm(cl, self.form) |
338 | 250 | return cl.create(**props) |
339 | 251 |
|
340 | 252 | def _post_editnode(self, nid, changes=None): |
@@ -376,7 +288,7 @@ def _post_editnode(self, nid, changes=None): |
376 | 288 | if len(nosy) == 1 and uid in nosy: |
377 | 289 | nosy = 0 |
378 | 290 | if (nosy and props.has_key('messages') and |
379 | | - props['messages'].isMultilinkType and |
| 291 | + isinstance(props['messages'], hyperdb.Multilink) and |
380 | 292 | props['messages'].classname == 'msg'): |
381 | 293 |
|
382 | 294 | # handle the note |
@@ -554,8 +466,64 @@ def main(self, dre=re.compile(r'([^\d]+)(\d+)'), nre=re.compile(r'new(\w+)')): |
554 | 466 | def __del__(self): |
555 | 467 | self.db.close() |
556 | 468 |
|
| 469 | +def parsePropsFromForm(cl, form, note_changed=0): |
| 470 | + '''Pull properties for the given class out of the form. |
| 471 | + ''' |
| 472 | + props = {} |
| 473 | + changed = [] |
| 474 | + keys = form.keys() |
| 475 | + num_re = re.compile('^\d+$') |
| 476 | + for key in keys: |
| 477 | + if not cl.properties.has_key(key): |
| 478 | + continue |
| 479 | + proptype = cl.properties[key] |
| 480 | + if isinstance(proptype, hyperdb.String): |
| 481 | + value = form[key].value.strip() |
| 482 | + elif isinstance(proptype, hyperdb.Date): |
| 483 | + value = date.Date(form[key].value.strip()) |
| 484 | + elif isinstance(proptype, hyperdb.Interval): |
| 485 | + value = date.Interval(form[key].value.strip()) |
| 486 | + elif isinstance(proptype, hyperdb.Link): |
| 487 | + value = form[key].value.strip() |
| 488 | + # handle key values |
| 489 | + link = cl.properties[key].classname |
| 490 | + if not num_re.match(value): |
| 491 | + try: |
| 492 | + value = self.db.classes[link].lookup(value) |
| 493 | + except: |
| 494 | + raise ValueError, 'property "%s": %s not a %s'%( |
| 495 | + key, value, link) |
| 496 | + elif isinstance(proptype, hyperdb.Multilink): |
| 497 | + value = form[key] |
| 498 | + if type(value) != type([]): |
| 499 | + value = [i.strip() for i in value.value.split(',')] |
| 500 | + else: |
| 501 | + value = [i.value.strip() for i in value] |
| 502 | + link = cl.properties[key].classname |
| 503 | + l = [] |
| 504 | + for entry in map(str, value): |
| 505 | + if not num_re.match(entry): |
| 506 | + try: |
| 507 | + entry = self.db.classes[link].lookup(entry) |
| 508 | + except: |
| 509 | + raise ValueError, \ |
| 510 | + 'property "%s": %s not a %s'%(key, |
| 511 | + entry, link) |
| 512 | + l.append(entry) |
| 513 | + l.sort() |
| 514 | + value = l |
| 515 | + props[key] = value |
| 516 | + # if changed, set it |
| 517 | + if note_changed and value != cl.get(self.nodeid, key): |
| 518 | + changed.append(key) |
| 519 | + props[key] = value |
| 520 | + return props, changed |
| 521 | + |
557 | 522 | # |
558 | 523 | # $Log: not supported by cvs2svn $ |
| 524 | +# Revision 1.20 2001/08/12 06:32:36 richard |
| 525 | +# using isinstance(blah, Foo) now instead of isFooType |
| 526 | +# |
559 | 527 | # Revision 1.19 2001/08/07 00:24:42 richard |
560 | 528 | # stupid typo |
561 | 529 | # |
|
0 commit comments