changeset 123:51cce9671db0

Cleanup of the link label generation.
author Richard Jones <richard@users.sourceforge.net>
date Sun, 29 Jul 2001 05:36:14 +0000
parents 828632ab0adf
children e395766f8435
files roundup/htmltemplate.py roundup/hyperdb.py
diffstat 2 files changed, 39 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/htmltemplate.py	Sun Jul 29 04:11:28 2001 +0000
+++ b/roundup/htmltemplate.py	Sun Jul 29 05:36:14 2001 +0000
@@ -1,4 +1,4 @@
-# $Id: htmltemplate.py,v 1.6 2001-07-29 04:06:42 richard Exp $
+# $Id: htmltemplate.py,v 1.7 2001-07-29 05:36:14 richard Exp $
 
 import os, re, StringIO, urllib, cgi, errno
 
@@ -41,32 +41,12 @@
             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
@@ -98,17 +78,7 @@
         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 +98,7 @@
             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 +131,7 @@
         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 +145,7 @@
             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 +181,12 @@
             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)
@@ -350,17 +270,7 @@
         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:
@@ -797,6 +707,9 @@
 
 #
 # $Log: not supported by cvs2svn $
+# 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
 #
--- a/roundup/hyperdb.py	Sun Jul 29 04:11:28 2001 +0000
+++ b/roundup/hyperdb.py	Sun Jul 29 05:36:14 2001 +0000
@@ -1,4 +1,4 @@
-# $Id: hyperdb.py,v 1.5 2001-07-29 04:05:37 richard Exp $
+# $Id: hyperdb.py,v 1.6 2001-07-29 05:36:14 richard Exp $
 
 # standard python modules
 import cPickle, re, string
@@ -387,6 +387,28 @@
         """Return the name of the key property for this class or None."""
         return self.key
 
+    def labelprop(self, nodeid):
+        ''' Return the property name for a label for the given node.
+
+        This method attempts to generate a consistent label for the node.
+        It tries the following in order:
+            1. key property
+            2. "name" property
+            3. "title" property
+            4. first property from the sorted property name list
+        '''
+        k = self.getkey()
+        if  k:
+            return k
+        props = self.getprops()
+        if props.has_key('name'):
+            return 'name'
+        elif props.has_key('title'):
+            return 'title'
+        props = props.keys()
+        props.sort()
+        return props[0]
+
     # TODO: set up a separate index db file for this? profile?
     def lookup(self, keyvalue):
         """Locate a particular node by its key property and return its id.
@@ -767,6 +789,9 @@
 
 #
 # $Log: not supported by cvs2svn $
+# Revision 1.5  2001/07/29 04:05:37  richard
+# Added the fabricated property "id".
+#
 # Revision 1.4  2001/07/27 06:25:35  richard
 # Fixed some of the exceptions so they're the right type.
 # Removed the str()-ification of node ids so we don't mask oopsy errors any

Roundup Issue Tracker: http://roundup-tracker.org/