def write(self, fieldNode, schema, field):
        name = field.__name__
        
        widget  = schema.queryTaggedValue(WIDGETS_KEY, {}).get(name, None)
        mode    = [(i,v) for i,n,v in schema.queryTaggedValue(MODES_KEY, []) if n == name]
        omitted = [(i,v) for i,n,v in schema.queryTaggedValue(OMITTED_KEY, []) if n == name]
        order   = [(d,v) for n,d,v in schema.queryTaggedValue(ORDER_KEY,  []) if n == name]
        
        if widget is not None:
            if not isinstance(widget, basestring):
                widget = "%s.%s" % (widget.__module__, widget.__name__)
            fieldNode.set(ns('widget', self.namespace), str(widget))
        
        mode_values = []
        for interface, value in mode:
            if interface is not Interface:
                value = "%s:%s" % (interface.__identifier__, value)
            mode_values.append(value)
        if mode_values:
            fieldNode.set(ns('mode', self.namespace), " ".join(mode_values))
        
        omitted_values = []
        for interface, value in omitted:
            if interface is not Interface:
                value = "%s:%s" % (interface.__identifier__, value)
            omitted_values.append(value)
        if omitted_values:
            fieldNode.set(ns('omitted', self.namespace), " ".join(omitted_values))

        for direction, relative_to in order:
            if direction == 'before':
                fieldNode.set(ns('before',  self.namespace), relative_to)
            elif direction == 'after':
                fieldNode.set(ns('after',  self.namespace), relative_to)
    def test_read(self):
        field_node = etree.Element('field')
        field_node.set(ns("widget", self.namespace),
                       "z3c.form.browser.password.PasswordFieldWidget")
        field_node.set(ns("mode", self.namespace), "hidden")
        field_node.set(ns("omitted", self.namespace), "true")
        field_node.set(ns("before", self.namespace), "somefield")
        field_node.set(ns("validator", self.namespace),
                       "plone.autoform.tests.test_utils.TestValidator")

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy")

        handler = FormSchema()
        handler.read(field_node, IDummy, IDummy['dummy'])

        self.assertEquals(
            {'dummy': 'z3c.form.browser.password.PasswordFieldWidget'},
            IDummy.getTaggedValue(WIDGETS_KEY))
        self.assertEquals([(Interface, 'dummy', 'true')],
                          IDummy.getTaggedValue(OMITTED_KEY))
        self.assertEquals([(Interface, 'dummy', 'hidden')],
                          IDummy.getTaggedValue(MODES_KEY))
        self.assertEquals([(
            'dummy',
            'before',
            'somefield',
        )], IDummy.getTaggedValue(ORDER_KEY))
        validator = getMultiAdapter((None, None, None, IDummy['dummy'], None),
                                    IValidator)
        from plone.autoform.tests.test_utils import TestValidator
        assert isinstance(validator, TestValidator)
    def test_read_values_with_interfaces(self):
        field_node1 = etree.Element('field')
        field_node1.set(ns("mode", self.namespace),
                        "z3c.form.interfaces.IForm:hidden")
        field_node1.set(ns("omitted", self.namespace),
                        "z3c.form.interfaces.IForm:true")

        field_node2 = etree.Element('field')
        field_node2.set(
            ns("mode", self.namespace),
            "z3c.form.interfaces.IForm:hidden z3c.form.interfaces.IEditForm:display"
        )
        field_node2.set(
            ns("omitted", self.namespace),
            "z3c.form.interfaces.IForm:true z3c.form.interfaces.IEditForm:false"
        )

        class IDummy(Interface):
            dummy1 = zope.schema.TextLine(title=u"dummy1")
            dummy2 = zope.schema.TextLine(title=u"dummy2")

        handler = FormSchema()
        handler.read(field_node1, IDummy, IDummy['dummy1'])
        handler.read(field_node2, IDummy, IDummy['dummy2'])

        expected_modes = [(IForm, u'dummy1', 'hidden'),
                          (IForm, u'dummy2', 'hidden'),
                          (IEditForm, u'dummy2', 'display')]
        self.assertEquals(expected_modes, IDummy.queryTaggedValue(MODES_KEY))
        expected_omitted = [(IForm, u'dummy1', 'true'),
                            (IForm, u'dummy2', 'true'),
                            (IEditForm, u'dummy2', 'false')]
        self.assertEquals(expected_omitted,
                          IDummy.queryTaggedValue(OMITTED_KEY))
    def test_write_values_with_interfaces(self):
        field_node1 = etree.Element('field')
        field_node2 = etree.Element('field')

        class IDummy(Interface):
            dummy1 = zope.schema.TextLine(title=u"dummy1")
            dummy2 = zope.schema.TextLine(title=u"dummy2")

        modes_values = [(IForm, u'dummy1', 'hidden'),
                        (IForm, u'dummy2', 'hidden'),
                        (IEditForm, u'dummy2', 'display')]
        IDummy.setTaggedValue(MODES_KEY, modes_values)
        omitted_values = [(IForm, u'dummy1', 'true'),
                          (IForm, u'dummy2', 'true'),
                          (IEditForm, u'dummy2', 'false')]
        IDummy.setTaggedValue(OMITTED_KEY, omitted_values)

        handler = FormSchema()
        handler.write(field_node1, IDummy, IDummy['dummy1'])
        handler.write(field_node2, IDummy, IDummy['dummy2'])

        self.assertEquals("z3c.form.interfaces.IForm:hidden",
                          field_node1.get(ns("mode", self.namespace)))
        self.assertEquals("z3c.form.interfaces.IForm:true",
                          field_node1.get(ns("omitted", self.namespace)))

        self.assertEquals(
            "z3c.form.interfaces.IForm:hidden z3c.form.interfaces.IEditForm:display",
            field_node2.get(ns("mode", self.namespace)))
        self.assertEquals(
            "z3c.form.interfaces.IForm:true z3c.form.interfaces.IEditForm:false",
            field_node2.get(ns("omitted", self.namespace)))
    def test_write(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")

        IDummy.setTaggedValue(WIDGETS_KEY, {'dummy': 'SomeWidget'})
        IDummy.setTaggedValue(OMITTED_KEY, [(Interface, 'dummy', 'true')])
        IDummy.setTaggedValue(MODES_KEY, [(Interface, 'dummy', 'hidden')])
        IDummy.setTaggedValue(ORDER_KEY, [(
            'dummy',
            'before',
            'somefield',
        )])

        handler = FormSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        widget_node = field_node.find(ns('widget', self.namespace))
        self.assertEquals("SomeWidget", widget_node.get('type'))
        self.assertEquals("true", field_node.get(ns("omitted",
                                                    self.namespace)))
        self.assertEquals("hidden", field_node.get(ns("mode", self.namespace)))
        self.assertEquals("somefield",
                          field_node.get(ns("before", self.namespace)))
 def read(self, fieldNode, schema, field):
     name = field.__name__
     for i in ["TDefault", "TEnabled", "TValidator"]:
         value = fieldNode.get(ns(i, self.namespace))
         if value:
             data = schema.queryTaggedValue(i, {})
             data[name] = value
             schema.setTaggedValue(i, data)
     # serverSide
     value = fieldNode.get(ns("serverSide", self.namespace))
     if value:
         data = schema.queryTaggedValue("serverSide", {})
         data[name] = value.lower() == "true"
         schema.setTaggedValue("serverSide", data)
     # validators
     value = fieldNode.get(ns("validators", self.namespace))
     if value:
         data = schema.queryTaggedValue("validators", {})
         data[name] = value.split("|")
         schema.setTaggedValue("validators", data)
     # hidden
     value = fieldNode.get(ns("THidden", self.namespace))
     if value:
         data = schema.queryTaggedValue("THidden", {})
         data[name] = value.lower() == "true"
         schema.setTaggedValue("THidden", data)
 def read(self, fieldNode, schema, field):
     name = field.__name__
     for i in ['TDefault', 'TEnabled', 'TValidator']:
         value = fieldNode.get(ns(i, self.namespace))
         if value:
             data = schema.queryTaggedValue(i, {})
             data[name] = value
             schema.setTaggedValue(i, data)
     # serverSide
     value = fieldNode.get(ns('serverSide', self.namespace))
     if value:
         data = schema.queryTaggedValue('serverSide', {})
         data[name] = value.lower() == 'true'
         schema.setTaggedValue('serverSide', data)
     # validators
     value = fieldNode.get(ns('validators', self.namespace))
     if value:
         data = schema.queryTaggedValue('validators', {})
         data[name] = value.split('|')
         schema.setTaggedValue('validators', data)
     # hidden
     value = fieldNode.get(ns('THidden', self.namespace))
     if value:
         data = schema.queryTaggedValue('THidden', {})
         data[name] = value.lower() == 'true'
         schema.setTaggedValue('THidden', data)
Exemple #8
0
    def write(self, fieldNode, schema, field):
        name = field.__name__

        read_permission = schema.queryTaggedValue(READ_PERMISSIONS_KEY, {}).get(name, None)
        write_permission = schema.queryTaggedValue(WRITE_PERMISSIONS_KEY, {}).get(name, None)

        if read_permission:
            fieldNode.set(ns('read-permission', self.namespace), read_permission)
        if write_permission:
            fieldNode.set(ns('write-permission', self.namespace), write_permission)
Exemple #9
0
    def write(self, fieldNode, schema, field):
        name = field.__name__

        widget = schema.queryTaggedValue(WIDGETS_KEY, {}).get(name, None)
        mode = [
            (i, v) for i, n, v in schema.queryTaggedValue(MODES_KEY, [])
            if n == name
        ]
        omitted = [
            (i, v) for i, n, v in schema.queryTaggedValue(OMITTED_KEY, [])
            if n == name
        ]
        order = [
            (d, v) for n, d, v in schema.queryTaggedValue(ORDER_KEY, [])
            if n == name
        ]

        if widget is not None:
            if not isinstance(widget, ParameterizedWidget):
                widget = ParameterizedWidget(widget)

            if widget.widget_factory or widget.params:
                widgetNode = etree.Element(ns('widget', self.namespace))
                widgetName = widget.getWidgetFactoryName()
                if widgetName is not None:
                    widgetNode.set('type', widgetName)

                widgetHandler = widget.getExportImportHandler(field)
                widgetHandler.write(widgetNode, widget.params)
                fieldNode.append(widgetNode)

        mode_values = []
        for interface, value in mode:
            if interface is not Interface:
                value = "%s:%s" % (interface.__identifier__, value)
            mode_values.append(value)
        if mode_values:
            fieldNode.set(ns('mode', self.namespace), " ".join(mode_values))

        omitted_values = []
        for interface, value in omitted:
            if interface is not Interface:
                value = "%s:%s" % (interface.__identifier__, value)
            omitted_values.append(value)
        if omitted_values:
            fieldNode.set(
                ns('omitted', self.namespace),
                " ".join(omitted_values)
            )

        for direction, relative_to in order:
            if direction == 'before':
                fieldNode.set(ns('before', self.namespace), relative_to)
            elif direction == 'after':
                fieldNode.set(ns('after', self.namespace), relative_to)
 def test_write_no_metadata(self):
     field_node = ElementTree.Element('field')
     
     class IDummy(Interface):
         dummy = zope.schema.TextLine(title=u"dummy")
     
     handler = SecuritySchema()
     handler.write(field_node, IDummy, IDummy['dummy'])
     
     self.assertEquals(None, field_node.get(ns("read-permission", self.namespace)))
     self.assertEquals(None, field_node.get(ns("write-permission", self.namespace)))
Exemple #11
0
def update_schema(xml, schema, name=u'', language=u''):
    root = etree.fromstring(xml)

    if name:
        name = name.strip()
    if language:
        language = u'/' + language.strip() + u'/'
    if isinstance(schema, str):
        schema_root = etree.fromstring(schema)
    else:
        schema_root = etree.fromstring(serializeSchema(schema, name))

    for el in root.findall(ns('schema')):
        name_ = el.attrib.get('name', u'')
        if name_ == name + language:
            root.remove(el)
            break
        elif name_ == name and not language:
            root.remove(el)
            break

    for el in schema_root.findall(ns('schema')):
        name_ = el.attrib.get('name', u'')
        if name_ == name + language:
            root.append(el)
            break
        elif name_ == name and language:
            el.attrib['name'] = language
            root.append(el)
            break
        elif name_ == name and not language:
            root.append(el)
            break

    # Drop default values from fields with defaultFactories
    for factory in root.xpath(
            '//supermodel:defaultFactory',
            namespaces=dict(supermodel=XML_NAMESPACE),
    ):
        for default in factory.itersiblings(ns('default'), preceding=False):
            default.getparent().remove(default)
        for default in factory.itersiblings(ns('default'), preceding=True):
            default.getparent().remove(default)

    return synchronized_schema(
        etree.tostring(
            root,
            pretty_print=True,
            xml_declaration=True,
            encoding='utf8',
        ),
        master=language,
    )
 def test_write_no_data(self):
     field_node = ElementTree.Element('field')
     
     class IDummy(Interface):
         dummy = zope.schema.TextLine(title=u"dummy1")
         
     handler = FormSchema()
     handler.write(field_node, IDummy, IDummy['dummy'])
     
     self.assertEquals(None, field_node.get(ns("widget", self.namespace)))
     self.assertEquals(None, field_node.get(ns("omitted", self.namespace)))
     self.assertEquals(None, field_node.get(ns("mode", self.namespace)))
     self.assertEquals(None, field_node.get(ns("before", self.namespace)))
    def test_write_no_data(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u'dummy1')

        handler = FormSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEqual(None, field_node.find(ns('widget', self.namespace)))
        self.assertEqual(None, field_node.get(ns('omitted', self.namespace)))
        self.assertEqual(None, field_node.get(ns('mode', self.namespace)))
        self.assertEqual(None, field_node.get(ns('before', self.namespace)))
 def test_read(self):
     field_node = ElementTree.Element('field')
     field_node.set(ns("read-permission", self.namespace), "dummy.Read")
     field_node.set(ns("write-permission", self.namespace), "dummy.Write")
     
     class IDummy(Interface):
         dummy = zope.schema.TextLine(title=u"dummy")
     
     handler = SecuritySchema()
     handler.read(field_node, IDummy, IDummy['dummy'])
     
     self.assertEquals({u'dummy': 'dummy.Read'}, IDummy.getTaggedValue(READ_PERMISSIONS_KEY))
     self.assertEquals({u'dummy': 'dummy.Write'}, IDummy.getTaggedValue(WRITE_PERMISSIONS_KEY))
 def test_write_no_permissions(self):
     field_node = ElementTree.Element('field')
     
     class IDummy(Interface):
         dummy = zope.schema.TextLine(title=u"dummy")
     
     IDummy.setTaggedValue(READ_PERMISSIONS_KEY, {u'dummy': None})
     
     handler = SecuritySchema()
     handler.write(field_node, IDummy, IDummy['dummy'])
     
     self.assertEquals(None, field_node.get(ns("read-permission", self.namespace)))
     self.assertEquals(None, field_node.get(ns("write-permission", self.namespace)))
    def test_write_no_data(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")

        handler = FormSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEquals(None, field_node.find(ns("widget", self.namespace)))
        self.assertEquals(None, field_node.get(ns("omitted", self.namespace)))
        self.assertEquals(None, field_node.get(ns("mode", self.namespace)))
        self.assertEquals(None, field_node.get(ns("before", self.namespace)))
    def test_write_no_metadata(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy")

        handler = SecuritySchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEquals(
            None, field_node.get(ns("read-permission", self.namespace)))
        self.assertEquals(
            None, field_node.get(ns("write-permission", self.namespace)))
 def write(self, fieldNode, schema, field):
     name = field.__name__
     for i in ['TDefault', 'TEnabled', 'TValidator']:
         value = schema.queryTaggedValue(i, {}).get(name, None)
         if value:
             fieldNode.set(ns(i, self.namespace), value)
     # serverSide
     value = schema.queryTaggedValue('serverSide', {}).get(name, None)
     if isinstance(value, bool):
         fieldNode.set(ns('serverSide', self.namespace), str(value))
     # validators
     value = schema.queryTaggedValue('validators', {}).get(name, None)
     if value:
         fieldNode.set(ns('validators', self.namespace), "|".join(value))
Exemple #19
0
    def write(self, fieldNode, schema, field):
        name = field.__name__

        read_permission = schema.queryTaggedValue(READ_PERMISSIONS_KEY,
                                                  {}).get(name, None)
        write_permission = schema.queryTaggedValue(WRITE_PERMISSIONS_KEY,
                                                   {}).get(name, None)

        if read_permission:
            fieldNode.set(ns('read-permission', self.namespace),
                          read_permission)
        if write_permission:
            fieldNode.set(ns('write-permission', self.namespace),
                          write_permission)
 def write(self, fieldNode, schema, field):
     name = field.__name__
     for i in ["TDefault", "TEnabled", "TValidator"]:
         value = schema.queryTaggedValue(i, {}).get(name, None)
         if value:
             fieldNode.set(ns(i, self.namespace), value)
     # serverSide
     value = schema.queryTaggedValue("serverSide", {}).get(name, None)
     if isinstance(value, bool):
         fieldNode.set(ns("serverSide", self.namespace), str(value))
     # validators
     value = schema.queryTaggedValue("validators", {}).get(name, None)
     if value:
         fieldNode.set(ns("validators", self.namespace), "|".join(value))
 def write(self, fieldNode, schema, field):
     name = field.__name__
     for i in ['TDefault', 'TEnabled', 'TValidator']:
         value = schema.queryTaggedValue(i, {}).get(name, None)
         if value:
             fieldNode.set(ns(i, self.namespace), value)
     # serverSide
     value = schema.queryTaggedValue('serverSide', {}).get(name, None)
     if isinstance(value, bool):
         fieldNode.set(ns('serverSide', self.namespace), str(value))
     # validators
     value = schema.queryTaggedValue('validators', {}).get(name, None)
     if value:
         fieldNode.set(ns('validators', self.namespace), "|".join(value))
    def test_read_values_with_interfaces(self):
        field_node1 = etree.Element('field')
        field_node1.set(
            ns('mode', self.namespace),
            'z3c.form.interfaces.IForm:hidden'
        )
        field_node1.set(
            ns('omitted', self.namespace),
            'z3c.form.interfaces.IForm:true'
        )

        field_node2 = etree.Element('field')
        field_node2.set(
            ns('mode', self.namespace),
            'z3c.form.interfaces.IForm:hidden '
            'z3c.form.interfaces.IEditForm:display'
        )
        field_node2.set(
            ns('omitted', self.namespace),
            'z3c.form.interfaces.IForm:true '
            'z3c.form.interfaces.IEditForm:false'
        )

        class IDummy(Interface):
            dummy1 = zope.schema.TextLine(title=u'dummy1')
            dummy2 = zope.schema.TextLine(title=u'dummy2')

        handler = FormSchema()
        handler.read(field_node1, IDummy, IDummy['dummy1'])
        handler.read(field_node2, IDummy, IDummy['dummy2'])

        expected_modes = [
            (IForm, u'dummy1', 'hidden'),
            (IForm, u'dummy2', 'hidden'),
            (IEditForm, u'dummy2', 'display')
        ]
        self.assertEqual(
            expected_modes,
            IDummy.queryTaggedValue(MODES_KEY)
        )
        expected_omitted = [
            (IForm, u'dummy1', 'true'),
            (IForm, u'dummy2', 'true'),
            (IEditForm, u'dummy2', 'false')
        ]
        self.assertEqual(
            expected_omitted,
            IDummy.queryTaggedValue(OMITTED_KEY)
        )
    def test_write_no_permissions(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy")

        IDummy.setTaggedValue(READ_PERMISSIONS_KEY, {u'dummy': None})

        handler = SecuritySchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEquals(
            None, field_node.get(ns("read-permission", self.namespace)))
        self.assertEquals(
            None, field_node.get(ns("write-permission", self.namespace)))
    def test_read(self):
        field_node = etree.Element('field')
        field_node.set(ns("read-permission", self.namespace), "dummy.Read")
        field_node.set(ns("write-permission", self.namespace), "dummy.Write")

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy")

        handler = SecuritySchema()
        handler.read(field_node, IDummy, IDummy['dummy'])

        self.assertEquals({u'dummy': 'dummy.Read'},
                          IDummy.getTaggedValue(READ_PERMISSIONS_KEY))
        self.assertEquals({u'dummy': 'dummy.Write'},
                          IDummy.getTaggedValue(WRITE_PERMISSIONS_KEY))
 def read(self, fieldNode, schema, field):
     name = field.__name__
     value = fieldNode.get(ns(self.xml_attr, self.namespace))
     if value:
         values = schema.queryTaggedValue(self.tag_key, {})
         values[name] = value
         schema.setTaggedValue(self.tag_key, values)
 def read(self, fieldNode, schema, field):
     name = field.__name__
     value = fieldNode.get(ns('execCondition', self.namespace))
     data = schema.queryTaggedValue('execCondition', {})
     if value:
         data[name] = value
         schema.setTaggedValue('execCondition', data)
 def read(self, fieldNode, schema, field):
     name = field.__name__
     value = fieldNode.get(ns("execCondition", self.namespace))
     data = schema.queryTaggedValue("execCondition", {})
     if value:
         data[name] = value
         schema.setTaggedValue("execCondition", data)
    def test_read_parameterized_widget_default(self):
        from plone.autoform.widgets import ParameterizedWidget

        param_node = etree.Element('klass')
        param_node.text = 'custom'
        param2_node = etree.Element('placeholder')
        param2_node.text = 'help'
        widget_node = etree.Element(ns('widget', self.namespace))
        widget_node.append(param_node)
        widget_node.append(param2_node)
        field_node = etree.Element('field')
        field_node.append(widget_node)

        class IDummy(Interface):
            foo = zope.schema.TextLine(title=u'foo')

        handler = FormSchema()
        handler.read(field_node, IDummy, IDummy['foo'])

        widgets = IDummy.queryTaggedValue(WIDGETS_KEY)
        self.assertTrue(isinstance(widgets['foo'], ParameterizedWidget))
        self.assertTrue(widgets['foo'].widget_factory is None)
        self.assertIn('klass', widgets['foo'].params)
        self.assertEqual(widgets['foo'].params['klass'], 'custom')
        self.assertIn('placeholder', widgets['foo'].params)
        self.assertEqual(widgets['foo'].params['placeholder'], 'help')
Exemple #29
0
    def test_read_parameterized_widget_default(self):
        from plone.autoform.widgets import ParameterizedWidget

        param_node = etree.Element('klass')
        param_node.text = 'custom'
        param2_node = etree.Element('placeholder')
        param2_node.text = 'help'
        widget_node = etree.Element(ns('widget', self.namespace))
        widget_node.append(param_node)
        widget_node.append(param2_node)
        field_node = etree.Element('field')
        field_node.append(widget_node)

        class IDummy(Interface):
            foo = zope.schema.TextLine(title=u'foo')

        handler = FormSchema()
        handler.read(field_node, IDummy, IDummy['foo'])

        widgets = IDummy.queryTaggedValue(WIDGETS_KEY)
        self.assertTrue(isinstance(widgets['foo'], ParameterizedWidget))
        self.assertTrue(widgets['foo'].widget_factory is None)
        self.assertIn('klass', widgets['foo'].params)
        self.assertEqual(widgets['foo'].params['klass'], 'custom')
        self.assertIn('placeholder', widgets['foo'].params)
        self.assertEqual(widgets['foo'].params['placeholder'], 'help')
Exemple #30
0
    def read(self, fieldNode, schema, field):
        name = field.__name__
        searchable = fieldNode.get(ns('searchable', self.namespace))

        if searchable:
            value = (Interface, name, 'true')
            self._add_searchable(schema, value)
Exemple #31
0
    def write(self, fieldNode, schema, field):
        name = field.__name__
        searchable = schema.queryTaggedValue(SEARCHABLE_KEY, [])
        field_names = [fld[1] for fld in searchable]

        if name in field_names:
            fieldNode.set(ns('searchable', self.namespace), 'true')
Exemple #32
0
    def read(self, fieldNode, schema, field):
        name = field.__name__

        read_permission = fieldNode.get(ns('read-permission', self.namespace))
        write_permission = fieldNode.get(ns('write-permission', self.namespace))

        read_permissions = schema.queryTaggedValue(READ_PERMISSIONS_KEY, {})
        write_permissions = schema.queryTaggedValue(WRITE_PERMISSIONS_KEY, {})

        if read_permission:
            read_permissions[name] = read_permission
            schema.setTaggedValue(READ_PERMISSIONS_KEY, read_permissions)

        if write_permission:
            write_permissions[name] = write_permission
            schema.setTaggedValue(WRITE_PERMISSIONS_KEY, write_permissions)
Exemple #33
0
 def read(self, fieldNode, schema, field):
     primary = fieldNode.get(ns('primary', self.namespace))
     if (
         primary is not None and
         primary.lower() in ("true", "on", "yes", "y", "1")
     ):
         alsoProvides(field, IPrimaryField)
Exemple #34
0
    def read(self, fieldNode, schema, field):
        name = field.__name__

        read_permission = fieldNode.get(ns('read-permission', self.namespace))
        write_permission = fieldNode.get(ns('write-permission',
                                            self.namespace))

        read_permissions = schema.queryTaggedValue(READ_PERMISSIONS_KEY, {})
        write_permissions = schema.queryTaggedValue(WRITE_PERMISSIONS_KEY, {})

        if read_permission:
            read_permissions[name] = read_permission
            schema.setTaggedValue(READ_PERMISSIONS_KEY, read_permissions)

        if write_permission:
            write_permissions[name] = write_permission
            schema.setTaggedValue(WRITE_PERMISSIONS_KEY, write_permissions)
Exemple #35
0
    def test_write(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u'dummy')

        IDummy.setTaggedValue(READ_PERMISSIONS_KEY, {u'dummy': 'dummy.Read'})
        IDummy.setTaggedValue(WRITE_PERMISSIONS_KEY, {u'dummy': 'dummy.Write'})

        handler = SecuritySchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEqual('dummy.Read',
                         field_node.get(ns('read-permission', self.namespace)))
        self.assertEqual(
            'dummy.Write',
            field_node.get(ns('write-permission', self.namespace)))
    def test_write_partial(self):
        field_node = ElementTree.Element('field')
        field_node2 = ElementTree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")
            dummy2 = zope.schema.TextLine(title=u"dummy2")

        IDummy.setTaggedValue(SEARCHABLE_KEY, [(Interface, 'dummy', 'true')])

        handler = IndexerSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])
        handler.write(field_node2, IDummy, IDummy['dummy2'])

        self.assertEquals("true",
                          field_node.get(ns("searchable", self.namespace)))
        self.assertEquals(None,
                          field_node2.get(ns("searchable", self.namespace)))
    def test_write_partial(self):
        field_node = ElementTree.Element('field')
        field_node2 = ElementTree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")
            dummy2 = zope.schema.TextLine(title=u"dummy2")

        IDummy.setTaggedValue(SEARCHABLE_KEY, [(Interface, 'dummy', 'true')])

        handler = IndexerSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])
        handler.write(field_node2, IDummy, IDummy['dummy2'])

        self.assertEquals("true",
                          field_node.get(ns("searchable", self.namespace)))
        self.assertEquals(None,
                          field_node2.get(ns("searchable", self.namespace)))
 def test_write_partial(self):
     field_node = ElementTree.Element('field')
     
     class IDummy(Interface):
         dummy = zope.schema.TextLine(title=u"dummy1")
     
     IDummy.setTaggedValue(WIDGETS_KEY, {'dummy': 'SomeWidget'})
     IDummy.setTaggedValue(OMITTED_KEY, [(Interface, 'dummy2', 'true')])
     IDummy.setTaggedValue(MODES_KEY, [(Interface, 'dummy', 'display'), (Interface, 'dummy2', 'hidden')])
     IDummy.setTaggedValue(ORDER_KEY, [])
     
     handler = FormSchema()
     handler.write(field_node, IDummy, IDummy['dummy'])
     
     self.assertEquals("SomeWidget", field_node.get(ns("widget", self.namespace)))
     self.assertEquals(None, field_node.get(ns("omitted", self.namespace)))
     self.assertEquals("display", field_node.get(ns("mode", self.namespace)))
     self.assertEquals(None, field_node.get(ns("before", self.namespace)))
 def write(self, fieldNode, schema, field):
     name = field.__name__
     for i in ["TDefault", "TEnabled", "TValidator"]:
         value = schema.queryTaggedValue(i, {}).get(name, None)
         if value:
             fieldNode.set(ns(i, self.namespace), value)
     # serverSide
     value = schema.queryTaggedValue("serverSide", {}).get(name, None)
     if isinstance(value, bool):
         fieldNode.set(ns("serverSide", self.namespace), str(value))
     # validators
     value = schema.queryTaggedValue("validators", {}).get(name, None)
     if value:
         fieldNode.set(ns("validators", self.namespace), "|".join(value))
     # hidden
     value = schema.queryTaggedValue("THidden", {}).get(name, None)
     if isinstance(value, bool):
         fieldNode.set(ns("THidden", self.namespace), str(value))
