Browse Source

Updated docs for admin generics, cleaned search users form.

Rafał Pitoń 11 years ago
parent
commit
429c6a6c5a
2 changed files with 31 additions and 12 deletions
  1. 12 0
      docs/developers/admin_actions.rst
  2. 19 12
      misago/users/forms/admin.py

+ 12 - 0
docs/developers/admin_actions.rst

@@ -53,6 +53,7 @@ Base class for lists if items. Supports following properties:
 
 * **template** - name of template file located in ``templates_dir`` used to render this view. Defaults to ``list.html``
 * **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.
 
 In addition to this, ListView defines following methods that you may be interested in overloading:
@@ -68,6 +69,17 @@ This function is expected to return queryset of items that will be displayed. If
 Class method that allows you to add custom links to item actions. Link should be a string with link name, not complete link. It should also accept same kwargs as other item actions links.
 
 
+.. function:: get_search_form(self, request):
+
+This function is used to get search form class that will be used to construct form for searching list items.
+
+If you decide to make your list searchable, remember that your Form must meet following requirements:
+
+* Must define ``filter_queryset(self, search_criteria, queryset)`` method that will be passed unfiltered queryset, which it should modify using filter/exclude clauses and data from search_criteria.
+* Must return queryset.
+* Must not define fields that use models for values.
+
+
 FormView
 --------
 

+ 19 - 12
misago/users/forms/admin.py

@@ -99,6 +99,11 @@ def UserFormFactory(FormType, instance):
     return type('UserFormFinal', (FormType,), extra_fields)
 
 
+import warnings
+warnings.warn("Admin search inactive users not implemented yet.",
+              FutureWarning)
+
+
 def StaffFlagUserFormFactory(FormType, instance, add_staff_field):
     FormType = UserFormFactory(FormType, instance)
 
@@ -139,28 +144,30 @@ class SearchUsersFormBase(forms.Form):
     inactive = forms.YesNoSwitch(label=_("Inactive only"))
     is_staff = forms.YesNoSwitch(label=_("Is administrator"))
 
-    def filter_queryset(self, cleaned_data, queryset):
-        if cleaned_data.get('username'):
+    def filter_queryset(self, search_criteria, queryset):
+        criteria = search_criteria
+        if criteria.get('username'):
             queryset = queryset.filter(
-                username_slug__startswith=cleaned_data.get('username').lower())
+                username_slug__startswith=criteria.get('username').lower())
 
-        if cleaned_data.get('email'):
+        if criteria.get('email'):
             queryset = queryset.filter(
-                email__istartswith=cleaned_data.get('email'))
+                email__istartswith=criteria.get('email'))
 
-        if cleaned_data.get('rank'):
+        if criteria.get('rank'):
             queryset = queryset.filter(
-                rank_id=cleaned_data.get('rank'))
+                rank_id=criteria.get('rank'))
 
-        if cleaned_data.get('role'):
+        if criteria.get('role'):
             queryset = queryset.filter(
-                roles__id=cleaned_data.get('role'))
+                roles__id=criteria.get('role'))
 
-        if cleaned_data.get('inactive'):
+        if criteria.get('inactive'):
             pass
 
-        if cleaned_data.get('is_staff'):
-            queryset = queryset.filter(is_staff=True)
+
+        if criteria.get('is_staff'):
+            queryset = criteria.filter(is_staff=True)
 
         return queryset