Skip to content

Instantly share code, notes, and snippets.

@w0rm
Created June 19, 2011 18:12
Show Gist options
  • Save w0rm/1034547 to your computer and use it in GitHub Desktop.
Save w0rm/1034547 to your computer and use it in GitHub Desktop.
Saving form state between requests in webpy
class Form(web.form.Form, object):
def __init__(self, name, *inputs, **kw):
super(Form, self).__init__(*inputs, **kw)
self.name = name
def save_state(self):
session.forms[self.name] = dict(
notes = dict((i.name, i.note) for i in self.inputs if i.note ),
values = dict((i.name, i.get_value()) for i in self.inputs ),
note = self.note
)
def load_state(self):
n = session.forms.pop(self.name, None)
if n:
self.note = n["note"]
for i in self.inputs:
i.note = n["notes"].get(i.name, "")
i.set_value(n["values"].get(i.name, ""))
def validates(self, source=None, _validate=True, **kw):
out = super(Form, self).validates(source, _validate, **kw)
if not out:
self.save_state()
return out
def render_css(self):
self.load_state()
return super(Form, self).render_css()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment