[Checkins] SVN: grok/trunk/src/grok/ Improve applyChanges() in
forms:
Philipp von Weitershausen
philikon at philikon.de
Thu Mar 15 14:26:49 EDT 2007
Log message for revision 73199:
Improve applyChanges() in forms:
- Rename applyChanges to apply_changes to match our PEP8 fetish
- Move it to general Form base class so that it's available on all forms.
It now takes the object it should operate on as the first argument. That
way you can either do::
newobj = MyModel(**data)
in your AddForm action (if MyModel's constructor takes arguments) or you can
do::
newobj = MyModel()
self.apply_changes(newobj, **data)
- Made it not modify the status (the status message is something that's specific
to forms, it was also not translatable). Instead, return boolean values like the
original applyChanges().
Changed:
U grok/trunk/src/grok/components.py
U grok/trunk/src/grok/ftests/form/actions.py
U grok/trunk/src/grok/ftests/form/addform.py
-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py 2007-03-15 18:18:29 UTC (rev 73198)
+++ grok/trunk/src/grok/components.py 2007-03-15 18:26:49 UTC (rev 73199)
@@ -349,6 +349,7 @@
class Form(View):
+
def __init__(self, context, request):
super(Form, self).__init__(context, request)
self.form = self.__real_form__(context, request)
@@ -375,25 +376,21 @@
return form.form_result
+ def apply_changes(self, obj, **data):
+ if form.applyChanges(obj, self.form.form_fields, data,
+ getattr(self.form, 'adapters', {})):
+ event.notify(ObjectModifiedEvent(obj))
+ return True
+ return False
class EditForm(Form):
label = ''
status = ''
- def applyChanges(self, **data):
- if form.applyChanges(self.context, self.form.form_fields, data,
- self.form.adapters):
- event.notify(ObjectModifiedEvent(self.context))
- self.status = "Updated"
- else:
- self.status = "No changes"
-
-
class AddForm(Form):
label = ''
status = ''
-
class DisplayForm(Form):
label = ''
status = ''
Modified: grok/trunk/src/grok/ftests/form/actions.py
===================================================================
--- grok/trunk/src/grok/ftests/form/actions.py 2007-03-15 18:18:29 UTC (rev 73198)
+++ grok/trunk/src/grok/ftests/form/actions.py 2007-03-15 18:26:49 UTC (rev 73199)
@@ -2,7 +2,7 @@
Using the @grok.action decorator, different actions can be defined on a
grok.EditForm. When @grok.action is used, the default behaviour (the 'Apply'
action) is not available anymore, but it can triggered manually by calling
-self.applyChanges(action, data).
+self.apply_changes(object, data).
>>> import grok
>>> from grok.ftests.form.actions import Mammoth
@@ -18,10 +18,19 @@
>>> browser.getControl("Apply").click()
>>> print browser.contents
<html>...
+ ...Modified!...
...Manfred the Mammoth...
...Really big...
...
+Save again without any changes:
+
+ >>> browser.getControl("Apply").click()
+ >>> print browser.contents
+ <html>...
+ ...No changes!...
+ ...
+
>>> browser.open("http://localhost/manfred/@@edit")
>>> browser.getControl(name="form.name").value = "Manfred the Second"
>>> browser.getControl("Hairy").click()
@@ -42,9 +51,12 @@
class Edit(grok.EditForm):
@grok.action("Apply")
def handle_apply(self, **data):
- self.applyChanges(**data)
+ if self.apply_changes(self.context, **data):
+ self.status = 'Modified!'
+ else:
+ self.status = 'No changes!'
@grok.action("Hairy")
def handle_hairy(self, **data):
- self.applyChanges(**data)
+ self.apply_changes(self.context, **data)
self.context.size += " and hairy"
Modified: grok/trunk/src/grok/ftests/form/addform.py
===================================================================
--- grok/trunk/src/grok/ftests/form/addform.py 2007-03-15 18:18:29 UTC (rev 73198)
+++ grok/trunk/src/grok/ftests/form/addform.py 2007-03-15 18:26:49 UTC (rev 73199)
@@ -9,6 +9,7 @@
>>> from zope.testbrowser.testing import Browser
>>> browser = Browser()
>>> browser.handleErrors = False
+
>>> browser.open("http://localhost/zoo/@@addmammoth")
>>> browser.getControl(name="form.name").value = "Manfred the Mammoth"
>>> browser.getControl(name="form.size").value = "Really big"
@@ -16,6 +17,13 @@
>>> print browser.contents
Hi, my name is Manfred the Mammoth, and I\'m "Really big"
+ >>> browser.open("http://localhost/zoo/@@addmammothapplychanges")
+ >>> browser.getControl(name="form.name").value = "Ellie the Mammoth"
+ >>> browser.getControl(name="form.size").value = "Really small"
+ >>> browser.getControl("Add entry").click()
+ >>> print browser.contents
+ Hi, my name is Ellie the Mammoth, and I\'m "Really small"
+
"""
import grok
from zope import schema
@@ -28,7 +36,7 @@
name = schema.TextLine(title=u"Name")
size = schema.TextLine(title=u"Size")
- def __init__(self, name, size):
+ def __init__(self, name='', size=''):
self.name = name
self.size = size
@@ -45,6 +53,15 @@
@grok.action('Add entry')
def add(self, **data):
- self.context['manfred'] = Mammoth(data['name'], data['size'])
- self.redirect(self.url(self.context['manfred']))
+ # pass data into Mammoth constructor
+ self.context['manfred'] = manfred = Mammoth(**data)
+ self.redirect(self.url(manfred))
+class AddMammothApplyChanges(AddMammoth):
+
+ @grok.action('Add entry')
+ def add(self, **data):
+ # instantiate Mammoth and then use self.apply_changes()
+ self.context['ellie'] = ellie = Mammoth()
+ self.apply_changes(ellie, **data)
+ self.redirect(self.url(ellie))
More information about the Checkins
mailing list