Example #1
0
    def get_form_helper(self):
        helper = FormHelper()
        helper.form_tag = False
        helper.label_class = 'col-md-1'
        helper.include_media = False
        helper.add_layout(self.get_form_layout())

        # deal with readonly fields
        readonly_fields = self.get_readonly_fields()
        if readonly_fields:
            detail = self.get_model_view(DetailAdminUtil, self.model,
                                         self.form_obj.instance)
            for field in readonly_fields:
                helper[field].wrap(ReadOnlyField, detail=detail)

        return helper
Example #2
0
    def instance_form(self, **kwargs):
        formset = self.get_formset(**kwargs)
        attrs = {'instance': self.model_instance, 'queryset': self.queryset()}
        if self.request_method == 'post':
            attrs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
                'save_as_new': "_saveasnew" in self.request.POST
            })
        instance = formset(**attrs)
        instance.view = self

        helper = FormHelper()
        helper.form_tag = False
        helper.include_media = False
        helper.label_class = 'col-md-1'
        # override form method to prevent render csrf_token in inline forms, see template 'bootstrap/whole_uni_form.html'
        helper.form_method = 'get'

        style = style_manager.get_style('one' if self.max_num ==
                                        1 else self.style)(self, instance)
        style.name = self.style

        if len(instance):
            layout = copy.deepcopy(self.form_layout)

            if layout is None:
                layout = Layout(*instance[0].fields.keys())
            elif type(layout) in (list, tuple) and len(layout) > 0:
                layout = Layout(*layout)

                rendered_fields = [i[1] for i in layout.get_field_names()]
                layout.extend([
                    f for f in instance[0].fields.keys()
                    if f not in rendered_fields
                ])

            helper.add_layout(layout)
            style.update_layout(helper)

            # replace delete field with Dynamic field, for hidden delete field when instance is NEW.
            helper[DELETION_FIELD_NAME].wrap(DeleteField)

        instance.helper = helper
        instance.style = style

        readonly_fields = self.get_readonly_fields()
        if readonly_fields:
            for form in instance:
                form.readonly_fields = []
                try:
                    # only a valid form can execute the save method
                    form_instance = form.save(commit=False)
                except ValueError:
                    form_instance = form.instance
                if form_instance:
                    instance_fields = dict([
                        (f.name, f) for f in form_instance._meta.get_fields()
                    ])
                    for readonly_field in readonly_fields:
                        value = None
                        label = None
                        if readonly_field in instance_fields:
                            label = instance_fields[
                                readonly_field].verbose_name
                            value = smart_text(
                                getattr(form_instance, readonly_field))
                        elif inspect.ismethod(
                                getattr(form_instance, readonly_field, None)):
                            value = getattr(form_instance, readonly_field)()
                            label = getattr(
                                getattr(form_instance, readonly_field),
                                'short_description', readonly_field)
                        elif inspect.ismethod(
                                getattr(self, readonly_field, None)):
                            value = getattr(self,
                                            readonly_field)(form_instance)
                            label = getattr(getattr(self, readonly_field),
                                            'short_description',
                                            readonly_field)
                        if value:
                            form.readonly_fields.append({
                                'label': label,
                                'contents': value
                            })
        return instance