Exemple #1
0
    def setUpWidgets(self, ignore_request=False):
        super(EditForm, self).setUpWidgets(ignore_request=ignore_request)

        # for translations, add a ``render_original`` method to each
        # widget, which will render the display widget bound to the
        # original (HEAD) document
        if self.is_translation:
            head = self.context.head
            form_fields = setUpFields(self.context.__class__, "view")
            for widget in self.widgets:
                form_field = form_fields.get(widget.context.__name__)
                if form_field is None:
                    form_field = form.Field(widget.context)

                # bind field to head document
                field = form_field.field.bind(head)

                # create custom widget or instantiate widget using
                # component lookup
                if form_field.custom_widget is not None:
                    display_widget = form_field.custom_widget(
                        field, self.request)
                else:
                    display_widget = component.getMultiAdapter(
                        (field, self.request), IDisplayWidget)

                display_widget.setRenderedValue(field.get(head))

                # attach widget as ``render_original``
                widget.render_original = display_widget
Exemple #2
0
    def setUpWidgets(self, ignore_request=False):
        self.set_untranslatable_fields_for_display()

        #get the translation if available
        language = self.request.get('language')

        translation = get_translation_for(self.context, language)
        if translation:
            self.is_translation = True
        else:
            self.is_translation = False
        context = copy(removeSecurityProxy(self.context))
        for field_translation in translation:
            setattr(context, field_translation.field_name,
                    field_translation.field_text)
        self.widgets = form.setUpEditWidgets(self.form_fields,
                                             self.prefix,
                                             context,
                                             self.request,
                                             adapters=self.adapters,
                                             ignore_request=ignore_request)

        if language is not None:
            widget = self.widgets['language']
            try:
                self.language = language
                widget.vocabulary = CurrentLanguageVocabulary().__call__(self)
                widget.vocabulary.getTermByToken(language)
            except LookupError:
                raise BadRequest("No such language token: '%s'" % language)

            # if the term exists in the vocabulary, set the value on
            # the widget
            widget.setRenderedValue(language)
        # for translations, add a ``render_original`` method to each
        # widget, which will render the display widget bound to the
        # original (HEAD) document
        head = self.context
        form_fields = setUpFields(self.context.__class__, "view")
        for widget in self.widgets:
            form_field = form_fields.get(widget.context.__name__)
            if form_field is None:
                form_field = form.Field(widget.context)

            # bind field to head document
            field = form_field.field.bind(head)

            # create custom widget or instantiate widget using
            # component lookup
            if form_field.custom_widget is not None:
                display_widget = form_field.custom_widget(field, self.request)
            else:
                display_widget = component.getMultiAdapter(
                    (field, self.request), IDisplayWidget)

            display_widget.setRenderedValue(field.get(head))

            # attach widget as ``render_original``
            widget.render_original = display_widget
def setUpFields( domain_model, mode ):
    """
    setup form fields for add/edit/view/search modes, with custom widgets
    enabled from model descriptor. this expects the domain model and mode
    passed in and will return a form.Fields instance
    """
    
    domain_model = removeSecurityProxy( domain_model )
    t = time.time()

    domain_interface = model.queryModelInterface( domain_model )
    domain_annotation = model.queryModelDescriptor( domain_interface )

    search_mode = mode == 'search'

    if not domain_annotation:
        if search_mode:
            form_fields = form.Fields( *setUpSearchFields( domain_interface ) )
        else:
            form_fields = form.Fields( domain_interface )
        return form_fields

    fields = []
    columns = getattr( domain_annotation, '%s_columns'%mode )
    

    
    for field_info in columns:
        if not field_info.name in domain_interface:
            #print "bad field", field_info.name, domain_interface.__name__
            continue
        custom_widget = getattr( field_info, "%s_widget"%mode )
        if search_mode:
            fields.append(
                form.Field( setUpSearchField( domain_interface[ field_info.name ] ),
                            custom_widget = custom_widget )
                )
        else:
            fields.append(
                form.Field( domain_interface[ field_info.name ],
                            custom_widget = custom_widget )
                )
    form_fields = form.Fields( *fields )
    #print "field setup cost", time.time()-t    
    return form_fields
Exemple #4
0
def setUpFields(domain_model, mode):
    """
    setup form fields for add/edit/view/search modes, with custom widgets
    enabled from model descriptor. this expects the domain model and mode
    passed in and will return a form.Fields instance
    """
    domain_model = removeSecurityProxy(domain_model)
    #import time
    #t = time.time()
    table_schema = bungeni.alchemist.utils.get_derived_table_schema(
        domain_model)
    descriptor_model = bungeni.alchemist.utils.get_descriptor(table_schema)

    search_mode = mode == "search"

    if not descriptor_model:
        if search_mode:
            form_fields = form.Fields(*setUpSearchFields(table_schema))
        else:
            form_fields = form.Fields(table_schema)
        return form_fields

    fields = []
    columns = getattr(descriptor_model, "%s_columns" % mode)

    for field_info in columns:
        if not field_info.name in table_schema:
            #print "bad field", field_info.name, table_schema.__name__
            continue
        custom_widget = getattr(field_info, "%s_widget" % mode)
        if search_mode:
            fields.append(
                form.Field(setUpSearchField(table_schema[field_info.name]),
                           custom_widget=custom_widget))
        else:
            fields.append(
                form.Field(table_schema[field_info.name],
                           custom_widget=custom_widget))
    form_fields = form.Fields(*fields)
    #print "field setup cost", time.time()-t
    return form_fields