Просмотр исходного кода

Admin mass actions are dicts now and support icons

Rafał Pitoń 11 лет назад
Родитель
Сommit
6389e522c1

+ 4 - 4
docs/developers/admin_actions.rst

@@ -55,7 +55,7 @@ Base class for lists if items. Supports following properties:
 * **items_per_page** - integer controlling number of items displayed on single page. Defaults to 0 which means no pagination
 * **SearchForm** - Form type used to construct form for filtering this list. Either this field or ``get_search_form`` method is required to make list searchable.
 * **ordering** - list of supported sorting methods. List of tuples. Each tuple should countain two items: name of ordering method (eg. "Usernames, descending") and ``order_by`` argument ("-username"). Defaults to none which means queryset will not be ordered. If contains only one element, queryset is ordered, but option for changing ordering method is not displayed.
-* **mass_actions** - tuple of tuples defining list's mass actions. Each tuple should define at least two items: name of ``ListView`` attribute to be called for action, and its button label. Optional third item in tuple will be used for Java Script confirm dialog.
+* **mass_actions** - list of dicts defining list's mass actions. Each dict should have ``action`` key that will be used to identify method to call, ``name`` for displayed name, ``icon`` for icon and optional ``confirmation`` message.
 * **selection_label** - Label displayed on mass action button if there are items selected. ``0`` will be replaced with number of selected items automatically.
 * **empty_selection_label** - Label displayed on mass action button if there are no items selected.
 
@@ -88,12 +88,12 @@ If you decide to make your list searchable, remember that your Form must meet fo
 * Must not define fields that use models for values.
 
 
-If you add custom mass action to view, besides adding new entry to ``mass_actions`` tuple, you have to define custom method following this definition:
+If you add custom mass action to view, besides adding new entry to ``mass_actions`` list, you have to define custom method following this definition:
 
 
-.. function:: action_NAME(self, request, items)
+.. function:: action_ACTION(self, request, items)
 
-``NAME`` will be replaced with action name. Request is ``HttpRequest`` instance used to call view and ``items`` is queryset with items selected for this action. This method should nothing or ``HttpResponse``. If you need to, you can raise ``MassActionError`` with error message as its first argument to interrupt mass action handler.
+``ACTION`` will be replaced with action dict ``action`` value. Request is ``HttpRequest`` instance used to call view and ``items`` is queryset with items selected for this action. This method should nothing or ``HttpResponse``. If you need to, you can raise ``MassActionError`` with error message as its first argument to interrupt mass action handler.
 
 
 FormView

+ 8 - 14
misago/admin/views/generic/list.py

@@ -40,11 +40,15 @@ class ListView(AdminView):
     empty_selection_label = _('Select items')
 
     @classmethod
-    def add_mass_action(cls, action, name, prompt=None):
+    def add_mass_action(cls, action, name, icon, confirmation=None):
         if not cls.mass_actions:
             cls.mass_actions = []
 
-        cls.extra_actions.append((action, name, prompt))
+        cls.extra_actions.append({
+            'action': action,
+            'name': name,
+            'icon': icon,
+            'confirmation': confirmation})
 
     @classmethod
     def add_item_action(cls, name, icon, link, style=None):
@@ -214,21 +218,11 @@ class ListView(AdminView):
 
     def select_mass_action(self, action):
         for definition in self.mass_actions:
-            if definition[0] == action:
+            if definition['action'] == action:
                 return action
         else:
             raise MassActionError(_("Action is not allowed."))
 
-    def mass_actions_as_dicts(self):
-        dicts = []
-        for definition in self.mass_actions or []:
-            dicts.append({
-                'action': definition[0],
-                'name': definition[1],
-                'prompt': definition[2] if len(definition) == 3 else None,
-                })
-        return dicts
-
     """
     Querystrings builder
     """
@@ -264,7 +258,7 @@ class ListView(AdminView):
     Dispatch response
     """
     def dispatch(self, request, *args, **kwargs):
-        mass_actions_list = self.mass_actions_as_dicts()
+        mass_actions_list = self.mass_actions or []
         extra_actions_list = self.extra_actions or []
 
         refresh_querystring = False

+ 3 - 3
misago/static/misago/admin/js/misago-tables.js

@@ -4,9 +4,9 @@ function tableMassActions(label_none, label_selected) {
   var $form = $controller.parents('form');
 
   $form.find('.dropdown-menu button').click(function() {
-    if ($(this).data('prompt')) {
-      var prompt = confirm($(this).data('prompt'));
-      return prompt;
+    if ($(this).data('confirmation')) {
+      var confirmation = confirm($(this).data('confirmation'));
+      return confirmation;
     } else {
       return true;
     }

+ 2 - 1
misago/templates/misago/admin/generic/list.html

@@ -79,7 +79,8 @@
       <ul class="dropdown-menu" role="menu">
         {% for action in mass_actions %}
         <li>
-          <button type="submit" name="action" value="{{ action.action }}" {% if action.prompt %}data-prompt="{{ action.prompt }}"{% endif %}>
+          <button type="submit" name="action" value="{{ action.action }}" {% if action.confirmation %}data-confirmation="{{ action.confirmation }}"{% endif %}>
+            <span class="{{ action.icon }}"></span>
             {{ action.name }}
           </button>
         </li>

+ 6 - 5
misago/users/views/admin/bans.py

@@ -32,11 +32,12 @@ class BansList(BanAdmin, generic.ListView):
     selection_label = _('With bans: 0')
     empty_selection_label = _('Select bans')
     mass_actions = (
-        (
-            'delete',
-            _('Remove bans'),
-            _('Are you sure you want to remove those bans?')
-        ),
+        {
+            'action': 'delete',
+            'icon': 'fa fa-times',
+            'name': _('Remove bans'),
+            'confirmation': _('Are you sure you want to remove those bans?')
+        },
     )
 
     def action_delete(self, request, items):