Created
June 19, 2011 18:12
-
-
Save w0rm/1034547 to your computer and use it in GitHub Desktop.
Saving form state between requests in webpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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