Code

hrm. don't activate detectors if they're just pyc
[roundup.git] / roundup / cgi / templating.py
index 124f97d9092848920539291c5baa824f9f065281..f1b78b448790ed8da5cf57b7dc7e219f38a89e1b 100644 (file)
@@ -202,7 +202,17 @@ class HTMLDatabase:
         l = self._client.db.classes.keys()
         l.sort()
         return [HTMLClass(self._client, cn) for cn in l]
-        
+
+def lookupIds(db, prop, ids, num_re=re.compile('-?\d+')):
+    cl = db.getclass(prop.classname)
+    l = []
+    for entry in ids:
+        if num_re.match(entry):
+            l.append(entry)
+        else:
+            l.append(cl.lookup(entry))
+    return l
+
 class HTMLClass:
     ''' Accesses through a class (either through *class* or *db.<classname>*)
     '''
@@ -222,7 +232,7 @@ class HTMLClass:
     def __getitem__(self, item):
         ''' return an HTMLProperty instance
         '''
-        #print 'getitem', (self, item)
+       #print 'HTMLClass.getitem', (self, item)
 
         # we don't exist
         if item == 'id':
@@ -232,13 +242,29 @@ class HTMLClass:
         prop = self._props[item]
 
         # look up the correct HTMLProperty class
+        form = self._client.form
         for klass, htmlklass in propclasses:
-            if isinstance(prop, hyperdb.Multilink):
-                value = []
+            if not isinstance(prop, klass):
+                continue
+            if form.has_key(item):
+                if isinstance(prop, hyperdb.Multilink):
+                    value = lookupIds(self._db, prop,
+                        handleListCGIValue(form[item]))
+                elif isinstance(prop, hyperdb.Link):
+                    value = form[item].value.strip()
+                    if value:
+                        value = lookupIds(self._db, prop, [value])[0]
+                    else:
+                        value = None
+                else:
+                    value = form[item].value.strip() or None
             else:
-                value = None
-            if isinstance(prop, klass):
-                return htmlklass(self._client, '', prop, item, value)
+                if isinstance(prop, hyperdb.Multilink):
+                    value = []
+                else:
+                    value = None
+            print (prop, value)
+            return htmlklass(self._client, '', prop, item, value)
 
         # no good
         raise KeyError, item
@@ -322,17 +348,24 @@ class HTMLClass:
              for x in self._klass.filter(None, filterspec, sort, group)]
         return l
 
-    def classhelp(self, properties, label='?', width='400', height='400'):
-        '''pop up a javascript window with class help
+    def classhelp(self, properties=None, label='list', width='500',
+            height='400'):
+        ''' Pop up a javascript window with class help
 
-           This generates a link to a popup window which displays the 
-           properties indicated by "properties" of the class named by
-           "classname". The "properties" should be a comma-separated list
-           (eg. 'id,name,description').
+            This generates a link to a popup window which displays the 
+            properties indicated by "properties" of the class named by
+            "classname". The "properties" should be a comma-separated list
+            (eg. 'id,name,description'). Properties defaults to all the
+            properties of a class (excluding id, creator, created and
+            activity).
 
-           You may optionally override the label displayed, the width and
-           height. The popup window will be resizable and scrollable.
+            You may optionally override the label displayed, the width and
+            height. The popup window will be resizable and scrollable.
         '''
+        if properties is None:
+            properties = self._klass.getprops(protected=0).keys()
+            properties.sort()
+            properties = ','.join(properties)
         return '<a href="javascript:help_window(\'%s?:template=help&' \
             ':contentonly=1&properties=%s\', \'%s\', \'%s\')"><b>'\
             '(%s)</b></a>'%(self.classname, properties, width, height, label)
@@ -378,7 +411,7 @@ class HTMLItem:
     def __getitem__(self, item):
         ''' return an HTMLProperty instance
         '''
-        #print 'getitem', (self, item)
+       #print 'HTMLItem.getitem', (self, item)
         if item == 'id':
             return self._nodeid
 
@@ -411,7 +444,12 @@ class HTMLItem:
         return '  <input type="hidden" name=":action" value="edit">\n'\
         '  <input type="submit" name="submit" value="%s">'%label
 
-    # XXX this probably should just return the history items, not the HTML
+    def journal(self, direction='descending'):
+        ''' Return a list of HTMLJournalEntry instances.
+        '''
+        # XXX do this
+        return []
+
     def history(self, direction='descending'):
         l = ['<table class="history">'
              '<tr><th colspan="4" class="header">',
@@ -753,13 +791,13 @@ class LinkHTMLProperty(HTMLProperty):
     '''
     def __getattr__(self, attr):
         ''' return a new HTMLItem '''
-        #print 'getattr', (self, attr, self._value)
+       #print 'Link.getattr', (self, attr, self._value)
         if not self._value:
             raise AttributeError, "Can't access missing value"
         if self._prop.classname == 'user':
-            klass = HTMLItem
-        else:
             klass = HTMLUser
+        else:
+            klass = HTMLItem
         i = klass(self._client, self._prop.classname, self._value)
         return getattr(i, attr)
 
@@ -865,7 +903,7 @@ class MultilinkHTMLProperty(HTMLProperty):
     def __getitem__(self, num):
         ''' iterate and return a new HTMLItem
         '''
-        #print 'getitem', (self, num)
+       #print 'Multi.getitem', (self, num)
         value = self._value[num]
         if self._prop.classname == 'user':
             klass = HTMLUser
@@ -873,6 +911,11 @@ class MultilinkHTMLProperty(HTMLProperty):
             klass = HTMLItem
         return klass(self._client, self._prop.classname, value)
 
+    def __contains__(self, value):
+        ''' Support the "in" operator
+        '''
+        return value in self._value
+
     def reverse(self):
         ''' return the list in reverse order
         '''
@@ -977,7 +1020,10 @@ def handleListCGIValue(value):
     if isinstance(value, type([])):
         return [value.value for value in value]
     else:
-        return value.value.split(',')
+        value = value.value.strip()
+        if not value:
+            return []
+        return value.split(',')
 
 class ShowDict:
     ''' A convenience access to the :columns index parameters