Mercurial > p > roundup > code
comparison roundup/cgi/templating.py @ 4878:f6e76a03b502
HTML* classes for cgi are now all new-style
Add regression test for old behaviour: Lookup of a value of a
HTMLProperty was possibly via getitem -- for old-style classes this
worked because __getattr__ returned the __getitem__ of a newly created
HTMLItem object, this does no longer work for new-style classes as these
look up special method only on the class not the instance.
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Sat, 29 Mar 2014 10:52:20 +0100 |
| parents | cdec6ed210d0 |
| children | 302c967d710c |
comparison
equal
deleted
inserted
replaced
| 4877:2ba982dcdf2c | 4878:f6e76a03b502 |
|---|---|
| 396 def input_xhtml(**attrs): | 396 def input_xhtml(**attrs): |
| 397 """Generate an 'input' (xhtml) element with given attributes""" | 397 """Generate an 'input' (xhtml) element with given attributes""" |
| 398 _set_input_default_args(attrs) | 398 _set_input_default_args(attrs) |
| 399 return '<input %s/>'%cgi_escape_attrs(**attrs) | 399 return '<input %s/>'%cgi_escape_attrs(**attrs) |
| 400 | 400 |
| 401 class HTMLInputMixin: | 401 class HTMLInputMixin(object): |
| 402 """ requires a _client property """ | 402 """ requires a _client property """ |
| 403 def __init__(self): | 403 def __init__(self): |
| 404 html_version = 'html4' | 404 html_version = 'html4' |
| 405 if hasattr(self._client.instance.config, 'HTML_VERSION'): | 405 if hasattr(self._client.instance.config, 'HTML_VERSION'): |
| 406 html_version = self._client.instance.config.HTML_VERSION | 406 html_version = self._client.instance.config.HTML_VERSION |
| 419 return self._client.translator.translate(domain="roundup", | 419 return self._client.translator.translate(domain="roundup", |
| 420 msgid=msgid, context=self._context) | 420 msgid=msgid, context=self._context) |
| 421 | 421 |
| 422 _ = gettext | 422 _ = gettext |
| 423 | 423 |
| 424 class HTMLPermissions: | 424 class HTMLPermissions(object): |
| 425 | 425 |
| 426 def view_check(self): | 426 def view_check(self): |
| 427 """ Raise the Unauthorised exception if the user's not permitted to | 427 """ Raise the Unauthorised exception if the user's not permitted to |
| 428 view this class. | 428 view this class. |
| 429 """ | 429 """ |
| 1901 value = '' | 1901 value = '' |
| 1902 | 1902 |
| 1903 return self.input(name=self._formname, value=value, size=size, | 1903 return self.input(name=self._formname, value=value, size=size, |
| 1904 **kwargs) | 1904 **kwargs) |
| 1905 | 1905 |
| 1906 class LinkHTMLProperty(HTMLProperty, object): | 1906 class LinkHTMLProperty(HTMLProperty): |
| 1907 """ Link HTMLProperty | 1907 """ Link HTMLProperty |
| 1908 Include the above as well as being able to access the class | 1908 Include the above as well as being able to access the class |
| 1909 information. Stringifying the object itself results in the value | 1909 information. Stringifying the object itself results in the value |
| 1910 from the item being displayed. Accessing attributes of this object | 1910 from the item being displayed. Accessing attributes of this object |
| 1911 result in the appropriate entry from the class being queried for the | 1911 result in the appropriate entry from the class being queried for the |
| 1912 property accessed (so item/assignedto/name would look up the user | 1912 property accessed (so item/assignedto/name would look up the user |
| 1913 entry identified by the assignedto property on item, and then the | 1913 entry identified by the assignedto property on item, and then the |
| 1914 name property of that user) | 1914 name property of that user) |
| 1915 | |
| 1916 (Has been turned into a new-style class to enable comparisons | |
| 1917 of values with None, see issue2550830.) | |
| 1918 """ | 1915 """ |
| 1919 def __init__(self, *args, **kw): | 1916 def __init__(self, *args, **kw): |
| 1920 HTMLProperty.__init__(self, *args, **kw) | 1917 HTMLProperty.__init__(self, *args, **kw) |
| 1921 # if we're representing a form value, then the -1 from the form really | 1918 # if we're representing a form value, then the -1 from the form really |
| 1922 # should be a None | 1919 # should be a None |
| 1933 return nothing | 1930 return nothing |
| 1934 msg = self._('Attempt to look up %(attr)s on a missing value') | 1931 msg = self._('Attempt to look up %(attr)s on a missing value') |
| 1935 return MissingValue(msg%locals()) | 1932 return MissingValue(msg%locals()) |
| 1936 i = HTMLItem(self._client, self._prop.classname, self._value) | 1933 i = HTMLItem(self._client, self._prop.classname, self._value) |
| 1937 return getattr(i, attr) | 1934 return getattr(i, attr) |
| 1935 | |
| 1936 def __getitem__(self, item): | |
| 1937 """Explicitly define __getitem__ -- this used to work earlier | |
| 1938 due to __getattr__ returning the __getitem__ of HTMLItem -- this | |
| 1939 lookup doesn't work for new-style classes. | |
| 1940 """ | |
| 1941 i = HTMLItem(self._client, self._prop.classname, self._value) | |
| 1942 return i[item] | |
| 1938 | 1943 |
| 1939 def plain(self, escape=0): | 1944 def plain(self, escape=0): |
| 1940 """ Render a "plain" representation of the property | 1945 """ Render a "plain" representation of the property |
| 1941 """ | 1946 """ |
| 1942 if not self.is_view_ok(): | 1947 if not self.is_view_ok(): |
| 3004 res.append(' <td></td>') | 3009 res.append(' <td></td>') |
| 3005 res.append(' </tr>') | 3010 res.append(' </tr>') |
| 3006 res.append('</table></td></tr></table>') | 3011 res.append('</table></td></tr></table>') |
| 3007 return "\n".join(res) | 3012 return "\n".join(res) |
| 3008 | 3013 |
| 3009 class MissingValue: | 3014 class MissingValue(object): |
| 3010 def __init__(self, description, **kwargs): | 3015 def __init__(self, description, **kwargs): |
| 3011 self.__description = description | 3016 self.__description = description |
| 3012 for key, value in kwargs.items(): | 3017 for key, value in kwargs.items(): |
| 3013 self.__dict__[key] = value | 3018 self.__dict__[key] = value |
| 3014 | 3019 |
