Skip to content

Commit

Permalink
set extra attributes when creating a new field
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault committed Jun 15, 2015
1 parent 87e00ff commit a5f1d40
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
2.0.6 (unreleased)
------------------

- Nothing changed yet.
- Supports additionalSchemata in field Add form
[ebrehault]


2.0.5 (2015-06-05)
Expand Down
50 changes: 44 additions & 6 deletions plone/schemaeditor/browser/schema/add_field.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
from zope.cachedescriptors.property import Lazy as lazy_property
from zope.component import getAdapters
from zope.event import notify
from zope.interface import Invalid
from zope.interface import Invalid, Interface
from zope.lifecycleevent import ObjectAddedEvent
from z3c.form import form, field
from z3c.form.interfaces import WidgetActionExecutionError
from plone.autoform.form import AutoExtensibleForm
from plone.z3cform.layout import wrap_form

from plone.schemaeditor import SchemaEditorMessageFactory as _
from plone.schemaeditor.interfaces import INewField
from plone.schemaeditor import interfaces
from plone.schemaeditor.utils import IEditableSchema, non_fieldset_fields,\
sortedFields
from plone.schemaeditor.utils import FieldAddedEvent


class FieldAddForm(form.AddForm):
class FieldAddForm(AutoExtensibleForm, form.AddForm):

fields = field.Fields(INewField)
fields = field.Fields(interfaces.INewField)
label = _("Add new field")
id = 'add-field-form'

# This is a trick: we want autoform to handle the additionalSchemata,
# but want to provide our own base schema below in updateFields.
schema = Interface

@lazy_property
def _schema(self):
return interfaces.INewField

@lazy_property
def additionalSchemata(self):
return [v for k, v in getAdapters((self.context, ),
interfaces.IFieldEditorExtender)]

def create(self, data):
extra = {}
factory = data.pop('factory')
return factory(**data)
all = data.keys()

# split regular attributes and extra ones
for key in all:
if key not in self._schema:
extra[key] = data[key]
data.pop(key)

# create the field with regular attributes
field_obj = factory(**data)

# set the extra attributes using the proper adapter
for schemata in self.additionalSchemata:
for key in extra:
(interface_name, property_name) = key.split('.')
if interface_name != schemata.__name__:
continue
setattr(schemata(field_obj), property_name, extra[key])

return field_obj

def add(self, field):
context = self.context
Expand All @@ -39,7 +75,9 @@ def add(self, field):
schema.addField(field)
except ValueError:
raise WidgetActionExecutionError('__name__',
Invalid(u'Please select a field name that is not already used.'))
Invalid(
u'Please select a field name that is not already used.'
))

schema.moveField(field.__name__, position)
notify(ObjectAddedEvent(field, context.schema))
Expand Down

0 comments on commit a5f1d40

Please sign in to comment.