Navigation Menu

Skip to content

Commit

Permalink
compound_key was ignoring key for RepeatingWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Nov 15, 2013
1 parent 85af595 commit ed09461
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
9 changes: 9 additions & 0 deletions tests/test_widgets.py
Expand Up @@ -589,6 +589,15 @@ class T(wd.RepeatingWidget):
children = [wd.Widget(id="foo")] children = [wd.Widget(id="foo")]
self.assert_(len(T().req().child.children) == 1) self.assert_(len(T().req().child.children) == 1)


def testRepeatingWithKeyDifferentFromId(self):
class T(wd.RepeatingWidget):
id = 'foo'
key = 'bar'
child = wd.Widget

w = T().req(value=['1', '2', '3'])
self.assertEquals(w.children[0].compound_key, 'bar:0')

def testNonWidgetChild(self): def testNonWidgetChild(self):
""" """
should throw an error if child is not a widget should throw an error if child is not a widget
Expand Down
27 changes: 14 additions & 13 deletions tw2/core/widgets.py
Expand Up @@ -257,7 +257,7 @@ def post_define(cls):
setattr(cls, p.name, p.default) setattr(cls, p.name, p.default)


@classmethod @classmethod
def _gen_compound_id(cls, for_url): def _gen_compound_name(cls, attr, for_url):
ancestors = [] ancestors = []
cur = cls cur = cls
while cur: while cur:
Expand All @@ -266,34 +266,35 @@ def _gen_compound_id(cls, for_url):
ancestors.append(cur) ancestors.append(cur)
cur = cur.parent cur = cur.parent
elems = reversed(list(filter(None, [ elems = reversed(list(filter(None, [
a._compound_id_elem(for_url) for a in ancestors a._compound_name_elem(attr, for_url) for a in ancestors
]))) ])))
if getattr(cls, 'id', None) or \ if getattr(cls, attr, None) or \
(cls.parent and issubclass(cls.parent, RepeatingWidget)): (cls.parent and issubclass(cls.parent, RepeatingWidget)):
return ':'.join(elems) return ':'.join(elems)
else: else:
return None return None


@classmethod @classmethod
def _compound_id_elem(cls, for_url): def _compound_name_elem(cls, attr, for_url):
if cls.parent and issubclass(cls.parent, RepeatingWidget): if cls.parent and issubclass(cls.parent, RepeatingWidget):
if for_url: if for_url:
return None return None
else: else:
return str(getattr(cls, 'repetition', None)) return str(getattr(cls, 'repetition', None))
else: else:
return getattr(cls, 'id', None) return getattr(cls, attr, None)


@classmethod @classmethod
def _gen_compound_key(cls): def _compound_id_elem(cls, for_url):
if not cls.key: return cls._compound_name_elem('id', for_url)
return cls.compound_id


parent_key = getattr(cls.parent, 'compound_key', None) @classmethod
if parent_key: def _gen_compound_id(cls, for_url):
return ':'.join([parent_key, cls.key]) return cls._gen_compound_name('id', for_url)
else:
return cls.key @classmethod
def _gen_compound_key(cls):
return cls._gen_compound_name('key', False)


@classmethod @classmethod
def get_link(cls): def get_link(cls):
Expand Down

0 comments on commit ed09461

Please sign in to comment.