Code

Fixed some isFooTypes that I missed.
authorrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Wed, 15 Aug 2001 23:43:18 +0000 (23:43 +0000)
committerrichard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2>
Wed, 15 Aug 2001 23:43:18 +0000 (23:43 +0000)
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
roundup/date.py
roundup/htmltemplate.py
roundup/hyperdb.py

index 4f43ab3707e9ef642cd4d4cd547478ec749a7322..e029d41ba2b53836e1ac923caeacffbc09298c22 100644 (file)
 # 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
 #
index db197974a4589a89ba8de31b4aa69a170a5a650d..fac763db6dd9aba6f9d35809bb143860c8a3d0a1 100644 (file)
@@ -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)
         <Date 2000-06-25.19:25:00>
     '''
-    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")
         <Date 2000-06-07.00:34:02>
     '''
-    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.
index 50ac79fe0a8ac5299e00ff5b2ee413347137d8e5..1a8e1d91efb3e4901de092a4b8c33835902b7111 100644 (file)
@@ -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
 #
index eeb48b752669ba8e21462ed8c9edab29e029dd7b..d9f79a8064112eb143daf77bb4aade80bbf7b602 100644 (file)
@@ -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
 #