Browse Source

Paginate generic admin lists.

Rafał Pitoń 11 years ago
parent
commit
840bd65682

+ 34 - 3
misago/admin/views/generic.py

@@ -1,6 +1,8 @@
 from django.contrib import messages
+from django.core.paginator import Paginator, EmptyPage
 from django.shortcuts import redirect
 from django.views.generic import View
+from misago.core.exceptions import ExplicitFirstPage
 from misago.admin import site
 from misago.admin.views import render
 
@@ -47,6 +49,9 @@ class AdminView(View):
 
     def render(self, request, context=None):
         context = context or {}
+
+        context['root_link'] = self.root_link
+
         return render(request, self.final_template(), context)
 
 
@@ -55,10 +60,36 @@ class ItemsList(AdminView):
 
     items_per_page = 0
 
-    def dispatch(self, request, *args, **kwargs):
-        context = {}
+    def get_queryset(self):
+        return self.get_model().objects.all()
 
-        context['items'] = self.get_model().objects.all()
+    def paginate_items(self, context, page):
+        try:
+            page = int(page)
+            if page == 1:
+                raise ExplicitFirstPage()
+            else:
+                page = 1
+        except ValueError:
+            page_no = 1
+
+        context['paginator'] = Paginator(context['items'],
+                                         self.items_per_page,
+                                         allow_empty_first_page=True)
+        context['page'] = context['paginator'].page(page)
+
+    def filter_items(self, request, context):
+        pass
+
+    def dispatch(self, request, *args, **kwargs):
+        context = {
+            'items': self.get_queryset(),
+            'paginator': None,
+            'page': None,
+        }
+
+        if self.items_per_page:
+            self.paginate_items(context, kwargs.get('page', 0))
 
         return self.render(request, context)
 

+ 15 - 0
misago/templates/misago/admin/generic/list.html

@@ -1,4 +1,5 @@
 {% extends "misago/admin/generic/base.html" %}
+{% load i18n %}
 
 
 {% block title %}
@@ -7,6 +8,14 @@
 
 
 {% block view %}
+<div class="table-actions">
+
+  {% if paginator %}
+  {% include "misago/admin/generic/paginator.html" %}
+  {% endif%}
+
+</div><!-- /.table-actions -->
+
 <div class="table-panel">
   <table class="table">
     <tr>
@@ -76,4 +85,10 @@
     {% endfor %}
   </table>
 </div><!-- /.table-panel -->
+
+{% if paginator %}
+<div class="table-actions">
+  {% include "misago/admin/generic/paginator.html" %}
+</div><!-- /.table-actions -->
+{% endif %}
 {% endblock view %}

+ 37 - 0
misago/templates/misago/admin/generic/paginator.html

@@ -0,0 +1,37 @@
+{% load i18n%}
+
+<ul class="pager pull-left">
+  <li class="page">
+    {% blocktrans with page=page.number pages=paginator.num_pages %}
+    Page {{ page}} of {{ pages }}
+    {% endblocktrans %}
+  </li>
+  {% if page.has_previous %}
+    <li>
+      <a href="{% url root_link %}" class="tooltip-top" title="{% trans "Go to first page" %}">
+        {% trans "First" %}
+      </a>
+    </li>
+    {% if page.number > 2 %}
+    <li>
+      <a href="{% url root_link page=page.previous_page_number %}" class="tooltip-top" title="{% trans "Go to previous page" %}">
+        <span class="glyphicon glyphicon-chevron-left"></span>
+      </a>
+    </li>
+    {% endif %}
+  {% endif %}
+  {% if page.has_next %}
+    {% if page.next_page_number < paginator.num_pages %}
+    <li>
+      <a href="{% url root_link page=page.next_page_number %}" class="tooltip-top" title="{% trans "Go to next page" %}">
+        <span class="glyphicon glyphicon-chevron-right"></span>
+      </a>
+    </li>
+    {% endif %}
+    <li>
+      <a href="{% url root_link page=paginator.num_pages %}" class="tooltip-top" title="{% trans "Go to last page" %}">
+        {% trans "Last" %}
+      </a>
+    </li>
+  {% endif %}
+</ul>

+ 1 - 1
misago/users/views/useradmin.py

@@ -10,4 +10,4 @@ class UserAdmin(generic.AdminBaseMixin):
 
 
 class UsersList(UserAdmin, generic.ItemsList):
-    pass
+    items_per_page = 20