Exemple #40
0
def merge_vocabularies(from_node, to_node):
    if from_node.tag == ns('values'):
        vocabulary = {}
        for term in from_node.iterchildren():
            vocabulary[term.attrib.get('key') or term.text] = term.text
        for term in to_node.iterchildren():
            key = term.attrib.get('key') or term.text
            term.text = vocabulary.get(key) or term.text
    return to_node
    def test_write_values_with_interfaces(self):
        field_node1 = etree.Element('field')
        field_node2 = etree.Element('field')

        class IDummy(Interface):
            dummy1 = zope.schema.TextLine(title=u'dummy1')
            dummy2 = zope.schema.TextLine(title=u'dummy2')

        modes_values = [
            (IForm, u'dummy1', 'hidden'),
            (IForm, u'dummy2', 'hidden'),
            (IEditForm, u'dummy2', 'display')
        ]
        IDummy.setTaggedValue(MODES_KEY, modes_values)
        omitted_values = [
            (IForm, u'dummy1', 'true'),
            (IForm, u'dummy2', 'true'),
            (IEditForm, u'dummy2', 'false')
        ]
        IDummy.setTaggedValue(OMITTED_KEY, omitted_values)

        handler = FormSchema()
        handler.write(field_node1, IDummy, IDummy['dummy1'])
        handler.write(field_node2, IDummy, IDummy['dummy2'])

        self.assertEqual(
            'z3c.form.interfaces.IForm:hidden',
            field_node1.get(ns('mode', self.namespace))
        )
        self.assertEqual(
            'z3c.form.interfaces.IForm:true',
            field_node1.get(ns('omitted', self.namespace))
        )

        self.assertEqual(
            'z3c.form.interfaces.IForm:hidden '
            'z3c.form.interfaces.IEditForm:display',
            field_node2.get(ns('mode', self.namespace))
        )
        self.assertEqual(
            'z3c.form.interfaces.IForm:true '
            'z3c.form.interfaces.IEditForm:false',
            field_node2.get(ns('omitted', self.namespace))
        )
    def test_write(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")

        IDummy.setTaggedValue(WIDGETS_KEY, {'dummy': 'SomeWidget'})
        IDummy.setTaggedValue(OMITTED_KEY, [(Interface, 'dummy', 'true')])
        IDummy.setTaggedValue(MODES_KEY, [(Interface, 'dummy', 'hidden')])
        IDummy.setTaggedValue(ORDER_KEY, [('dummy', 'before', 'somefield',)])

        handler = FormSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        widget_node = field_node.find(ns('widget', self.namespace))
        self.assertEquals("SomeWidget", widget_node.get('type'))
        self.assertEquals("true", field_node.get(ns("omitted", self.namespace)))
        self.assertEquals("hidden", field_node.get(ns("mode", self.namespace)))
        self.assertEquals("somefield", field_node.get(ns("before", self.namespace)))
    def test_read(self):
        field_node = etree.Element('field')
        field_node.set(
            ns('widget', self.namespace),
            'z3c.form.browser.password.PasswordFieldWidget'
        )
        field_node.set(ns('mode', self.namespace), 'hidden')
        field_node.set(ns('omitted', self.namespace), 'true')
        field_node.set(ns('before', self.namespace), 'somefield')
        field_node.set(
            ns('validator', self.namespace),
            'plone.autoform.tests.test_utils.TestValidator'
        )

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u'dummy')

        handler = FormSchema()
        handler.read(field_node, IDummy, IDummy['dummy'])

        self.assertEqual(
            {'dummy': 'z3c.form.browser.password.PasswordFieldWidget'},
            IDummy.getTaggedValue(WIDGETS_KEY)
        )
        self.assertEqual(
            [(Interface, 'dummy', 'true')],
            IDummy.getTaggedValue(OMITTED_KEY)
        )
        self.assertEqual(
            [(Interface, 'dummy', 'hidden')],
            IDummy.getTaggedValue(MODES_KEY)
        )
        self.assertEqual(
            [('dummy', 'before', 'somefield',)],
            IDummy.getTaggedValue(ORDER_KEY)
        )
        validator = getMultiAdapter(
            (None, None, None, IDummy['dummy'], None),
            IValidator
        )
        from plone.autoform.tests.test_utils import TestValidator
        assert isinstance(validator, TestValidator)
 def read(self, fieldNode, schema, field):
     name = field.__name__
     for i in ["TDefault", "TEnabled", "TValidator"]:
         value = fieldNode.get(ns(i, self.namespace))
         if value:
             data = schema.queryTaggedValue(i, {})
             data[name] = value
             schema.setTaggedValue(i, data)
     # serverSide
     value = fieldNode.get(ns("serverSide", self.namespace))
     if value:
         data = schema.queryTaggedValue("serverSide", {})
         data[name] = value == "True" or value == "true"
         schema.setTaggedValue("serverSide", data)
     # validators
     value = fieldNode.get(ns("validators", self.namespace))
     if value:
         data = schema.queryTaggedValue("validators", {})
         data[name] = value.split("|")
         schema.setTaggedValue("validators", data)
 def read(self, fieldNode, schema, field):
     name = field.__name__
     for i in ['TDefault', 'TEnabled', 'TValidator']:
         value = fieldNode.get(ns(i, self.namespace))
         if value:
             data = schema.queryTaggedValue(i, {})
             data[name] = value
             schema.setTaggedValue(i, data)
     # serverSide
     value = fieldNode.get(ns('serverSide', self.namespace))
     if value:
         data = schema.queryTaggedValue('serverSide', {})
         data[name] = value == 'True' or value == 'true'
         schema.setTaggedValue('serverSide', data)
     # validators
     value = fieldNode.get(ns('validators', self.namespace))
     if value:
         data = schema.queryTaggedValue('validators', {})
         data[name] = value.split('|')
         schema.setTaggedValue('validators', data)
