Code

Added documentaion.
[roundup.git] / roundup / htmltemplate.py
index f39a72bc80daf69024f5a22d81069c1aed1fa7bf..ed0fefff9b5ae62f6a6cac876c86aca879395b78 100644 (file)
@@ -1,15 +1,16 @@
-# $Id: htmltemplate.py,v 1.6 2001-07-29 04:06:42 richard Exp $
+# $Id: htmltemplate.py,v 1.16 2001-08-01 03:52:23 richard Exp $
 
 import os, re, StringIO, urllib, cgi, errno
 
 import hyperdb, date
 
 class Base:
-    def __init__(self, db, templates, classname, nodeid=None, form=None):
+    def __init__(self, db, templates, classname, nodeid=None, form=None,
+            filterspec=None):
         # TODO: really not happy with the way templates is passed on here
         self.db, self.templates = db, templates
         self.classname, self.nodeid = classname, nodeid
-        self.form = form
+        self.form, self.filterspec = form, filterspec
         self.cl = self.db.classes[self.classname]
         self.properties = self.cl.getprops()
 
@@ -41,32 +42,12 @@ class Plain(Base):
             value = str(value)
         elif propclass.isLinkType:
             linkcl = self.db.classes[propclass.classname]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             if value: value = str(linkcl.get(value, k))
             else: value = '[unselected]'
         elif propclass.isMultilinkType:
             linkcl = self.db.classes[propclass.classname]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             value = ', '.join([linkcl.get(i, k) for i in value])
         else:
             s = 'Plain: bad propclass "%s"'%propclass
