Ejemplo n.º 1
0
    def prepare_template_vars(self, name_prefix, data):
        '''Prepare data for the template.
        
        The markup for the object will be generated here, i.e before the 
        actual template is called to render().
        '''

        tpl_vars = FieldWidget.prepare_template_vars(self, name_prefix, data)
        field, value = self.field, self.value
        qname = tpl_vars.get('qname')

        # If value is None, pass an empty (but structured) object created
        # from the default factory
        if value is None:
            factory = Object.Factory(self.field.schema)
            value = factory()

        # Build the template context for the object's widget: some variables
        # must be moved from current field context to the object's context
        data1 = {}
        for k in ['title', 'description', 'required', 'readonly']:
            v = tpl_vars.pop(k, None)
            if v:
                data1[k] = v

        # Generate markup for the object, feed to the current template context
        qa = self.context.requested_action
        markup = markup_for_object(qa,
                                   value,
                                   errors=self.errors,
                                   name_prefix=qname,
                                   data=data1)
        tpl_vars['content'] = {'markup': markup}

        return tpl_vars
Ejemplo n.º 2
0
    def _from_xml(self, e):
        f = self.field

        obj = None
        if f.context and isinstance(f.context.value, Object):
            obj = f.context.value
        else:
            obj = Object.Factory(f.schema)()

        ys = serializer_for_object(obj)
        ys.target_namespace = self.target_namespace

        p = e[0]

        o = ys.from_xml(p)
        return o
Ejemplo n.º 3
0
    def _to_xsd_type(self, type_prefix):
        xsd_uri = self.nsmap['xs']
        f = self.field

        obj = None
        if f.context and isinstance(f.context.value, Object):
            obj = f.context.value
        else:
            obj = Object.Factory(f.schema)()

        # Create an xs:complexType element

        e = Element(QName(xsd_uri, 'complexType'))
        e1 = SubElement(e, QName(xsd_uri, 'sequence'))

        # Append the XSD definition for obj-typed objects

        ys = serializer_for_object(obj)
        ys.target_namespace = self.target_namespace

        ye, ye_tdefs = ys.to_xsd(type_prefix=type_prefix)
        e1.append(ye)

        return (e, ye_tdefs)