Esempio n. 1
0
    def _get_actions(self):
        u'''获取所有action'''
        if not self.admin_view.opts:
            actions = self.actions or self.admin_view.form_actions
            return [
                ac for ac in actions if not ac.perm or (
                    ac.perm and self.user.has_perm('auth.' + ac.perm))
            ]

        if self.actions is None:
            return SortedDict()
        if self.model and self.admin_view.grid and self.can_delete_multi:
            actions = [
                self._get_action(action) for action in [DeleteSelectedAction]
            ]
        else:
            actions = []

        for klass in self.admin_view.__class__.mro()[::-1]:
            class_actions = getattr(klass, 'actions', [])
            if not class_actions:
                continue
            actions.extend(
                [self._get_action(action) for action in class_actions])

        actions = filter(None, actions)
        actions = SortedDict([(name, (ac, name, desc, icon))
                              for ac, name, desc, icon in actions])

        return actions
Esempio n. 2
0
    def get_form_list(self):
        if not hasattr(self, '_form_list'):
            init_form_list = SortedDict()

            assert len(
                self.wizard_form_list) > 0, 'at least one form is needed'

            for i, form in enumerate(self.wizard_form_list):
                init_form_list[unicode(form[0])] = form[1]

            self._form_list = init_form_list

        return self._form_list
Esempio n. 3
0
    def get_response(self, __):
        if self.request.GET.get('_format') == 'html':
            self.admin_view.detail_template = 'xadmin/views/quick_detail.html'
            return __()

        form = self.admin_view.form_obj
        layout = form.helper.layout

        results = []

        for p, f in layout.get_field_names():
            result = self.admin_view.get_field_result(f)
            results.append((result.label, result.val))

        return self.render_response(SortedDict(results))
Esempio n. 4
0
    def get_form_list(self):
        """
        This method returns a form_list based on the initial form list but
        checks if there is a condition method/value in the condition_list.
        If an entry exists in the condition list, it will call/read the value
        and respect the result. (True means add the form, False means ignore
        the form)

        The form_list is always generated on the fly because condition methods
        could use data from other (maybe previous forms).
        """
        form_list = SortedDict()
        for form_key, form_class in six.iteritems(self.form_list):
            # try to fetch the value from condition list, by default, the form
            # gets passed to the new list.
            condition = self.condition_dict.get(form_key, True)
            if callable(condition):
                # call the value if needed, passes the current instance.
                condition = condition(self)
            if condition:
                form_list[form_key] = form_class
        return form_list
Esempio n. 5
0
 def get_ordering_field_columns(self):
     """
     从请求参数中得到排序信息 eg o=-create_time.status.-intro.title
     """
     ordering = self._get_default_ordering()
     ordering_fields = SortedDict()
     if defs.ORDER_VAR not in self.params or not self.params[
             defs.ORDER_VAR]:
         for field in ordering:
             if field.startswith('-'):
                 field = field[1:]
                 order_type = 'desc'
             else:
                 order_type = 'asc'
             for attr in self.list_display:
                 if self.get_ordering_field(attr) == field:
                     ordering_fields[field] = order_type
                     break
     else:
         for p in self.params[defs.ORDER_VAR].split('.'):
             __, pfx, field_name = p.rpartition('-')
             ordering_fields[field_name] = 'desc' if pfx == '-' else 'asc'
     return ordering_fields
Esempio n. 6
0
    def get_initkwargs(cls,
                       form_list,
                       initial_dict=None,
                       instance_dict=None,
                       condition_dict=None,
                       *args,
                       **kwargs):
        """
        Creates a dict with all needed parameters for the form wizard instances.

        * `form_list` - is a list of forms. The list entries can be single form
          classes or tuples of (`step_name`, `form_class`). If you pass a list
          of forms, the wizardview will convert the class list to
          (`zero_based_counter`, `form_class`). This is needed to access the
          form for a specific step.
        * `initial_dict` - contains a dictionary of initial data dictionaries.
          The key should be equal to the `step_name` in the `form_list` (or
          the str of the zero based counter - if no step_names added in the
          `form_list`)
        * `instance_dict` - contains a dictionary whose values are model
          instances if the step is based on a ``ModelForm`` and querysets if
          the step is based on a ``ModelFormSet``. The key should be equal to
          the `step_name` in the `form_list`. Same rules as for `initial_dict`
          apply.
        * `condition_dict` - contains a dictionary of boolean values or
          callables. If the value of for a specific `step_name` is callable it
          will be called with the wizardview instance as the only argument.
          If the return value is true, the step's form will be used.
        """
        kwargs.update({
            'initial_dict': initial_dict or {},
            'instance_dict': instance_dict or {},
            'condition_dict': condition_dict or {},
        })
        init_form_list = SortedDict()

        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.
                init_form_list[six.text_type(form[0])] = form[1]
            else:
                # if not, add the form with a zero based counter as unicode
                init_form_list[six.text_type(i)] = form

        # walk through the new created list of forms
        for form in six.itervalues(init_form_list):
            if issubclass(form, formsets.BaseFormSet):
                # if the element is based on BaseFormSet (FormSet/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.")

        # build the kwargs for the wizardview instances
        kwargs['form_list'] = init_form_list
        return kwargs