comparison roundup/cgi/templating.py @ 1931:f1e5e5115c29

Always sort MultilinkHTMLProperty in the correct order... ...usually alphabetically (feature [SF#790512]). Extract find_sort_key method. Use nested scopes in make_sort_function.
author Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
date Thu, 20 Nov 2003 15:20:21 +0000
parents 9bf9cf980656
children cd7af2579d20
comparison
equal deleted inserted replaced
1930:71056b09f2bf 1931:f1e5e5115c29
1 from __future__ import nested_scopes
2
1 import sys, cgi, urllib, os, re, os.path, time, errno, mimetypes 3 import sys, cgi, urllib, os, re, os.path, time, errno, mimetypes
2 4
3 from roundup import hyperdb, date, rcsv 5 from roundup import hyperdb, date, rcsv
4 from roundup.i18n import _ 6 from roundup.i18n import _
5 7
1191 **conditions): 1193 **conditions):
1192 ''' Render a form select list for this property 1194 ''' Render a form select list for this property
1193 ''' 1195 '''
1194 value = self._value 1196 value = self._value
1195 1197
1196 # sort function
1197 sortfunc = make_sort_function(self._db, self._prop.classname)
1198
1199 linkcl = self._db.getclass(self._prop.classname) 1198 linkcl = self._db.getclass(self._prop.classname)
1200 l = ['<select name="%s">'%self._formname] 1199 l = ['<select name="%s">'%self._formname]
1201 k = linkcl.labelprop(1) 1200 k = linkcl.labelprop(1)
1202 s = '' 1201 s = ''
1203 if value is None: 1202 if value is None:
1248 ''' Multilink HTMLProperty 1247 ''' Multilink HTMLProperty
1249 1248
1250 Also be iterable, returning a wrapper object like the Link case for 1249 Also be iterable, returning a wrapper object like the Link case for
1251 each entry in the multilink. 1250 each entry in the multilink.
1252 ''' 1251 '''
1252 def __init__(self, *args, **kwargs):
1253 HTMLProperty.__init__(self, *args, **kwargs)
1254 if self._value:
1255 self._value.sort(make_sort_function(self._db, self._prop.classname))
1256
1253 def __len__(self): 1257 def __len__(self):
1254 ''' length of the multilink ''' 1258 ''' length of the multilink '''
1255 return len(self._value) 1259 return len(self._value)
1256 1260
1257 def __getattr__(self, attr): 1261 def __getattr__(self, attr):
1300 return value 1304 return value
1301 1305
1302 def field(self, size=30, showid=0): 1306 def field(self, size=30, showid=0):
1303 ''' Render a form edit field for the property 1307 ''' Render a form edit field for the property
1304 ''' 1308 '''
1305 sortfunc = make_sort_function(self._db, self._prop.classname)
1306 linkcl = self._db.getclass(self._prop.classname) 1309 linkcl = self._db.getclass(self._prop.classname)
1307 value = self._value[:] 1310 value = self._value[:]
1308 if value:
1309 value.sort(sortfunc)
1310 # map the id to the label property 1311 # map the id to the label property
1311 if not linkcl.getkey(): 1312 if not linkcl.getkey():
1312 showid=1 1313 showid=1
1313 if not showid: 1314 if not showid:
1314 k = linkcl.labelprop(1) 1315 k = linkcl.labelprop(1)
1320 **conditions): 1321 **conditions):
1321 ''' Render a form select list for this property 1322 ''' Render a form select list for this property
1322 ''' 1323 '''
1323 value = self._value 1324 value = self._value
1324 1325
1325 # sort function
1326 sortfunc = make_sort_function(self._db, self._prop.classname)
1327
1328 linkcl = self._db.getclass(self._prop.classname) 1326 linkcl = self._db.getclass(self._prop.classname)
1329 if linkcl.getprops().has_key('order'): 1327 sort_on = ('+', find_sort_key(linkcl))
1330 sort_on = ('+', 'order') 1328 options = linkcl.filter(None, conditions, sort_on)
1331 else:
1332 sort_on = ('+', linkcl.labelprop())
1333 options = linkcl.filter(None, conditions, sort_on, (None,None))
1334 height = height or min(len(options), 7) 1329 height = height or min(len(options), 7)
1335 l = ['<select multiple name="%s" size="%s">'%(self._formname, height)] 1330 l = ['<select multiple name="%s" size="%s">'%(self._formname, height)]
1336 k = linkcl.labelprop(1) 1331 k = linkcl.labelprop(1)
1337 1332
1338 # make sure we list the current values if they're retired 1333 # make sure we list the current values if they're retired
1384 1379
1385 def make_sort_function(db, classname): 1380 def make_sort_function(db, classname):
1386 '''Make a sort function for a given class 1381 '''Make a sort function for a given class
1387 ''' 1382 '''
1388 linkcl = db.getclass(classname) 1383 linkcl = db.getclass(classname)
1389 if linkcl.getprops().has_key('order'): 1384 sort_on = find_sort_key(linkcl)
1390 sort_on = 'order' 1385 def sortfunc(a, b):
1391 else:
1392 sort_on = linkcl.labelprop()
1393 def sortfunc(a, b, linkcl=linkcl, sort_on=sort_on):
1394 return cmp(linkcl.get(a, sort_on), linkcl.get(b, sort_on)) 1386 return cmp(linkcl.get(a, sort_on), linkcl.get(b, sort_on))
1395 return sortfunc 1387 return sortfunc
1388
1389 def find_sort_key(linkcl):
1390 if linkcl.getprops().has_key('order'):
1391 return 'order'
1392 else:
1393 return linkcl.labelprop()
1396 1394
1397 def handleListCGIValue(value): 1395 def handleListCGIValue(value):
1398 ''' Value is either a single item or a list of items. Each item has a 1396 ''' Value is either a single item or a list of items. Each item has a
1399 .value that we're actually interested in. 1397 .value that we're actually interested in.
1400 ''' 1398 '''

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