From f8321089b2a445b0ee36ade9b6e65924073eff6b Mon Sep 17 00:00:00 2001 From: richard Date: Wed, 15 Aug 2001 23:43:18 +0000 Subject: [PATCH] Fixed some isFooTypes that I missed. Refactored some code in the CGI code. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@235 57a73879-2fb5-44c3-a270-3262357dd7e2 --- roundup/cgi_client.py | 160 ++++++++++++++++------------------------ roundup/date.py | 11 ++- roundup/htmltemplate.py | 13 +++- roundup/hyperdb.py | 16 ++-- 4 files changed, 88 insertions(+), 112 deletions(-) diff --git a/roundup/cgi_client.py b/roundup/cgi_client.py index 4f43ab3..e029d41 100644 --- a/roundup/cgi_client.py +++ b/roundup/cgi_client.py @@ -15,11 +15,11 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: cgi_client.py,v 1.20 2001-08-12 06:32:36 richard Exp $ +# $Id: cgi_client.py,v 1.21 2001-08-15 23:43:18 richard Exp $ import os, cgi, pprint, StringIO, urlparse, re, traceback, mimetypes -import roundupdb, htmltemplate, date +import roundupdb, htmltemplate, date, hyperdb class Unauthorised(ValueError): pass @@ -124,7 +124,8 @@ class Client: if key[0] == ':': continue prop = props[key] value = self.form[key] - if isinstance(prop.isLinkType or prop, hyperdb.Multilink): + if (isinstance(prop, hyperdb.Link) or + isinstance(prop, hyperdb.Multilink)): if type(value) == type([]): value = [arg.value for arg in value] else: @@ -197,55 +198,9 @@ class Client: keys = self.form.keys() num_re = re.compile('^\d+$') if keys: - changed = [] - props = {} try: - keys = self.form.keys() - for key in keys: - if not cl.properties.has_key(key): - continue - proptype = cl.properties[key] - if isinstance(proptype, hyperdb.String): - value = str(self.form[key].value).strip() - elif isinstance(proptype, hyperdb.Date): - value = date.Date(str(self.form[key].value)) - elif isinstance(proptype, hyperdb.Interval): - value = date.Interval(str(self.form[key].value)) - elif isinstance(proptype, hyperdb.Link): - value = str(self.form[key].value).strip() - # handle key values - link = cl.properties[key].classname - if not num_re.match(value): - try: - value = self.db.classes[link].lookup(value) - except: - raise ValueError, 'property "%s": %s not a %s'%( - key, value, link) - elif isinstance(proptype, hyperdb.Multilink): - value = self.form[key] - if type(value) != type([]): - value = [i.strip() for i in str(value.value).split(',')] - else: - value = [str(i.value).strip() for i in value] - link = cl.properties[key].classname - l = [] - for entry in map(str, value): - if not num_re.match(entry): - try: - entry = self.db.classes[link].lookup(entry) - except: - raise ValueError, \ - 'property "%s": %s not a %s'%(key, - entry, link) - l.append(entry) - l.sort() - value = l - # if changed, set it - if value != cl.get(self.nodeid, key): - changed.append(key) - props[key] = value + props, changed = parsePropsFromForm(cl, self.form) cl.set(self.nodeid, **props) - self._post_editnode(self.nodeid, changed) # and some nice feedback for the user message = '%s edited ok'%', '.join(changed) @@ -290,51 +245,8 @@ class Client: def _createnode(self): ''' create a node based on the contents of the form ''' - cn = self.classname - cl = self.db.classes[cn] - props = {} - keys = self.form.keys() - num_re = re.compile('^\d+$') - for key in keys: - if not cl.properties.has_key(key): - continue - proptype = cl.properties[key] - if isinstance(proptype, hyperdb.String): - value = self.form[key].value.strip() - elif isinstance(proptype, hyperdb.Date): - value = date.Date(self.form[key].value.strip()) - elif isinstance(proptype, hyperdb.Interval): - value = date.Interval(self.form[key].value.strip()) - elif isinstance(proptype, hyperdb.Link): - value = self.form[key].value.strip() - # handle key values - link = cl.properties[key].classname - if not num_re.match(value): - try: - value = self.db.classes[link].lookup(value) - except: - raise ValueError, 'property "%s": %s not a %s'%( - key, value, link) - elif isinstance(proptype, hyperdb.Multilink): - value = self.form[key] - if type(value) != type([]): - value = [i.strip() for i in value.value.split(',')] - else: - value = [i.value.strip() for i in value] - link = cl.properties[key].classname - l = [] - for entry in map(str, value): - if not num_re.match(entry): - try: - entry = self.db.classes[link].lookup(entry) - except: - raise ValueError, \ - 'property "%s": %s not a %s'%(key, - entry, link) - l.append(entry) - l.sort() - value = l - props[key] = value + cl = self.db.classes[self.classname] + props, dummy = parsePropsFromForm(cl, self.form) return cl.create(**props) def _post_editnode(self, nid, changes=None): @@ -376,7 +288,7 @@ class Client: if len(nosy) == 1 and uid in nosy: nosy = 0 if (nosy and props.has_key('messages') and - props['messages'].isMultilinkType and + isinstance(props['messages'], hyperdb.Multilink) and props['messages'].classname == 'msg'): # handle the note @@ -554,8 +466,64 @@ class Client: def __del__(self): self.db.close() +def parsePropsFromForm(cl, form, note_changed=0): + '''Pull properties for the given class out of the form. + ''' + props = {} + changed = [] + keys = form.keys() + num_re = re.compile('^\d+$') + for key in keys: + if not cl.properties.has_key(key): + continue + proptype = cl.properties[key] + if isinstance(proptype, hyperdb.String): + value = form[key].value.strip() + elif isinstance(proptype, hyperdb.Date): + value = date.Date(form[key].value.strip()) + elif isinstance(proptype, hyperdb.Interval): + value = date.Interval(form[key].value.strip()) + elif isinstance(proptype, hyperdb.Link): + value = form[key].value.strip() + # handle key values + link = cl.properties[key].classname + if not num_re.match(value): + try: + value = self.db.classes[link].lookup(value) + except: + raise ValueError, 'property "%s": %s not a %s'%( + key, value, link) + elif isinstance(proptype, hyperdb.Multilink): + value = form[key] + if type(value) != type([]): + value = [i.strip() for i in value.value.split(',')] + else: + value = [i.value.strip() for i in value] + link = cl.properties[key].classname + l = [] + for entry in map(str, value): + if not num_re.match(entry): + try: + entry = self.db.classes[link].lookup(entry) + except: + raise ValueError, \ + 'property "%s": %s not a %s'%(key, + entry, link) + l.append(entry) + l.sort() + value = l + props[key] = value + # if changed, set it + if note_changed and value != cl.get(self.nodeid, key): + changed.append(key) + props[key] = value + return props, changed + # # $Log: not supported by cvs2svn $ +# Revision 1.20 2001/08/12 06:32:36 richard +# using isinstance(blah, Foo) now instead of isFooType +# # Revision 1.19 2001/08/07 00:24:42 richard # stupid typo # diff --git a/roundup/date.py b/roundup/date.py index db19797..fac763d 100644 --- a/roundup/date.py +++ b/roundup/date.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: date.py,v 1.10 2001-08-07 00:24:42 richard Exp $ +# $Id: date.py,v 1.11 2001-08-15 23:43:18 richard Exp $ import time, re, calendar @@ -71,8 +71,6 @@ class Date: >>> Date("14:25", -5) ''' - isDate = 1 - def __init__(self, spec='.', offset=0): """Construct a date given a specification and a time zone offset. @@ -116,7 +114,7 @@ class Date: 1. an interval from this date to produce another date. 2. a date from this date to produce an interval. """ - if other.isDate: + if isinstance(other, Date): # TODO this code will fall over laughing if the dates cross # leap years, phases of the moon, .... a = calendar.timegm((self.year, self.month, self.day, self.hour, @@ -239,8 +237,6 @@ class Interval: >>> Date(". + 2d") - Interval("3w") ''' - isInterval = 1 - def __init__(self, spec, sign=1): """Construct an interval given a specification.""" if type(spec) == type(''): @@ -380,6 +376,9 @@ if __name__ == '__main__': # # $Log: not supported by cvs2svn $ +# Revision 1.10 2001/08/07 00:24:42 richard +# stupid typo +# # Revision 1.9 2001/08/07 00:15:51 richard # Added the copyright/license notice to (nearly) all files at request of # Bizar Software. diff --git a/roundup/htmltemplate.py b/roundup/htmltemplate.py index 50ac79f..1a8e1d9 100644 --- a/roundup/htmltemplate.py +++ b/roundup/htmltemplate.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: htmltemplate.py,v 1.19 2001-08-12 06:32:36 richard Exp $ +# $Id: htmltemplate.py,v 1.20 2001-08-15 23:43:18 richard Exp $ import os, re, StringIO, urllib, cgi, errno @@ -94,8 +94,9 @@ class Field(Base): # TODO: pull the value from the form if isinstance(propclass, hyperdb.Multilink): value = [] else: value = '' - if isinstance((propclass.isStringType or propclass, hyperdb.Date) or - propclass.isIntervalType): + if (isinstance(propclass, hyperdb.String) or + isinstance(propclass, hyperdb.Date) or + isinstance(propclass, hyperdb.Interval)): size = size or 30 if value is None: value = '' @@ -297,7 +298,8 @@ class Checklist(Base): value = self.filterspec.get(property, []) else: value = [] - if isinstance(propclass.isLinkType or propclass, hyperdb.Multilink): + if (isinstance(propclass, hyperdb.Link) or + isinstance(propclass, hyperdb.Multilink)): linkcl = self.db.classes[propclass.classname] l = [] k = linkcl.labelprop() @@ -740,6 +742,9 @@ def newitem(client, templates, db, classname, form, replace=re.compile( # # $Log: not supported by cvs2svn $ +# Revision 1.19 2001/08/12 06:32:36 richard +# using isinstance(blah, Foo) now instead of isFooType +# # Revision 1.18 2001/08/07 00:24:42 richard # stupid typo # diff --git a/roundup/hyperdb.py b/roundup/hyperdb.py index eeb48b7..d9f79a8 100644 --- a/roundup/hyperdb.py +++ b/roundup/hyperdb.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: hyperdb.py,v 1.15 2001-08-12 06:32:36 richard Exp $ +# $Id: hyperdb.py,v 1.16 2001-08-15 23:43:18 richard Exp $ # standard python modules import cPickle, re, string @@ -190,11 +190,11 @@ class Class: raise TypeError, 'new property "%s" not a string'%key elif isinstance(prop, Date): - if not hasattr(value, 'isDate'): + if not isinstance(value, date.Date): raise TypeError, 'new property "%s" not a Date'% key elif isinstance(prop, Interval): - if not hasattr(value, 'isInterval'): + if not isinstance(value, date.Interval): raise TypeError, 'new property "%s" not an Interval'% key for key, prop in self.properties.items(): @@ -345,11 +345,11 @@ class Class: raise TypeError, 'new property "%s" not a string'%key elif isinstance(prop, Date): - if not hasattr(value, 'isDate'): + if not isinstance(value, date.Date): raise TypeError, 'new property "%s" not a Date'% key elif isinstance(prop, Interval): - if not hasattr(value, 'isInterval'): + if not isinstance(value, date.Interval): raise TypeError, 'new property "%s" not an Interval'% key node[key] = value @@ -675,7 +675,8 @@ class Class: av = an[prop] = av.lower() if bv and bv[0] in string.uppercase: bv = bn[prop] = bv.lower() - if isinstance(propclass.isStringType or propclass, Date): + if (isinstance(propclass, String) or + isinstance(propclass, Date)): if dir == '+': r = cmp(av, bv) if r != 0: return r @@ -806,6 +807,9 @@ def Choice(name, *options): # # $Log: not supported by cvs2svn $ +# Revision 1.15 2001/08/12 06:32:36 richard +# using isinstance(blah, Foo) now instead of isFooType +# # Revision 1.14 2001/08/07 00:24:42 richard # stupid typo # -- 2.30.2