From a335a01910ee4488ff551f5594df8d7d0f846c33 Mon Sep 17 00:00:00 2001 From: stefan Date: Fri, 9 Oct 2009 13:58:43 +0000 Subject: [PATCH] Provide ability to specify arbitrary HTML attributes. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4371 57a73879-2fb5-44c3-a270-3262357dd7e2 --- roundup/cgi/templating.py | 70 +++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/roundup/cgi/templating.py b/roundup/cgi/templating.py index 2087508..797cd13 100644 --- a/roundup/cgi/templating.py +++ b/roundup/cgi/templating.py @@ -420,17 +420,19 @@ def _set_input_default_args(dic): except KeyError: pass +def cgi_escape_attrs(**attrs): + return ' '.join(['%s="%s"'%(k,cgi.escape(str(v), True)) + for k,v in attrs.items()]) + def input_html4(**attrs): """Generate an 'input' (html4) element with given attributes""" _set_input_default_args(attrs) - return ''%' '.join(['%s="%s"'%(k,cgi.escape(str(v), True)) - for k,v in attrs.items()]) + return ''%cgi_escape_attrs(**attrs) def input_xhtml(**attrs): """Generate an 'input' (xhtml) element with given attributes""" _set_input_default_args(attrs) - return ''%' '.join(['%s="%s"'%(k,cgi.escape(str(v), True)) - for k,v in attrs.items()]) + return ''%cgi_escape_attrs(**attrs) class HTMLInputMixin: """ requires a _client property """ @@ -1464,8 +1466,7 @@ class StringHTMLProperty(HTMLProperty): value = '"'.join(value.split('"')) name = self._formname - passthrough_args = ' '.join(['%s="%s"' % (k, cgi.escape(str(v), True)) - for k,v in kwargs.items()]) + passthrough_args = cgi_escape_attrs(**kwargs) return ('') % locals() @@ -1503,7 +1504,7 @@ class PasswordHTMLProperty(HTMLProperty): return '' return self._('*encrypted*') - def field(self, size=30): + def field(self, size=30, **kwargs): """ Render a form edit field for the property. If not editable, just display the value via plain(). @@ -1511,7 +1512,8 @@ class PasswordHTMLProperty(HTMLProperty): if not self.is_edit_ok(): return self.plain(escape=1) - return self.input(type="password", name=self._formname, size=size) + return self.input(type="password", name=self._formname, size=size, + **kwargs) def confirm(self, size=30): """ Render a second form edit field for the property, used for @@ -1540,7 +1542,7 @@ class NumberHTMLProperty(HTMLProperty): return str(self._value) - def field(self, size=30): + def field(self, size=30, **kwargs): """ Render a form edit field for the property. If not editable, just display the value via plain(). @@ -1552,7 +1554,8 @@ class NumberHTMLProperty(HTMLProperty): if value is None: value = '' - return self.input(name=self._formname, value=value, size=size) + return self.input(name=self._formname, value=value, size=size, + **kwargs) def __int__(self): """ Return an int of me @@ -1576,7 +1579,7 @@ class BooleanHTMLProperty(HTMLProperty): return '' return self._value and self._("Yes") or self._("No") - def field(self): + def field(self, **kwargs): """ Render a form edit field for the property If not editable, just display the value via plain(). @@ -1592,15 +1595,17 @@ class BooleanHTMLProperty(HTMLProperty): checked = value and "checked" or "" if value: s = self.input(type="radio", name=self._formname, value="yes", - checked="checked") + checked="checked", **kwargs) s += self._('Yes') - s +=self.input(type="radio", name=self._formname, value="no") + s +=self.input(type="radio", name=self._formname, value="no", + **kwargs) s += self._('No') else: - s = self.input(type="radio", name=self._formname, value="yes") + s = self.input(type="radio", name=self._formname, value="yes", + **kwargs) s += self._('Yes') s +=self.input(type="radio", name=self._formname, value="no", - checked="checked") + checked="checked", **kwargs) s += self._('No') return s @@ -1658,7 +1663,8 @@ class DateHTMLProperty(HTMLProperty): return DateHTMLProperty(self._client, self._classname, self._nodeid, self._prop, self._formname, ret) - def field(self, size=30, default=None, format=_marker, popcal=True): + def field(self, size=30, default=None, format=_marker, popcal=True, + **kwargs): """Render a form edit field for the property If not editable, just display the value via plain(). @@ -1693,7 +1699,8 @@ class DateHTMLProperty(HTMLProperty): elif isinstance(value, str) or isinstance(value, unicode): # most likely erroneous input to be passed back to user if isinstance(value, unicode): value = value.encode('utf8') - return self.input(name=self._formname, value=value, size=size) + return self.input(name=self._formname, value=value, size=size, + **kwargs) else: raw_value = value @@ -1713,7 +1720,8 @@ class DateHTMLProperty(HTMLProperty): if format is not self._marker: value = value.pretty(format) - s = self.input(name=self._formname, value=value, size=size) + s = self.input(name=self._formname, value=value, size=size, + **kwargs) if popcal: s += self.popcal() return s @@ -1808,7 +1816,7 @@ class IntervalHTMLProperty(HTMLProperty): return self._value.pretty() - def field(self, size=30): + def field(self, size=30, **kwargs): """ Render a form edit field for the property If not editable, just display the value via plain(). @@ -1820,7 +1828,8 @@ class IntervalHTMLProperty(HTMLProperty): if value is None: value = '' - return self.input(name=self._formname, value=value, size=size) + return self.input(name=self._formname, value=value, size=size, + **kwargs) class LinkHTMLProperty(HTMLProperty): """ Link HTMLProperty @@ -1873,7 +1882,7 @@ class LinkHTMLProperty(HTMLProperty): value = cgi.escape(value) return value - def field(self, showid=0, size=None): + def field(self, showid=0, size=None, **kwargs): """ Render a form edit field for the property If not editable, just display the value via plain(). @@ -1891,10 +1900,11 @@ class LinkHTMLProperty(HTMLProperty): value = linkcl.get(self._value, k) else: value = self._value - return self.input(name=self._formname, value=value, size=size) + return self.input(name=self._formname, value=value, size=size, + **kwargs) def menu(self, size=None, height=None, showid=0, additional=[], value=None, - sort_on=None, **conditions): + sort_on=None, html_kwargs = {}, **conditions): """ Render a form select list for this property "size" is used to limit the length of the list labels @@ -1927,7 +1937,8 @@ class LinkHTMLProperty(HTMLProperty): value = None linkcl = self._db.getclass(self._prop.classname) - l = [''%cgi_escape_attrs(name = self._formname, + **html_kwargs)] k = linkcl.labelprop(1) s = '' if value is None: @@ -2093,7 +2104,7 @@ class MultilinkHTMLProperty(HTMLProperty): value = cgi.escape(value) return value - def field(self, size=30, showid=0): + def field(self, size=30, showid=0, **kwargs): """ Render a form edit field for the property If not editable, just display the value via plain(). @@ -2110,10 +2121,11 @@ class MultilinkHTMLProperty(HTMLProperty): k = linkcl.labelprop(1) value = lookupKeys(linkcl, k, value) value = ','.join(value) - return self.input(name=self._formname, size=size, value=value) + return self.input(name=self._formname, size=size, value=value, + **kwargs) def menu(self, size=None, height=None, showid=0, additional=[], - value=None, sort_on=None, **conditions): + value=None, sort_on=None, html_kwargs = {}, **conditions): """ Render a form '%(self._formname, height)] + l = ['