multipleformwizard.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:django-formtools-addons 作者: vikingco 项目源码 文件源码
def compute_form_list(cls, form_list, *args, **kwargs):
        computed_form_list = OrderedDict()

        assert len(form_list) > 0, 'at least one form is needed'

        # walk through the passed form list
        for i, form in enumerate(form_list):
            if isinstance(form, (list, tuple)):
                # if the element is a tuple, add the tuple to the new created
                # sorted dictionary.

                (step_name, form) = form
                if isinstance(form, dict):
                    form_mapping = form
                    computed_form_list[six.text_type(step_name)] = form_mapping
                elif isinstance(form, (list, tuple)):
                    form_mapping = OrderedDict(form)
                    computed_form_list[six.text_type(step_name)] = form_mapping
                elif issubclass(form, (forms.Form, forms.BaseForm, forms.BaseFormSet)):
                    computed_form_list[six.text_type(step_name)] = form
            else:
                # if not, add the form with a zero based counter as unicode
                computed_form_list[six.text_type(i)] = form

        # walk through the new created list of forms
        for form in six.itervalues(computed_form_list):
            form_collection = []
            if isinstance(form, dict):
                form_collection = form.values()
            elif issubclass(form, formsets.BaseFormSet):
                # if the element is based on BaseFormSet (FormSet/ModelFormSet)
                # we need to override the form variable.
                form = form.form
                form_collection = [form]

            for form in form_collection:
                # must test for BaseFormSet again in case form_collection
                # is a dict containing one.
                if issubclass(form, formsets.BaseFormSet):
                    # if the element is based on BaseFormSet (FormSet or
                    # ModelFormSet) we need to override the form variable.
                    form = form.form
                # check if any form contains a FileField, if yes, we need a
                # file_storage added to the wizardview (by subclassing).
                for field in six.itervalues(form.base_fields):
                    if (isinstance(field, forms.FileField) and
                            not hasattr(cls, 'file_storage')):
                        raise NoFileStorageConfigured(
                            "You need to define 'file_storage' in your "
                            "wizard view in order to handle file uploads.")

        return computed_form_list
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号