Exemple #46
0
    def test_write_partial(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u'dummy1')

        IDummy.setTaggedValue(WIDGETS_KEY, {'dummy': 'SomeWidget'})
        IDummy.setTaggedValue(OMITTED_KEY, [(Interface, 'dummy2', 'true')])
        IDummy.setTaggedValue(MODES_KEY, [(Interface, 'dummy', 'display'),
                                          (Interface, 'dummy2', 'hidden')])
        IDummy.setTaggedValue(ORDER_KEY, [])

        handler = FormSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        widget_node = field_node.find(ns('widget', self.namespace))
        self.assertEqual('SomeWidget', widget_node.get('type'))
        self.assertEqual(None, field_node.get(ns('omitted', self.namespace)))
        self.assertEqual('display', field_node.get(ns('mode', self.namespace)))
        self.assertEqual(None, field_node.get(ns('before', self.namespace)))
    def test_write(self):
        field_node = etree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u'dummy')

        IDummy.setTaggedValue(READ_PERMISSIONS_KEY, {u'dummy': 'dummy.Read'})
        IDummy.setTaggedValue(WRITE_PERMISSIONS_KEY, {u'dummy': 'dummy.Write'})

        handler = SecuritySchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEqual(
            'dummy.Read',
            field_node.get(ns('read-permission', self.namespace))
        )
        self.assertEqual(
            'dummy.Write',
            field_node.get(ns('write-permission', self.namespace))
        )
    def test_write_no_data(self):
        field_node = ElementTree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")

        handler = IndexerSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEquals(None, field_node.get(ns("searchable",
                                                  self.namespace)))
    def test_read(self):
        schema_node = ElementTree.Element("schema")
        schema_node.set(ns("field", SF_NAMESPACE), u"Orangutan")

        class IDummy(Interface):
            foo = schema.TextLine()

        handler = self._makeOne()
        handler.read(schema_node, IDummy, IDummy["foo"])

        self.assertEquals({"foo": u"Orangutan"}, IDummy.getTaggedValue("salesforce.fields"))
    def test_read(self):
        schema_node = ElementTree.Element("schema")
        schema_node.set(ns("object", SF_NAMESPACE), u"Orangutan")

        class IDummy(Interface):
            pass

        handler = self._makeOne()
        handler.read(schema_node, IDummy)

        self.assertEquals(u"Orangutan", IDummy.getTaggedValue("salesforce.object"))
    def test_write_no_data(self):
        field_node = ElementTree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy1")

        handler = IndexerSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEquals(None,
                          field_node.get(ns("searchable", self.namespace)))
    def test_read(self):
        field_node = ElementTree.Element('field')
        field_node.set(ns("searchable", self.namespace), "true")

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u"dummy")

        handler = IndexerSchema()
        handler.read(field_node, IDummy, IDummy['dummy'])

        self.assertEquals([(Interface, 'dummy', 'true')],
                          IDummy.getTaggedValue(SEARCHABLE_KEY))
    def test_write(self):
        schema_node = ElementTree.Element("schema")

        class IDummy(Interface):
            foo = schema.TextLine()

        IDummy.setTaggedValue("salesforce.fields", {"foo": u"Chimpanzee"})

        handler = self._makeOne()
        handler.write(schema_node, IDummy, IDummy["foo"])

        self.assertEquals("Chimpanzee", schema_node.get(ns("field", SF_NAMESPACE)))
    def test_read_multiple(self):
        field_node1 = etree.Element('field')
        field_node1.set(ns("widget", self.namespace),
                        "z3c.form.browser.password.PasswordFieldWidget")
        field_node1.set(ns("mode", self.namespace), "hidden")
        field_node1.set(ns("omitted", self.namespace), "true")
        field_node1.set(ns("before", self.namespace), "somefield")

        field_node2 = etree.Element('field')
        field_node2.set(ns("mode", self.namespace), "display")
        field_node2.set(ns("omitted", self.namespace), "yes")

        class IDummy(Interface):
            dummy1 = zope.schema.TextLine(title=u"dummy1")
            dummy2 = zope.schema.TextLine(title=u"dummy2")

        handler = FormSchema()
        handler.read(field_node1, IDummy, IDummy['dummy1'])
        handler.read(field_node2, IDummy, IDummy['dummy2'])

        self.assertEquals(
            {'dummy1': 'z3c.form.browser.password.PasswordFieldWidget'},
            IDummy.getTaggedValue(WIDGETS_KEY))
        self.assertEquals([(Interface, 'dummy1', 'true'),
                           (Interface, 'dummy2', 'yes')],
                          IDummy.getTaggedValue(OMITTED_KEY))
        self.assertEquals([(Interface, 'dummy1', 'hidden'),
                           (Interface, 'dummy2', 'display')],
                          IDummy.getTaggedValue(MODES_KEY))
        self.assertEquals([(
            'dummy1',
            'before',
            'somefield',
        )], IDummy.getTaggedValue(ORDER_KEY))
