From: David G. <svn...@pl...> - 2011-12-31 01:47:23
|
Repository: plone.schemaeditor Branch: refs/heads/master Date: 2011-12-30T17:47:03-08:00 Author: David Glick (davisagli) <dg...@gm...> Commit: https://github.com/plone/plone.schemaeditor/commit/c78863ccf02b6f1746304d9f262cc972737a44a7 change the approach for editing default values Files changed: M CHANGES.txt M plone/schemaeditor/browser/field/edit.py M plone/schemaeditor/browser/schema/listing.py M plone/schemaeditor/tests/choice.txt M plone/schemaeditor/tests/editing.txt M plone/schemaeditor/tests/minmax.txt M setup.py diff --git a/CHANGES.txt b/CHANGES.txt index f1f7668..1bb85e0 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,9 +1,14 @@ Changelog ========= -1.1.3 - unreleased +1.2.0 - unreleased ------------------ +* Edit field defaults from the schema listing instead of in the field + overlays. This simplifies making sure that the default can't be set + to invalid values. + [davisagli] + * Limit the height of text areas in the schema listing to avoid extra scrolling. [davisagli] diff --git a/plone/schemaeditor/browser/field/edit.py b/plone/schemaeditor/browser/field/edit.py index 76dbea7..146f1a2 100644 --- a/plone/schemaeditor/browser/field/edit.py +++ b/plone/schemaeditor/browser/field/edit.py @@ -1,6 +1,6 @@ from Acquisition import aq_parent, aq_inner -from zope.interface import implements, Interface, Invalid +from zope.interface import implements, Interface from zope.cachedescriptors.property import Lazy as lazy_property from zope.component import adapts from zope.event import notify @@ -9,7 +9,6 @@ from zope.i18nmessageid import MessageFactory from z3c.form import form, field, button -from z3c.form.interfaces import WidgetActionExecutionError from plone.z3cform import layout from plone.schemaeditor.interfaces import IFieldEditForm @@ -63,7 +62,7 @@ def fields(self): fields = field.Fields(IFieldTitle) # omit the order attribute since it's managed elsewhere - fields += field.Fields(self.schema).omit('order', 'title', 'missing_value') + fields += field.Fields(self.schema).omit('order', 'title', 'default', 'missing_value') if self.schema.isOrExtends(IBool): fields = fields.omit('required') return fields @@ -71,19 +70,6 @@ def fields(self): @button.buttonAndHandler(PMF(u'Save'), name='save') def handleSave(self, action): data, errors = self.extractData() - - # For choice fields, make sure default is in the valid values - if 'values' in data: - values = data['values'] or [] - if 'default' in data and data['default']: - default = data['default'] - if type(default) is not list: - default = [default] - for value in default: - if value not in values: - raise WidgetActionExecutionError('default', - Invalid(_(u'Please enter a valid vocabulary value.'))) - if errors: self.status = self.formErrorsMessage return @@ -94,18 +80,8 @@ def handleSave(self, action): if 'max' in data: self.field.max = None - default = data.pop('default', _marker) changes = self.applyChanges(data) - # make sure we can report invalid defaults - if default is not _marker: - try: - changes2 = self.applyChanges({'default': default}) - except schema.ValidationError, e: - raise WidgetActionExecutionError('default', e) - else: - changes = changes or changes2 - if changes: self.status = self.successMessage else: @@ -119,7 +95,12 @@ def handleCancel(self, action): self.redirectToParent() def redirectToParent(self): - self.request.response.redirect(aq_parent(aq_inner(self.context)).absolute_url()) + parent = aq_parent(aq_inner(self.context)) + url = parent.absolute_url() + if hasattr(parent, 'schemaEditorView'): + url += '/@@' + parent.schemaEditorView + self.request.response.redirect(url) + # form wrapper to use Plone form template class EditView(layout.FormWrapper): diff --git a/plone/schemaeditor/browser/schema/listing.py b/plone/schemaeditor/browser/schema/listing.py index b58a4b0..a025a1c 100644 --- a/plone/schemaeditor/browser/schema/listing.py +++ b/plone/schemaeditor/browser/schema/listing.py @@ -1,5 +1,6 @@ from zope.component import queryUtility from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile +from zope.event import notify from z3c.form import form, field, button from plone.z3cform.layout import FormWrapper @@ -7,6 +8,7 @@ from plone.schemaeditor import SchemaEditorMessageFactory as _ from plone.schemaeditor.interfaces import IFieldFactory +from plone.schemaeditor.utils import SchemaModifiedEvent class SchemaListing(form.Form): @@ -21,8 +23,6 @@ def fields(self): def updateWidgets(self): super(SchemaListing, self).updateWidgets() for widget in self.widgets.values(): - widget.disabled = 'disabled' - # limit size of the preview for text areas if hasattr(widget, 'rows'): if widget.rows is None or widget.rows > 5: @@ -48,6 +48,20 @@ def edit_url(self, field): def delete_url(self, field): return '%s/%s/@@delete' % (self.context.absolute_url(), field.__name__) + @button.buttonAndHandler(_(u'Save Defaults')) + def handleSaveDefaults(self, action): + data, errors = self.extractData() + if errors: + self.status = self.formErrorsMessage + return + + for fname, value in data.items(): + self.context.schema[fname].default = value + notify(SchemaModifiedEvent(self.context.schema)) + + # update widgets to take the new defaults into account + self.updateWidgets() + class ReadOnlySchemaListing(SchemaListing): buttons = button.Buttons() diff --git a/plone/schemaeditor/tests/choice.txt b/plone/schemaeditor/tests/choice.txt index 6ca297e..1239de0 100644 --- a/plone/schemaeditor/tests/choice.txt +++ b/plone/schemaeditor/tests/choice.txt @@ -61,27 +61,10 @@ If duplicate values are entered an error is raised. <div class="error">The 'United States' vocabulary value conflicts with 'United States'.</div> ... -For now, the 'default' field type is a TextLine field. - - >>> browser.getControl('Default') - <Control name='form.widgets.default' type='text'> - -If set, the default field must be a valid option from the vocabulary. - - >>> browser.getControl('Vocabulary').value = '\n'.join( - ... ['Alaska', 'Russia', 'United States', "C\xc3\xb4te d'Ivoire", 'Other']) - >>> browser.getControl('Default').value = 'Germany' - >>> browser.getControl('Save').click() - >>> print browser.contents - <... - <div class="error">Please enter a valid vocabulary value.</div> - ... - Enter valid values and save the field settings. >>> browser.getControl('Vocabulary').value = '\n'.join( ... ['Alaska', 'Russia', 'United States', "C\xc3\xb4te d'Ivoire", 'Other']) - >>> browser.getControl('Default').value = '' >>> browser.getControl('Save').click() [event: ObjectModifiedEvent on Choice] [event: SchemaModifiedEvent on DummySchemaContext] @@ -153,19 +136,12 @@ If duplicate values are entered an error is raised. <div class="error">The 'Lisp' vocabulary value conflicts with 'Lisp'.</div> ... -For now, the 'default' field type is a Text field. - - >>> browser.getControl('Default') - <Control name='form.widgets.default' type='textarea'> - Enter unique values and save the field settings. >>> browser.getControl('Vocabulary').value = '\n'.join( ... ['Plone', 'Python', 'Lisp']) - >>> browser.getControl('Default').value = 'Plone' >>> browser.getControl('Save').click() [event: ObjectModifiedEvent on List] - [event: ObjectModifiedEvent on List] [event: SchemaModifiedEvent on DummySchemaContext] When the edit form for the content type is loaded, the vocabulary @@ -176,10 +152,6 @@ values specified can be selected. >>> from_ctl <ListControl name='form.widgets.categories.from' type='select'> >>> to_ctl = browser.getControl(name="form.widgets.categories.to") - >>> item = to_ctl.getControl('Plone') - >>> item - <ItemControl name='form.widgets.categories.to' type='select' - optionValue='Plone' selected=False> >>> item = from_ctl.getControl('Python') >>> to_ctl.mech_control.items.append(item.mech_item) @@ -187,4 +159,4 @@ values specified can be selected. >>> from_ctl.getControl('Lisp').selected False >>> to_ctl.options - ['Plone', 'Python'] + ['Python'] diff --git a/plone/schemaeditor/tests/editing.txt b/plone/schemaeditor/tests/editing.txt index 02146ba..7e31cff 100644 --- a/plone/schemaeditor/tests/editing.txt +++ b/plone/schemaeditor/tests/editing.txt @@ -73,10 +73,10 @@ Let's navigate to the 'favorite-color' field we just created:: >>> "Edit Field 'favorite_color'" in browser.contents True -Now we can change various attributes. For instance, let's change the default -value of the 'color' field to 'orange':: +Now we can change various attributes. For instance, let's change the help text +for the 'color' field:: - >>> browser.getControl('Default Value').value = 'orange' + >>> browser.getControl('Description').value = 'Enter your favorite color.' And now click the button to save the change. This should take us back to the list of schema fields, which should reflect the change:: @@ -89,8 +89,8 @@ of schema fields, which should reflect the change:: Let's confirm that the new default value was correctly saved to the actual schema:: - >>> IDummySchema['favorite_color'].default - u'orange' + >>> IDummySchema['favorite_color'].description + u'Enter your favorite color.' Let's go back and try to make an invalid change. The form won't let us:: diff --git a/plone/schemaeditor/tests/minmax.txt b/plone/schemaeditor/tests/minmax.txt index 5d4854e..ff0fde7 100644 --- a/plone/schemaeditor/tests/minmax.txt +++ b/plone/schemaeditor/tests/minmax.txt @@ -50,25 +50,13 @@ Set the range to 13 to 100. [event: ObjectModifiedEvent on Int] [event: SchemaModifiedEvent on DummySchemaContext] -Return to the form and try to set a default outside the range. This is -disallowed. - - >>> browser.getLink(url='age').click() - >>> browser.getControl(name='form.widgets.default').value = '1' - >>> browser.getControl('Save').click() - [event: ObjectModifiedEvent on Int] - >>> print browser.contents - <... - <div class="error">Value is too small</div> - ... - Return to the form and set the range to values outside the current range. + >>> browser.getLink(url='age').click() >>> browser.getControl(name='form.widgets.min').value = '0' >>> browser.getControl(name='form.widgets.max').value = '200' >>> browser.getControl('Save').click() [event: ObjectModifiedEvent on Int] - [event: ObjectModifiedEvent on Int] [event: SchemaModifiedEvent on DummySchemaContext] This should complete without error. diff --git a/setup.py b/setup.py index eb7dce3..13d1609 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '1.1.3dev' +version = '1.2.0dev' setup(name='plone.schemaeditor', version=version, |