Merge pull request #1003 from m-sanders/feature/snippet-pagination

Added pagination to snippets.
pull/888/merge
Karl Hobley 2015-02-20 13:14:45 +00:00
commit b2872d2f93
7 zmienionych plików z 149 dodań i 7 usunięć

Wyświetl plik

@ -4,9 +4,10 @@
<div class="nice-padding">
{% if items %}
{% include "wagtailsnippets/snippets/list.html" with choosing=1 %}
{% include "wagtailsnippets/chooser/list.html" with choosing=1 %}
{% else %}
{% url 'wagtailsnippets_create' content_type.app_label content_type.model as wagtailsnippets_create_snippet_url %}
<p>{% blocktrans %}You haven't created any {{ snippet_type_name }} snippets. Why not <a href="{{ wagtailsnippets_create_snippet_url }}" target="_blank">create one now{% endblocktrans %}</p>
{% endif %}
</div>

Wyświetl plik

@ -1,6 +1,39 @@
function(modal) {
$('a.snippet-choice', modal.body).click(function() {
modal.loadUrl(this.href);
var listingUrl = $('#snippet-chooser-list', modal.body).data('url');
console.log(listingUrl);
function ajaxifyLinks (context) {
$('a.snippet-choice', modal.body).click(function() {
modal.loadUrl(this.href);
return false;
});
$('.pagination a', context).click(function() {
var page = this.getAttribute("data-page");
setPage(page);
return false;
});
}
function setPage(page) {
$.ajax({
url: listingUrl,
data: {p: page},
dataType: "html",
success: function(data, status, xhr) {
var response = eval('(' + data + ')');
$(modal.body).html(response.html);
if (response.onload) {
response.onload(self);
}
ajaxifyLinks($('#snippet-chooser-list'));
}
});
return false;
});
}
ajaxifyLinks(modal.body);
}

Wyświetl plik

@ -0,0 +1,29 @@
{% load i18n %}
{% url "wagtailsnippets_choose" content_type.app_label content_type.model as linkurl %}
<table class="listing" id="snippet-chooser-list" data-url="{{ linkurl }}">
<col />
<col />
<col width="16%" />
<thead>
<tr class="table-headers">
<th>{% trans "Title" %}</th>
</tr>
</thead>
<tbody>
{% for snippet in items %}
<tr>
<td class="title">
{% if choosing %}
<h2><a class="snippet-choice" href="{% url 'wagtailsnippets_chosen' content_type.app_label content_type.model snippet.id %}">{{ snippet }}</a></h2>
{% else %}
<h2><a href="{% url 'wagtailsnippets_edit' content_type.app_label content_type.model snippet.id %}">{{ snippet }}</a></h2>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% include "wagtailadmin/shared/pagination_nav.html" with items=items is_ajax=1 %}

Wyświetl plik

@ -21,4 +21,7 @@
</tr>
{% endfor %}
</tbody>
</table>
</table>
{% include "wagtailadmin/shared/pagination_nav.html" with items=items is_ajax=is_ajax linkurl=linkurl %}

Wyświetl plik

@ -1,3 +1,4 @@
from django.http import Http404
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.db import models
@ -43,6 +44,14 @@ class TestSnippetListView(TestCase, WagtailTestUtils):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailsnippets/snippets/type_index.html')
def test_simple_pagination(self):
pages = ['0', '1', '-1', '9999', 'Not a page']
for page in pages:
response = self.get({'p': page})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailsnippets/snippets/type_index.html')
def test_displays_add_button(self):
self.assertContains(self.get(), "Add advert")
@ -219,3 +228,48 @@ class TestUsedBy(TestCase):
def test_snippet_used_by(self):
advert = Advert.objects.get(id=1)
self.assertEqual(type(advert.get_usage()[0]), Page)
class TestSnippetChoose(TestCase, WagtailTestUtils):
fixtures = ['wagtail/tests/fixtures/test.json']
def setUp(self):
self.login()
def get(self, params=None):
return self.client.get(reverse('wagtailsnippets_choose',
args=('tests', 'advert')),
params or {})
def test_simple(self):
response = self.get()
self.assertTemplateUsed(response, 'wagtailsnippets/chooser/choose.html')
def test_simple_pagination(self):
pages = ['0', '1', '-1', '9999', 'Not a page']
for page in pages:
response = self.get({'p': page})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailsnippets/chooser/choose.html')
class TestSnippetChosen(TestCase, WagtailTestUtils):
fixtures = ['wagtail/tests/fixtures/test.json']
def setUp(self):
self.login()
def get(self, pk, params=None):
return self.client.get(reverse('wagtailsnippets_chosen',
args=('tests', 'advert', pk)),
params or {})
def test_choose_a_page(self):
response = self.get(pk=Advert.objects.all()[0].pk)
self.assertTemplateUsed(response, 'wagtailsnippets/chooser/chosen.js')
def test_choose_a_non_existing_page(self):
response = self.get(999999)
self.assertEqual(response.status_code, 404)

Wyświetl plik

@ -1,4 +1,5 @@
import json
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from six import text_type
@ -16,13 +17,23 @@ def choose(request, content_type_app_name, content_type_model_name):
items = model.objects.all()
p = request.GET.get("p", 1)
paginator = Paginator(items, 25)
try:
paginated_items = paginator.page(p)
except PageNotAnInteger:
paginated_items = paginator.page(1)
except EmptyPage:
paginated_items = paginator.page(paginator.num_pages)
return render_modal_workflow(
request,
'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js',
{
'content_type': content_type,
'snippet_type_name': snippet_type_name,
'items': items,
'items': paginated_items,
}
)

Wyświetl plik

@ -94,11 +94,22 @@ def list(request, content_type_app_name, content_type_model_name):
items = model.objects.all()
# Pagination
p = request.GET.get('p', 1)
paginator = Paginator(items, 20)
try:
paginated_items = paginator.page(p)
except PageNotAnInteger:
paginated_items = paginator.page(1)
except EmptyPage:
paginated_items = paginator.page(paginator.num_pages)
return render(request, 'wagtailsnippets/snippets/type_index.html', {
'content_type': content_type,
'snippet_type_name': snippet_type_name,
'snippet_type_name_plural': snippet_type_name_plural,
'items': items,
'items': paginated_items,
})