Skip to content

Commit

Permalink
Move findForm to wokkel.data_form
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphm committed Jan 5, 2010
1 parent a62c936 commit 5aba9f9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 27 deletions.
22 changes: 22 additions & 0 deletions wokkel/data_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,25 @@ def typeCheck(self, fieldDefs=None, filterUnknown=False):

for field in filtered:
self.removeField(field)



def findForm(element, formNamespace):
"""
Find a Data Form.
Look for an element that represents a Data Form with the specified
form namespace as a child element of the given element.
"""
if not element:
return None

for child in element.elements():
if (child.uri, child.name) == ((NS_X_DATA, 'x')):
form = Form.fromElement(child)

if (form.formNamespace == formNamespace or
not form.formNamespace and form.formType=='cancel'):
return form

return None
30 changes: 3 additions & 27 deletions wokkel/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,30 +252,6 @@ def __init__(self, verb=None):
self.verb = verb


@staticmethod
def _findForm(element, formNamespace):
"""
Find a Data Form.
Look for an element that represents a Data Form with the specified
form namespace as a child element of the given element.
"""
if not element:
return None

form = None
for child in element.elements():
try:
form = data_form.Form.fromElement(child)
except data_form.Error:
continue

if form.formNamespace != NS_PUBSUB_NODE_CONFIG:
continue

return form


def _parse_node(self, verbElement):
"""
Parse the required node identifier out of the verbElement.
Expand Down Expand Up @@ -366,7 +342,7 @@ def _parse_default(self, verbElement):
"""
Parse node type out of a request for the default node configuration.
"""
form = PubSubRequest._findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
if form and form.formType == 'submit':
values = form.getValues()
self.nodeType = values.get('pubsub#node_type', 'leaf')
Expand All @@ -378,7 +354,7 @@ def _parse_configure(self, verbElement):
"""
Parse options out of a request for setting the node configuration.
"""
form = PubSubRequest._findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG)
if form:
if form.formType == 'submit':
self.options = form.getValues()
Expand Down Expand Up @@ -437,7 +413,7 @@ def _render_maxItems(self, verbElement):


def _parse_options(self, verbElement):
form = PubSubRequest._findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
form = data_form.findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS)
if form:
if form.formType == 'submit':
self.options = form.getValues()
Expand Down
71 changes: 71 additions & 0 deletions wokkel/test/test_data_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,77 @@ def test_typeCheckUnknownRemoved(self):
self.assertEqual([], checked)



class FindFormTest(unittest.TestCase):
"""
Tests for L{data_form.findForm}.
"""

def test_findForm(self):
element = domish.Element((None, 'test'))
theForm = data_form.Form('submit', formNamespace='myns')
element.addChild(theForm.toElement())
form = data_form.findForm(element, 'myns')
self.assertEqual('myns', form.formNamespace)


def test_noFormType(self):
element = domish.Element((None, 'test'))
otherForm = data_form.Form('submit')
element.addChild(otherForm.toElement())
form = data_form.findForm(element, 'myns')
self.assertIdentical(None, form)


def test_noFormTypeCancel(self):
"""
Cancelled forms don't have a FORM_TYPE field, the first is returned.
"""
element = domish.Element((None, 'test'))
cancelledForm = data_form.Form('cancel')
element.addChild(cancelledForm.toElement())
form = data_form.findForm(element, 'myns')
self.assertEqual('cancel', form.formType)


def test_otherFormType(self):
"""
Forms with other FORM_TYPEs are ignored.
"""
element = domish.Element((None, 'test'))
otherForm = data_form.Form('submit', formNamespace='otherns')
element.addChild(otherForm.toElement())
form = data_form.findForm(element, 'myns')
self.assertIdentical(None, form)


def test_otherFormTypeCancel(self):
"""
Cancelled forms with another FORM_TYPE are ignored.
"""
element = domish.Element((None, 'test'))
cancelledForm = data_form.Form('cancel', formNamespace='otherns')
element.addChild(cancelledForm.toElement())
form = data_form.findForm(element, 'myns')
self.assertIdentical(None, form)


def test_noElement(self):
"""
When None is passed as element, None is returned.
"""
element = None
form = data_form.findForm(element, 'myns')
self.assertIdentical(None, form)


def test_noForm(self):
"""
When no child element is a form, None is returned.
"""
element = domish.Element((None, 'test'))
form = data_form.findForm(element, 'myns')
self.assertIdentical(None, form)
def test_typeCheckNoFieldDefs(self):
"""
If there are no field defs, an empty dictionary is assumed.
Expand Down

0 comments on commit 5aba9f9

Please sign in to comment.