Skip to content

Commit

Permalink
List-like View Parametrization access
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Falešník committed Feb 7, 2017
1 parent 4f1abae commit 42efdf1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/widgetastic/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,26 @@ def __call__(self, *args, **kwargs):
self.parent_object.child_widget_accessed(result)
return result

def __getitem__(self, int_or_slice):
"""Emulates list-like behaviour.
Maps into the dict-like structure by utilizing all() to get the list of all items and then
it picks the one selected by the list-like accessor. Supports both integers and slices.
"""
all_items = self.view_class.all(self.parent_object.browser)
items = all_items[int_or_slice]
single = isinstance(int_or_slice, int)
if single:
items = [items]
views = []
for args in items:
views.append(self(*args))

if single:
return views[0]
else:
return views

def __getattr__(self, attr):
raise AttributeError(
'This is not an instance of {}. You need to call this object and pass the required '
Expand Down
10 changes: 10 additions & 0 deletions testing/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ def all(cls, browser):
('def-345', ): {'checkbox': False},
}})

# list-like access
assert view.table_row[0].col1.text == 'qwer'
cx, cy = view.table_row[1:3]
assert cx.col1.text == 'bar_x'
assert cy.col1.text == 'bar_y'

cx, cy = view.table_row[0:3:2]
assert cx.col1.text == 'qwer'
assert cy.col1.text == 'bar_y'


def test_parametrized_view_read_without_all(browser):
class MyView(View):
Expand Down

0 comments on commit 42efdf1

Please sign in to comment.