Skip to content

Commit 9ca5b2f

Browse files
author
Richard Jones
committed
Cleanup of the link label generation.
1 parent de472c9 commit 9ca5b2f

File tree

2 files changed

+39
-101
lines changed

2 files changed

+39
-101
lines changed

roundup/htmltemplate.py

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: htmltemplate.py,v 1.6 2001-07-29 04:06:42 richard Exp $
1+
# $Id: htmltemplate.py,v 1.7 2001-07-29 05:36:14 richard Exp $
22

33
import os, re, StringIO, urllib, cgi, errno
44

@@ -41,32 +41,12 @@ def __call__(self, property):
4141
value = str(value)
4242
elif propclass.isLinkType:
4343
linkcl = self.db.classes[propclass.classname]
44-
k = linkcl.getkey()
45-
# if the linked-to class doesn't have a key property, then try
46-
# 'name', then 'title' and then just use a random one.
47-
if not k:
48-
linkprops = linkcl.getprops()
49-
if linkprops.has_key('name'):
50-
k = 'name'
51-
elif linkprops.has_key('title'):
52-
k = 'title'
53-
else:
54-
k = linkprops.keys()[0]
44+
k = linkcl.labelprop()
5545
if value: value = str(linkcl.get(value, k))
5646
else: value = '[unselected]'
5747
elif propclass.isMultilinkType:
5848
linkcl = self.db.classes[propclass.classname]
59-
k = linkcl.getkey()
60-
# if the linked-to class doesn't have a key property, then try
61-
# 'name', then 'title' and then just use a random one.
62-
if not k:
63-
linkprops = linkcl.getprops()
64-
if linkprops.has_key('name'):
65-
k = 'name'
66-
elif linkprops.has_key('title'):
67-
k = 'title'
68-
else:
69-
k = linkprops.keys()[0]
49+
k = linkcl.labelprop()
7050
value = ', '.join([linkcl.get(i, k) for i in value])
7151
else:
7252
s = 'Plain: bad propclass "%s"'%propclass
@@ -98,17 +78,7 @@ def __call__(self, property, size=None, height=None, showid=0):
9878
elif propclass.isLinkType:
9979
linkcl = self.db.classes[propclass.classname]
10080
l = ['<select name="%s">'%property]
101-
k = linkcl.getkey()
102-
# if the linked-to class doesn't have a key property, then try
103-
# 'name', then 'title' and then just use a random one.
104-
if not k:
105-
linkprops = linkcl.getprops()
106-
if linkprops.has_key('name'):
107-
k = 'name'
108-
elif linkprops.has_key('title'):
109-
k = 'title'
110-
else:
111-
k = linkprops.keys()[0]
81+
k = linkcl.labelprop()
11282
for optionid in linkcl.list():
11383
option = linkcl.get(optionid, k)
11484
s = ''
@@ -128,17 +98,7 @@ def __call__(self, property, size=None, height=None, showid=0):
12898
list = linkcl.list()
12999
height = height or min(len(list), 7)
130100
l = ['<select multiple name="%s" size="%s">'%(property, height)]
131-
k = linkcl.getkey()
132-
# if the linked-to class doesn't have a key property, then try
133-
# 'name', then 'title' and then just use a random one.
134-
if not k:
135-
linkprops = linkcl.getprops()
136-
if linkprops.has_key('name'):
137-
k = 'name'
138-
elif linkprops.has_key('title'):
139-
k = 'title'
140-
else:
141-
k = linkprops.keys()[0]
101+
k = linkcl.labelprop()
142102
for optionid in list:
143103
option = linkcl.get(optionid, k)
144104
s = ''
@@ -171,17 +131,7 @@ def __call__(self, property, size=None, height=None, showid=0):
171131
if propclass.isLinkType:
172132
linkcl = self.db.classes[propclass.classname]
173133
l = ['<select name="%s">'%property]
174-
k = linkcl.getkey()
175-
# if the linked-to class doesn't have a key property, then try
176-
# 'name', then 'title' and then just use a random one.
177-
if not k:
178-
linkprops = linkcl.getprops()
179-
if linkprops.has_key('name'):
180-
k = 'name'
181-
elif linkprops.has_key('title'):
182-
k = 'title'
183-
else:
184-
k = linkprops.keys()[0]
134+
k = linkcl.labelprop()
185135
for optionid in linkcl.list():
186136
option = linkcl.get(optionid, k)
187137
s = ''
@@ -195,17 +145,7 @@ def __call__(self, property, size=None, height=None, showid=0):
195145
list = linkcl.list()
196146
height = height or min(len(list), 7)
197147
l = ['<select multiple name="%s" size="%s">'%(property, height)]
198-
k = linkcl.getkey()
199-
# if the linked-to class doesn't have a key property, then try
200-
# 'name', then 'title' and then just use a random one.
201-
if not k:
202-
linkprops = linkcl.getprops()
203-
if linkprops.has_key('name'):
204-
k = 'name'
205-
elif linkprops.has_key('title'):
206-
k = 'title'
207-
else:
208-
k = linkprops.keys()[0]
148+
k = linkcl.labelprop()
209149
for optionid in list:
210150
option = linkcl.get(optionid, k)
211151
s = ''
@@ -241,32 +181,12 @@ def __call__(self, property=None, **args):
241181
if value is None:
242182
return '[not assigned]'
243183
linkcl = self.db.classes[propclass.classname]
244-
k = linkcl.getkey()
245-
# if the linked-to class doesn't have a key property, then try
246-
# 'name', then 'title' and then just use a random one.
247-
if not k:
248-
linkprops = linkcl.getprops()
249-
if linkprops.has_key('name'):
250-
k = 'name'
251-
elif linkprops.has_key('title'):
252-
k = 'title'
253-
else:
254-
k = linkprops.keys()[0]
184+
k = linkcl.labelprop()
255185
linkvalue = linkcl.get(value, k)
256186
return '<a href="%s%s">%s</a>'%(linkcl, value, linkvalue)
257187
if propclass.isMultilinkType:
258188
linkcl = self.db.classes[propclass.classname]
259-
k = linkcl.getkey()
260-
# if the linked-to class doesn't have a key property, then try
261-
# 'name', then 'title' and then just use a random one.
262-
if not k:
263-
linkprops = linkcl.getprops()
264-
if linkprops.has_key('name'):
265-
k = 'name'
266-
elif linkprops.has_key('title'):
267-
k = 'title'
268-
else:
269-
k = linkprops.keys()[0]
189+
k = linkcl.labelprop()
270190
l = []
271191
for value in value:
272192
linkvalue = linkcl.get(value, k)
@@ -350,17 +270,7 @@ def __call__(self, property, **args):
350270
if propclass.isLinkType or propclass.isMultilinkType:
351271
linkcl = self.db.classes[propclass.classname]
352272
l = []
353-
k = linkcl.getkey()
354-
# if the linked-to class doesn't have a key property, then try
355-
# 'name', then 'title' and then just use a random one.
356-
if not k:
357-
linkprops = linkcl.getprops()
358-
if linkprops.has_key('name'):
359-
k = 'name'
360-
elif linkprops.has_key('title'):
361-
k = 'title'
362-
else:
363-
k = linkprops.keys()[0]
273+
k = linkcl.labelprop()
364274
for optionid in linkcl.list():
365275
option = linkcl.get(optionid, k)
366276
if optionid in value:
@@ -797,6 +707,9 @@ def newitem(client, templates, db, classname, form, replace=re.compile(
797707

798708
#
799709
# $Log: not supported by cvs2svn $
710+
# Revision 1.6 2001/07/29 04:06:42 richard
711+
# Fixed problem in link display when Link value is None.
712+
#
800713
# Revision 1.5 2001/07/28 08:17:09 richard
801714
# fixed use of stylesheet
802715
#

roundup/hyperdb.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: hyperdb.py,v 1.5 2001-07-29 04:05:37 richard Exp $
1+
# $Id: hyperdb.py,v 1.6 2001-07-29 05:36:14 richard Exp $
22

33
# standard python modules
44
import cPickle, re, string
@@ -387,6 +387,28 @@ def getkey(self):
387387
"""Return the name of the key property for this class or None."""
388388
return self.key
389389

390+
def labelprop(self, nodeid):
391+
''' Return the property name for a label for the given node.
392+
393+
This method attempts to generate a consistent label for the node.
394+
It tries the following in order:
395+
1. key property
396+
2. "name" property
397+
3. "title" property
398+
4. first property from the sorted property name list
399+
'''
400+
k = self.getkey()
401+
if k:
402+
return k
403+
props = self.getprops()
404+
if props.has_key('name'):
405+
return 'name'
406+
elif props.has_key('title'):
407+
return 'title'
408+
props = props.keys()
409+
props.sort()
410+
return props[0]
411+
390412
# TODO: set up a separate index db file for this? profile?
391413
def lookup(self, keyvalue):
392414
"""Locate a particular node by its key property and return its id.
@@ -767,6 +789,9 @@ def Choice(name, *options):
767789

768790
#
769791
# $Log: not supported by cvs2svn $
792+
# Revision 1.5 2001/07/29 04:05:37 richard
793+
# Added the fabricated property "id".
794+
#
770795
# Revision 1.4 2001/07/27 06:25:35 richard
771796
# Fixed some of the exceptions so they're the right type.
772797
# Removed the str()-ification of node ids so we don't mask oopsy errors any

0 commit comments

Comments
 (0)