Skip to content

Commit

Permalink
Backwards compat with tw0.9 on display() calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphbean committed Mar 4, 2012
1 parent c0a681a commit 27eb8e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
13 changes: 11 additions & 2 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ def setUp(self):
#--
# Widget.process
#--
def test_backwards_compat(self):
""" Ticket #4 """
def test_backwards_compat_new(self):
""" Ticket #4 `.__init__(id, ...)` """
test = twc.Widget('test')
eq_(test.id, 'test')

def test_backwards_compat_display(self):
""" Ticket #4 `.display(value, ...)` """
test = twc.Widget(
id='test',
template="genshi:tw2.core.test_templates.field_genshi"
)
output = test.display("foo")
eq_(output, u'<p>foo </p>')

def xxtest_required(self):
test = twc.Widget(id='test')
try:
Expand Down
26 changes: 16 additions & 10 deletions tw2/core/widgets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import copy, weakref, re, itertools, inspect, webob
import template, core, util, validation as vd, params as pm
import warnings
try:
import formencode
except ImportError:
formencode = None

TW1_BACKPAT_WARNING_MESSAGE = \
"tw1-style calling is deprecated. Use tw2 keyword syntax."
reserved_names = ('parent', 'demo_for', 'child', 'submit', 'datasrc', 'newlink', 'edit')
_widget_seq = itertools.count(0)

Expand Down Expand Up @@ -77,18 +80,15 @@ def req(cls, **kw):
ins.__init__(**kw)
return ins

def __new__(cls, *args, **kw):
def __new__(cls, id=None, **kw):
"""
New is overloaded to return a subclass of the widget, rather than an instance.
"""
if len(args) > 0:
if len(args) > 1:
raise pm.ParameterError(
"Only one positional argument accepted.")
if 'id' in kw:
raise pm.ParameterError(
"You cannot specify two widget ids.")
kw['id'] = args[0]

# Support backwards compatibility with tw1-style calling
if id and 'id' not in kw:
kw['id'] = id
warnings.warn(TW1_BACKPAT_WARNING_MESSAGE)

newname = calc_name(cls, kw)
return type(cls.__name__+'_s', (cls,), kw)
Expand Down Expand Up @@ -249,7 +249,7 @@ def add_call(self, extra_arg, call, location="bodybottom"):
self._js_calls.append([call, location])

@util.class_or_instance
def display(self, cls, displays_on=None, **kw):
def display(self, cls, value=None, displays_on=None, **kw):
"""Display the widget - render the template. In the template, the
widget instance is available as the variable ``$w``.
Expand All @@ -261,6 +261,12 @@ def display(self, cls, displays_on=None, **kw):
the parent's template engine, or the default, if there is no
parent. Set this to ``string`` to get raw string output.
"""

# Support backwards compatibility with tw1-style calling
if value and 'value' not in kw:
kw['value'] = value
warnings.warn(TW1_BACKPAT_WARNING_MESSAGE)

if not self:
# Create a self (since we were called as a classmethod)
vw = vw_class = core.request_local().get('validated_widget')
Expand Down

0 comments on commit 27eb8e1

Please sign in to comment.