Exemple #55
0
def split_schema(xml):
    """Split XML supermodel schema into main schemata and additional
    schemata """
    root = etree.fromstring(xml)

    stack = []
    for el in root.findall(ns('schema')):
        if el.attrib.get('name') and not IS_TRANSLATION.match(
                el.attrib.get('name')):
            stack.append(el)
            root.remove(el)

    schemata = etree.tostring(root)

    for el in root.findall(ns('schema')):
        root.remove(el)
    while stack:
        root.append(stack.pop())

    additional_schemata = etree.tostring(root)

    return schemata, additional_schemata
    def test_read_multiple(self):
        field_node1 = ElementTree.Element('field')
        field_node1.set(ns("searchable", self.namespace), "true")

        field_node2 = ElementTree.Element('field')

        field_node3 = ElementTree.Element('field')
        field_node3.set(ns("searchable", self.namespace), "true")

        class IDummy(Interface):
            dummy1 = zope.schema.TextLine(title=u"dummy1")
            dummy2 = zope.schema.TextLine(title=u"dummy2")
            dummy3 = zope.schema.TextLine(title=u"dummy3")

        handler = IndexerSchema()
        handler.read(field_node1, IDummy, IDummy['dummy1'])
        handler.read(field_node2, IDummy, IDummy['dummy2'])
        handler.read(field_node3, IDummy, IDummy['dummy3'])

        self.assertEquals([(Interface, 'dummy1', 'true'),
                           (Interface, 'dummy3', 'true')],
                          IDummy.getTaggedValue(SEARCHABLE_KEY))
Exemple #57
0
    def test_write(self):
        field_node = ElementTree.Element('field')

        class IDummy(Interface):
            dummy = zope.schema.TextLine(title=u'dummy1')

        IDummy.setTaggedValue(SEARCHABLE_KEY, [(Interface, 'dummy', 'true')])

        handler = IndexerSchema()
        handler.write(field_node, IDummy, IDummy['dummy'])

        self.assertEqual(
            'true',
            field_node.get(ns('searchable', self.namespace)))