Skip to content

Commit 5d82207

Browse files
author
Richard Jones
committed
more htmltemplate tests and cleanup
1 parent 7062560 commit 5d82207

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

roundup/htmltemplate.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: htmltemplate.py,v 1.65 2002-01-22 00:12:06 richard Exp $
18+
# $Id: htmltemplate.py,v 1.66 2002-01-22 06:35:40 richard Exp $
1919

2020
__doc__ = """
2121
Template engine.
@@ -299,19 +299,18 @@ def do_link(self, property=None, is_download=0):
299299
'''
300300
if not self.nodeid and self.form is None:
301301
return _('[Link: not called from item]')
302+
303+
# get the value
304+
value = self.determine_value(property)
305+
if not value:
306+
return _('[no %(propname)s]')%{'propname':property.capitalize()}
307+
302308
propclass = self.properties[property]
303-
if self.nodeid:
304-
value = self.cl.get(self.nodeid, property)
305-
else:
306-
if isinstance(propclass, hyperdb.Multilink): value = []
307-
elif isinstance(propclass, hyperdb.Link): value = None
308-
else: value = ''
309309
if isinstance(propclass, hyperdb.Link):
310310
linkname = propclass.classname
311-
if value is None: return '[no %s]'%property.capitalize()
312311
linkcl = self.db.classes[linkname]
313312
k = linkcl.labelprop()
314-
linkvalue = linkcl.get(value, k)
313+
linkvalue = cgi.escape(linkcl.get(value, k))
315314
if is_download:
316315
return '<a href="%s%s/%s">%s</a>'%(linkname, value,
317316
linkvalue, linkvalue)
@@ -321,20 +320,16 @@ def do_link(self, property=None, is_download=0):
321320
linkname = propclass.classname
322321
linkcl = self.db.classes[linkname]
323322
k = linkcl.labelprop()
324-
if not value:
325-
return _('[no %(propname)s]')%{'propname': property.capitalize()}
326323
l = []
327324
for value in value:
328-
linkvalue = linkcl.get(value, k)
325+
linkvalue = cgi.escape(linkcl.get(value, k))
329326
if is_download:
330327
l.append('<a href="%s%s/%s">%s</a>'%(linkname, value,
331328
linkvalue, linkvalue))
332329
else:
333330
l.append('<a href="%s%s">%s</a>'%(linkname, value,
334331
linkvalue))
335332
return ', '.join(l)
336-
if isinstance(propclass, hyperdb.String) and value == '':
337-
return _('[no %(propname)s]')%{'propname': property.capitalize()}
338333
if is_download:
339334
return '<a href="%s%s/%s">%s</a>'%(self.classname, self.nodeid,
340335
value, value)
@@ -1043,6 +1038,10 @@ def render(self, form):
10431038

10441039
#
10451040
# $Log: not supported by cvs2svn $
1041+
# Revision 1.65 2002/01/22 00:12:06 richard
1042+
# Wrote more unit tests for htmltemplate, and while I was at it, I polished
1043+
# off the implementation of some of the functions so they behave sanely.
1044+
#
10461045
# Revision 1.64 2002/01/21 03:25:59 richard
10471046
# oops
10481047
#

test/test_htmltemplate.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
# but WITHOUT ANY WARRANTY; without even the implied warranty of
99
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1010
#
11-
# $Id: test_htmltemplate.py,v 1.2 2002-01-22 00:12:07 richard Exp $
11+
# $Id: test_htmltemplate.py,v 1.3 2002-01-22 06:35:40 richard Exp $
1212

1313
import unittest, cgi
1414

15+
from roundup import date, password
1516
from roundup.htmltemplate import TemplateFunctions
16-
from roundup import date
17-
from roundup import password
17+
from roundup.i18n import _
1818
from roundup.hyperdb import String, Password, Date, Interval, Link, Multilink
1919

2020
class Class:
2121
def get(self, nodeid, attribute, default=None):
2222
if attribute == 'string':
2323
return 'Node %s: I am a string'%nodeid
24+
elif attribute == 'filename':
25+
return 'file.foo'
2426
elif attribute == 'date':
2527
return date.Date('2000-01-01')
2628
elif attribute == 'interval':
@@ -40,7 +42,8 @@ def list(self):
4042
def getprops(self):
4143
return {'string': String(), 'date': Date(), 'interval': Interval(),
4244
'link': Link('other'), 'multilink': Multilink('other'),
43-
'password': Password(), 'html': String(), 'key': String()}
45+
'password': Password(), 'html': String(), 'key': String(),
46+
'novalue': String(), 'filename': String()}
4447
def labelprop(self):
4548
return 'key'
4649

@@ -58,6 +61,7 @@ def setUp(self):
5861
self.tf = tf = TemplateFunctions()
5962
tf.nodeid = '1'
6063
tf.cl = Class()
64+
tf.classname = 'test_class'
6165
tf.properties = tf.cl.getprops()
6266
tf.db = Database()
6367

@@ -134,6 +138,12 @@ def testField_multilink(self):
134138
'<input name="multilink" size="10" value="the key,the key">')
135139

136140
# def do_menu(self, property, size=None, height=None, showid=0):
141+
def testMenu_nonlinks(self):
142+
self.assertEqual(self.tf.do_menu('string'), _('[Menu: not a link]'))
143+
self.assertEqual(self.tf.do_menu('date'), _('[Menu: not a link]'))
144+
self.assertEqual(self.tf.do_menu('interval'), _('[Menu: not a link]'))
145+
self.assertEqual(self.tf.do_menu('password'), _('[Menu: not a link]'))
146+
137147
def testMenu_link(self):
138148
self.assertEqual(self.tf.do_menu('link'), '''<select name="link">
139149
<option value="-1">- no selection -</option>
@@ -170,12 +180,45 @@ def testMenu_multilink(self):
170180
<option selected value="2">other2: the key</option>
171181
</select>''')
172182

183+
# def do_link(self, property=None, is_download=0):
184+
def testLink_novalue(self):
185+
self.assertEqual(self.tf.do_link('novalue'),
186+
_('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
187+
188+
def testLink_string(self):
189+
self.assertEqual(self.tf.do_link('string'),
190+
'<a href="test_class1">Node 1: I am a string</a>')
191+
192+
def testLink_file(self):
193+
self.assertEqual(self.tf.do_link('filename', is_download=1),
194+
'<a href="test_class1/file.foo">file.foo</a>')
195+
196+
def testLink_date(self):
197+
self.assertEqual(self.tf.do_link('date'),
198+
'<a href="test_class1">2000-01-01.00:00:00</a>')
199+
200+
def testLink_interval(self):
201+
self.assertEqual(self.tf.do_link('interval'),
202+
'<a href="test_class1">- 3d</a>')
203+
204+
def testLink_link(self):
205+
self.assertEqual(self.tf.do_link('link'),
206+
'<a href="other1">the key</a>')
207+
208+
def testLink_multilink(self):
209+
self.assertEqual(self.tf.do_link('multilink'),
210+
'<a href="other1">the key</a>, <a href="other2">the key</a>')
211+
173212
def suite():
174213
return unittest.makeSuite(NodeCase, 'test')
175214

176215

177216
#
178217
# $Log: not supported by cvs2svn $
218+
# Revision 1.2 2002/01/22 00:12:07 richard
219+
# Wrote more unit tests for htmltemplate, and while I was at it, I polished
220+
# off the implementation of some of the functions so they behave sanely.
221+
#
179222
# Revision 1.1 2002/01/21 11:05:48 richard
180223
# New tests for htmltemplate (well, it's a beginning)
181224
#

0 commit comments

Comments
 (0)