Skip to content

Instantly share code, notes, and snippets.

@dwt
Created August 13, 2014 14:43
Show Gist options
  • Select an option

  • Save dwt/3ea8c4c4af2ebc702eb8 to your computer and use it in GitHub Desktop.

Select an option

Save dwt/3ea8c4c4af2ebc702eb8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import colander
import deform
from deform.widget import TextAreaWidget, SequenceWidget
class AnswerEditSchema(colander.Schema):
answer_title = colander.SchemaNode(
colander.String(),
validator=colander.Length(1, 384),
title=u"Title",
missing=u'',
widget=deform.widget.TextAreaWidget(),
)
class QuestionEditSchema(colander.Schema):
question_title = colander.SchemaNode(
colander.String(),
validator=colander.Length(2),
title=u"Title",
widget=deform.widget.TextAreaWidget(),
)
answers = colander.SchemaNode(
colander.Sequence(),
AnswerEditSchema(
title=u"Answer",
name=u"answer"
),
widget=SequenceWidget(min_len=2),
title=u"Answers",
)
schema = QuestionEditSchema().bind()
form = deform.Form(schema=schema)
post_values = [('_charset_', u'UTF-8'), ('question_title', u'english title'), ('__start__', u'answers:sequence'), ('__start__', u':mapping'), ('title', u''), ('__end__', u':mapping'), ('__start__', u':mapping'), ('title', u'english wrong answer'), ('__end__', u':mapping'), ('__end__', u'answers:sequence'), ('submit', u'submit')]
app_struct = form.validate(post_values)
# app_struct = {'answers': [{'answer_title': 'fnord1'}, {'answer_title': u'english wrong answer'}], 'question_title': 'fnord2'}
print 'app_struct:\t', app_struct
app_struct['answers'][0]['answer_title'] = 'fnord1'
app_struct['question_title'] = 'fnord2'
print 'new app struct:\t', app_struct
rendered = form.render(app_struct)
assert 'fnord1' in rendered, "form.render() does not respect app_struct['answers'][0]['answer_title']"
assert 'fnord2' in rendered, "form.render() does not respect app_struct['question_title']"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment