Skip to content

Commit 7099a81

Browse files
author
Richard Jones
committed
Fixed some isFooTypes that I missed.
Refactored some code in the CGI code.
1 parent 197adf7 commit 7099a81

File tree

4 files changed

+88
-112
lines changed

4 files changed

+88
-112
lines changed

roundup/cgi_client.py

Lines changed: 64 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
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 $
1919

2020
import os, cgi, pprint, StringIO, urlparse, re, traceback, mimetypes
2121

22-
import roundupdb, htmltemplate, date
22+
import roundupdb, htmltemplate, date, hyperdb
2323

2424
class Unauthorised(ValueError):
2525
pass
@@ -124,7 +124,8 @@ def index_filterspec(self):
124124
if key[0] == ':': continue
125125
prop = props[key]
126126
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)):
128129
if type(value) == type([]):
129130
value = [arg.value for arg in value]
130131
else:
@@ -197,55 +198,9 @@ def shownode(self, message=None):
197198
keys = self.form.keys()
198199
num_re = re.compile('^\d+$')
199200
if keys:
200-
changed = []
201-
props = {}
202201
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)
247203
cl.set(self.nodeid, **props)
248-
249204
self._post_editnode(self.nodeid, changed)
250205
# and some nice feedback for the user
251206
message = '%s edited ok'%', '.join(changed)
@@ -290,51 +245,8 @@ def showfile(self):
290245
def _createnode(self):
291246
''' create a node based on the contents of the form
292247
'''
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)
338250
return cl.create(**props)
339251

340252
def _post_editnode(self, nid, changes=None):
@@ -376,7 +288,7 @@ def _post_editnode(self, nid, changes=None):
376288
if len(nosy) == 1 and uid in nosy:
377289
nosy = 0
378290
if (nosy and props.has_key('messages') and
379-
props['messages'].isMultilinkType and
291+
isinstance(props['messages'], hyperdb.Multilink) and
380292
props['messages'].classname == 'msg'):
381293

382294
# handle the note
@@ -554,8 +466,64 @@ def main(self, dre=re.compile(r'([^\d]+)(\d+)'), nre=re.compile(r'new(\w+)')):
554466
def __del__(self):
555467
self.db.close()
556468

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+
557522
#
558523
# $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+
#
559527
# Revision 1.19 2001/08/07 00:24:42 richard
560528
# stupid typo
561529
#

roundup/date.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: date.py,v 1.10 2001-08-07 00:24:42 richard Exp $
18+
# $Id: date.py,v 1.11 2001-08-15 23:43:18 richard Exp $
1919

2020
import time, re, calendar
2121

@@ -71,8 +71,6 @@ class Date:
7171
>>> Date("14:25", -5)
7272
<Date 2000-06-25.19:25:00>
7373
'''
74-
isDate = 1
75-
7674
def __init__(self, spec='.', offset=0):
7775
"""Construct a date given a specification and a time zone offset.
7876
@@ -116,7 +114,7 @@ def __sub__(self, other):
116114
1. an interval from this date to produce another date.
117115
2. a date from this date to produce an interval.
118116
"""
119-
if other.isDate:
117+
if isinstance(other, Date):
120118
# TODO this code will fall over laughing if the dates cross
121119
# leap years, phases of the moon, ....
122120
a = calendar.timegm((self.year, self.month, self.day, self.hour,
@@ -239,8 +237,6 @@ class Interval:
239237
>>> Date(". + 2d") - Interval("3w")
240238
<Date 2000-06-07.00:34:02>
241239
'''
242-
isInterval = 1
243-
244240
def __init__(self, spec, sign=1):
245241
"""Construct an interval given a specification."""
246242
if type(spec) == type(''):
@@ -380,6 +376,9 @@ def test():
380376

381377
#
382378
# $Log: not supported by cvs2svn $
379+
# Revision 1.10 2001/08/07 00:24:42 richard
380+
# stupid typo
381+
#
383382
# Revision 1.9 2001/08/07 00:15:51 richard
384383
# Added the copyright/license notice to (nearly) all files at request of
385384
# Bizar Software.

