Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Commit

Permalink
Resolves #88 -- There is now a resolve_all utility method for Form
Browse files Browse the repository at this point in the history
  • Loading branch information
icook committed Oct 20, 2013
1 parent fdb3da4 commit 1246be5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/yota/__init__.py
Expand Up @@ -377,6 +377,12 @@ def insert_after(self, prev_attr_name, new_node_list):
for new_node in new_node_list:
self._node_list.append(new_node)

def resolve_all(self, data):
""" This is a utility method that runs resolve_data on all nodes with
the provided data dictionary. """
for node in self._node_list:
node.resolve_data(data)

def get_by_attr(self, name):
""" Safe accessor for looking up a node by :attr:`Node._attr_name` """
try:
Expand Down
25 changes: 10 additions & 15 deletions src/yota/nodes.py
Expand Up @@ -132,21 +132,16 @@ def set_identifiers(self, parent_name):
self.title = self._attr_name.capitalize().replace('_', ' ')

def resolve_data(self, data):
""" This method links data from form submission back to Nodes. HTML
form data is represented by a dictionary that is keyed by the 'name'
attribute of the form element. Since most Nodes only render a single
form element, and the default set_identifiers generates a single 'name'
attribute for the Node then this function attempts to find data by
linking the two together. However, if you were to change that semantic
this would need to change. Look at the CheckGroupNode for a reference
impplementation of this behaviour, or the Docs under "Custom Nodes".
This method should operate by setting its own data attribute, as this
is how Validators conventionally look for data.
... note:: This method will throw an exception at validation time if
the data dictionary contains no key name, so it important to
override this function to a NoOp if your Node generates no data.
NonDataNode was created for this exact purpose.
""" This method links data from input data into the Nodes. By default
HTML form data is represented by a dictionary that is keyed by the
'name' attribute of the form element. Since most Nodes only render a
single form element, and the default set_identifiers generates a single
'name' attribute for the Node then this function attempts to find data
by linking the two together. However, if you were to change that
semantic this would need to change. Look at the CheckGroupNode for a
reference impplementation of this behaviour, or the Docs under "Custom
Nodes". This method should operate by setting its own data attribute,
as this is how Validators conventionally look for data.
:param data: The dictionary of data that is passed to your validation
method call.
Expand Down
9 changes: 9 additions & 0 deletions src/yota/tests/test_form.py
Expand Up @@ -267,6 +267,15 @@ class TForm(yota.Form):
assert('t' in test.data_by_attr())
assert('two' in test.data_by_name())

def test_form_resolve(self):
""" resolve all should function as expected """
class TForm(yota.Form):
t = EntryNode()

test = TForm()
test.resolve_all({'t': 'Something'})
assert test.t.data == 'Something'

def test_dynamic_insert(self):
""" insert_after test, and subsequently insert itself """
class TForm(yota.Form):
Expand Down

0 comments on commit 1246be5

Please sign in to comment.