@@ -77,11 +58,21 @@ class Field(Base):
         to be edited
     '''
     def __call__(self, property, size=None, height=None, showid=0):
-        if not self.nodeid and self.form is None:
+        if not self.nodeid and self.form is None and self.filterspec is None:
             return '[Field: not called from item]'
         propclass = self.properties[property]
         if self.nodeid:
-            value = self.cl.get(self.nodeid, property)
+            value = self.cl.get(self.nodeid, property, None)
+            # TODO: remove this from the code ... it's only here for
+            # handling schema changes, and they should be handled outside
+            # of this code...
+            if propclass.isMultilinkType and value is None:
+                value = []
+        elif self.filterspec is not None:
+            if propclass.isMultilinkType:
+                value = self.filterspec.get(property, [])
+            else:
+                value = self.filterspec.get(property, '')
         else:
             # TODO: pull the value from the form
             if propclass.isMultilinkType: value = []
@@ -98,17 +89,7 @@ class Field(Base):
         elif propclass.isLinkType:
             linkcl = self.db.classes[propclass.classname]
             l = ['<select name="%s">'%property]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             for optionid in linkcl.list():
                 option = linkcl.get(optionid, k)
                 s = ''
@@ -128,17 +109,7 @@ class Field(Base):
             list = linkcl.list()
             height = height or min(len(list), 7)
             l = ['<select multiple name="%s" size="%s">'%(property, height)]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             for optionid in list:
                 option = linkcl.get(optionid, k)
                 s = ''
@@ -171,17 +142,7 @@ class Menu(Base):
         if propclass.isLinkType:
             linkcl = self.db.classes[propclass.classname]
             l = ['<select name="%s">'%property]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             for optionid in linkcl.list():
                 option = linkcl.get(optionid, k)
                 s = ''
@@ -195,17 +156,7 @@ class Menu(Base):
             list = linkcl.list()
             height = height or min(len(list), 7)
             l = ['<select multiple name="%s" size="%s">'%(property, height)]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             for optionid in list:
                 option = linkcl.get(optionid, k)
                 s = ''
@@ -241,32 +192,12 @@ class Link(Base):
             if value is None:
                 return '[not assigned]'
             linkcl = self.db.classes[propclass.classname]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             linkvalue = linkcl.get(value, k)
             return '<a href="%s%s">%s</a>'%(linkcl, value, linkvalue)
         if propclass.isMultilinkType:
             linkcl = self.db.classes[propclass.classname]
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             l = []
             for value in value:
                 linkvalue = linkcl.get(value, k)
@@ -345,30 +276,22 @@ class Checklist(Base):
         propclass = self.properties[property]
         if self.nodeid:
             value = self.cl.get(self.nodeid, property)
+        elif self.filterspec is not None:
+            value = self.filterspec.get(property, [])
         else:
             value = []
         if propclass.isLinkType or propclass.isMultilinkType:
             linkcl = self.db.classes[propclass.classname]
             l = []
-            k = linkcl.getkey()
-            # if the linked-to class doesn't have a key property, then try
-            # 'name', then 'title' and then just use a random one.
-            if not k:
-                linkprops = linkcl.getprops()
-                if linkprops.has_key('name'):
-                    k = 'name'
-                elif linkprops.has_key('title'):
-                    k = 'title'
-                else: 
-                    k = linkprops.keys()[0]
+            k = linkcl.labelprop()
             for optionid in linkcl.list():
                 option = linkcl.get(optionid, k)
-                if optionid in value:
+                if optionid in value or option in value:
                     checked = 'checked'
                 else:
                     checked = ''
                 l.append('%s:<input type="checkbox" %s name="%s" value="%s">'%(
-                    option, checked, propclass.classname, option))
+                    option, checked, property, option))
             return '\n'.join(l)
         return '[Checklist: not a link]'
 
@@ -403,6 +326,9 @@ class History(Base):
     ''' list the history of the item
     '''
     def __call__(self, **args):
+        if self.nodeid is None:
+            return "[History: node doesn't exist]"
+
         l = ['<table width=100% border=0 cellspacing=0 cellpadding=2>',
             '<tr class="list-header">',
             '<td><span class="list-item"><strong>Date</strong></span></td>',
@@ -476,14 +402,14 @@ def sortby(sort_name, columns, filter, sort, group, filterspec):
     for name in sort:
         dir = name[0]
         if dir == '-':
-            dir = ''
-        else:
             name = name[1:]
+        else:
+            dir = ''
         if sort_name == name:
-            if dir == '':
-                s_dir = '-'
-            elif dir == '-':
+            if dir == '-':
                 s_dir = ''
+            else:
+                s_dir = '-'
         else:
             m.append(dir+urllib.quote(name))
     m.insert(0, s_dir+urllib.quote(sort_name))
@@ -495,22 +421,23 @@ def index(client, templates, db, classname, filterspec={}, filter=[],
         columns=[], sort=[], group=[], show_display_form=1, nodeids=None,
         col_re=re.compile(r'<property\s+name="([^>]+)">')):
     globals = {
-        'plain': Plain(db, templates, classname, form={}),
-        'field': Field(db, templates, classname, form={}),
-        'menu': Menu(db, templates, classname, form={}),
-        'link': Link(db, templates, classname, form={}),
-        'count': Count(db, templates, classname, form={}),
-        'reldate': Reldate(db, templates, classname, form={}),
-        'download': Download(db, templates, classname, form={}),
-        'checklist': Checklist(db, templates, classname, form={}),
-        'list': List(db, templates, classname, form={}),
-        'history': History(db, templates, classname, form={}),
-        'submit': Submit(db, templates, classname, form={}),
-        'note': Note(db, templates, classname, form={})
+        'plain': Plain(db, templates, classname, filterspec=filterspec),
+        'field': Field(db, templates, classname, filterspec=filterspec),
+        'menu': Menu(db, templates, classname, filterspec=filterspec),
+        'link': Link(db, templates, classname, filterspec=filterspec),
+        'count': Count(db, templates, classname, filterspec=filterspec),
+        'reldate': Reldate(db, templates, classname, filterspec=filterspec),
+        'download': Download(db, templates, classname, filterspec=filterspec),
+        'checklist': Checklist(db, templates, classname, filterspec=filterspec),
+        'list': List(db, templates, classname, filterspec=filterspec),
+        'history': History(db, templates, classname, filterspec=filterspec),
+        'submit': Submit(db, templates, classname, filterspec=filterspec),
+        'note': Note(db, templates, classname, filterspec=filterspec)
     }
     cl = db.classes[classname]
     properties = cl.getprops()
     w = client.write
+    w('<form>')
 
     try:
         template = open(os.path.join(templates, classname+'.filter')).read()
@@ -521,28 +448,26 @@ def index(client, templates, db, classname, filterspec={}, filter=[],
         all_filters = []
     if template and filter:
         # display the filter section
-        w('<form>')
         w('<table width=100% border=0 cellspacing=0 cellpadding=2>')
         w('<tr class="location-bar">')
         w(' <th align="left" colspan="2">Filter specification...</th>')
         w('</tr>')
         replace = IndexTemplateReplace(globals, locals(), filter)
         w(replace.go(template))
-        if columns:
-            w('<input type="hidden" name=":columns" value="%s">'%','.join(columns))
-        if filter:
-            w('<input type="hidden" name=":filter" value="%s">'%','.join(filter))
-        if sort:
-            w('<input type="hidden" name=":sort" value="%s">'%','.join(sort))
-        if group:
-            w('<input type="hidden" name=":group" value="%s">'%','.join(group))
-        for k, v in filterspec.items():
-            if type(v) == type([]): v = ','.join(v)
-            w('<input type="hidden" name="%s" value="%s">'%(k, v))
         w('<tr class="location-bar"><td width="1%%">&nbsp;</td>')
         w('<td><input type="submit" value="Redisplay"></td></tr>')
         w('</table>')
-        w('</form>')
+
+    # If the filters aren't being displayed, then hide their current
+    # value in the form
+    if not filter:
+        for k, v in filterspec.items():
+            if type(v) == type([]): v = ','.join(v)
+            w('<input type="hidden" name="%s" value="%s">'%(k, v))
+
+    # make sure that the sorting doesn't get lost either
+    if sort:
+        w('<input type="hidden" name=":sort" value="%s">'%','.join(sort))
 
     # XXX deviate from spec here ...
     # load the index section template and figure the default columns from it
@@ -630,13 +555,8 @@ def index(client, templates, db, classname, filterspec={}, filter=[],
         return
 
     # now add in the filter/columns/group/etc config table form
-    w('<p><form>')
+    w('<p>')
     w('<table width=100% border=0 cellspacing=0 cellpadding=2>')
-    for k,v in filterspec.items():
-        if type(v) == type([]): v = ','.join(v)
-        w('<input type="hidden" name="%s" value="%s">'%(k, v))
-    if sort:
-        w('<input type="hidden" name=":sort" value="%s">'%','.join(sort))
     names = []
     for name in cl.getprops().keys():
         if name in all_filters or name in all_columns:
@@ -790,13 +710,56 @@ def newitem(client, templates, db, classname, form, replace=re.compile(
         s = open(os.path.join(templates, classname+'.newitem')).read()
     except:
         s = open(os.path.join(templates, classname+'.item')).read()
-    w('<form action="new%s">'%classname)
+    w('<form action="new%s" method="POST" enctype="multipart/form-data">'%classname)
+    for key in form.keys():
+        if key[0] == ':':
+            value = form[key].value
+            if type(value) != type([]): value = [value]
+            for value in value:
+                w('<input type="hidden" name="%s" value="%s">'%(key, value))
     replace = ItemTemplateReplace(globals, locals(), None, None)
     w(replace.go(s))
     w('</form>')
 
 #
 # $Log: not supported by cvs2svn $
+# Revision 1.15  2001/07/30 08:12:17  richard
+# Added time logging and file uploading to the templates.
+#
+# Revision 1.14  2001/07/30 06:17:45  richard
+# Features:
+#  . Added ability for cgi newblah forms to indicate that the new node
+#    should be linked somewhere.
+# Fixed:
+#  . Fixed the agument handling for the roundup-admin find command.
+#  . Fixed handling of summary when no note supplied for newblah. Again.
+#  . Fixed detection of no form in htmltemplate Field display.
+#
+# Revision 1.13  2001/07/30 02:37:53  richard
+# Temporary measure until we have decent schema migration.
+#
+# Revision 1.12  2001/07/30 01:24:33  richard
+# Handles new node display now.
+#
+# Revision 1.11  2001/07/29 09:31:35  richard
+# oops
+#
+# Revision 1.10  2001/07/29 09:28:23  richard
+# Fixed sorting by clicking on column headings.
+#
+# Revision 1.9  2001/07/29 08:27:40  richard
+# Fixed handling of passed-in values in form elements (ie. during a
+# drill-down)
+#
+# Revision 1.8  2001/07/29 07:01:39  richard
+# Added vim command to all source so that we don't get no steenkin' tabs :)
+#
+# Revision 1.7  2001/07/29 05:36:14  richard
+# Cleanup of the link label generation.
+#
+# Revision 1.6  2001/07/29 04:06:42  richard
+# Fixed problem in link display when Link value is None.
+#
 # Revision 1.5  2001/07/28 08:17:09  richard
 # fixed use of stylesheet
 #
@@ -816,3 +779,5 @@ def newitem(client, templates, db, classname, form, replace=re.compile(
 # Revision 1.1  2001/07/22 11:58:35  richard
 # More Grande Splite
 #
+#
+# vim: set filetype=python ts=4 sw=4 et si