roundup/htmltemplate.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: htmltemplate.py,v 1.19 2001-08-12 06:32:36 richard Exp $
18+
# $Id: htmltemplate.py,v 1.20 2001-08-15 23:43:18 richard Exp $
1919

2020
import os, re, StringIO, urllib, cgi, errno
2121

@@ -94,8 +94,9 @@ def __call__(self, property, size=None, height=None, showid=0):
9494
# TODO: pull the value from the form
9595
if isinstance(propclass, hyperdb.Multilink): value = []
9696
else: value = ''
97-
if isinstance((propclass.isStringType or propclass, hyperdb.Date) or
98-
propclass.isIntervalType):
97+
if (isinstance(propclass, hyperdb.String) or
98+
isinstance(propclass, hyperdb.Date) or
99+
isinstance(propclass, hyperdb.Interval)):
99100
size = size or 30
100101
if value is None:
101102
value = ''
@@ -297,7 +298,8 @@ def __call__(self, property, **args):
297298
value = self.filterspec.get(property, [])
298299
else:
299300
value = []
300-
if isinstance(propclass.isLinkType or propclass, hyperdb.Multilink):
301+
if (isinstance(propclass, hyperdb.Link) or
302+
isinstance(propclass, hyperdb.Multilink)):
301303
linkcl = self.db.classes[propclass.classname]
302304
l = []
303305
k = linkcl.labelprop()
@@ -740,6 +742,9 @@ def newitem(client, templates, db, classname, form, replace=re.compile(
740742

741743
#
742744
# $Log: not supported by cvs2svn $
745+
# Revision 1.19 2001/08/12 06:32:36 richard
746+
# using isinstance(blah, Foo) now instead of isFooType
747+
#
743748
# Revision 1.18 2001/08/07 00:24:42 richard
744749
# stupid typo
745750
#

roundup/hyperdb.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: hyperdb.py,v 1.15 2001-08-12 06:32:36 richard Exp $
18+
# $Id: hyperdb.py,v 1.16 2001-08-15 23:43:18 richard Exp $
1919

2020
# standard python modules
2121
import cPickle, re, string
@@ -190,11 +190,11 @@ def create(self, **propvalues):
190190
raise TypeError, 'new property "%s" not a string'%key
191191

192192
elif isinstance(prop, Date):
193-
if not hasattr(value, 'isDate'):
193+
if not isinstance(value, date.Date):
194194
raise TypeError, 'new property "%s" not a Date'% key
195195

196196
elif isinstance(prop, Interval):
197-
if not hasattr(value, 'isInterval'):
197+
if not isinstance(value, date.Interval):
198198
raise TypeError, 'new property "%s" not an Interval'% key
199199

200200
for key, prop in self.properties.items():
@@ -345,11 +345,11 @@ class or a KeyError is raised.
345345
raise TypeError, 'new property "%s" not a string'%key
346346

347347
elif isinstance(prop, Date):
348-
if not hasattr(value, 'isDate'):
348+
if not isinstance(value, date.Date):
349349
raise TypeError, 'new property "%s" not a Date'% key
350350

351351
elif isinstance(prop, Interval):
352-
if not hasattr(value, 'isInterval'):
352+
if not isinstance(value, date.Interval):
353353
raise TypeError, 'new property "%s" not an Interval'% key
354354

355355
node[key] = value
@@ -675,7 +675,8 @@ def sortfun(a, b, sort=sort, group=group, properties=self.getprops(),
675675
av = an[prop] = av.lower()
676676
if bv and bv[0] in string.uppercase:
677677
bv = bn[prop] = bv.lower()
678-
if isinstance(propclass.isStringType or propclass, Date):
678+
if (isinstance(propclass, String) or
679+
isinstance(propclass, Date)):
679680
if dir == '+':
680681
r = cmp(av, bv)
681682
if r != 0: return r
@@ -806,6 +807,9 @@ def Choice(name, *options):
806807

807808
#
808809
# $Log: not supported by cvs2svn $
810+
# Revision 1.15 2001/08/12 06:32:36 richard
811+
# using isinstance(blah, Foo) now instead of isFooType
812+
#
809813
# Revision 1.14 2001/08/07 00:24:42 richard
810814
# stupid typo
811815
#

0 commit comments

Comments
 (0)