~ubuntu-branches/ubuntu/jaunty/python-django/jaunty-updates

« back to all changes in this revision

Viewing changes to tests/modeltests/pagination/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
30. Object pagination
3
 
 
4
 
Django provides a framework for paginating a list of objects in a few lines
5
 
of code. This is often useful for dividing search results or long lists of
6
 
objects into easily readable pages.
7
 
"""
8
 
 
9
 
from django.db import models
10
 
 
11
 
class Article(models.Model):
12
 
    headline = models.CharField(max_length=100, default='Default headline')
13
 
    pub_date = models.DateTimeField()
14
 
 
15
 
    def __unicode__(self):
16
 
        return self.headline
17
 
 
18
 
__test__ = {'API_TESTS':"""
19
 
# Prepare a list of objects for pagination.
20
 
>>> from datetime import datetime
21
 
>>> for x in range(1, 10):
22
 
...     a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
23
 
...     a.save()
24
 
 
25
 
##################
26
 
# Paginator/Page #
27
 
##################
28
 
 
29
 
>>> from django.core.paginator import Paginator
30
 
>>> paginator = Paginator(Article.objects.all(), 5)
31
 
>>> paginator.count
32
 
9
33
 
>>> paginator.num_pages
34
 
2
35
 
>>> paginator.page_range
36
 
[1, 2]
37
 
 
38
 
# Get the first page.
39
 
>>> p = paginator.page(1)
40
 
>>> p
41
 
<Page 1 of 2>
42
 
>>> p.object_list
43
 
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>, <Article: Article 5>]
44
 
>>> p.has_next()
45
 
True
46
 
>>> p.has_previous()
47
 
False
48
 
>>> p.has_other_pages()
49
 
True
50
 
>>> p.next_page_number()
51
 
2
52
 
>>> p.previous_page_number()
53
 
0
54
 
>>> p.start_index()
55
 
1
56
 
>>> p.end_index()
57
 
5
58
 
 
59
 
# Get the second page.
60
 
>>> p = paginator.page(2)
61
 
>>> p
62
 
<Page 2 of 2>
63
 
>>> p.object_list
64
 
[<Article: Article 6>, <Article: Article 7>, <Article: Article 8>, <Article: Article 9>]
65
 
>>> p.has_next()
66
 
False
67
 
>>> p.has_previous()
68
 
True
69
 
>>> p.has_other_pages()
70
 
True
71
 
>>> p.next_page_number()
72
 
3
73
 
>>> p.previous_page_number()
74
 
1
75
 
>>> p.start_index()
76
 
6
77
 
>>> p.end_index()
78
 
9
79
 
 
80
 
# Empty pages raise EmptyPage.
81
 
>>> paginator.page(0)
82
 
Traceback (most recent call last):
83
 
...
84
 
EmptyPage: ...
85
 
>>> paginator.page(3)
86
 
Traceback (most recent call last):
87
 
...
88
 
EmptyPage: ...
89
 
 
90
 
# Empty paginators with allow_empty_first_page=True.
91
 
>>> paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True)
92
 
>>> paginator.count
93
 
0
94
 
>>> paginator.num_pages
95
 
1
96
 
>>> paginator.page_range
97
 
[1]
98
 
 
99
 
# Empty paginators with allow_empty_first_page=False.
100
 
>>> paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False)
101
 
>>> paginator.count
102
 
0
103
 
>>> paginator.num_pages
104
 
0
105
 
>>> paginator.page_range
106
 
[]
107
 
 
108
 
# Paginators work with regular lists/tuples, too -- not just with QuerySets.
109
 
>>> paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
110
 
>>> paginator.count
111
 
9
112
 
>>> paginator.num_pages
113
 
2
114
 
>>> paginator.page_range
115
 
[1, 2]
116
 
 
117
 
# Get the first page.
118
 
>>> p = paginator.page(1)
119
 
>>> p
120
 
<Page 1 of 2>
121
 
>>> p.object_list
122
 
[1, 2, 3, 4, 5]
123
 
>>> p.has_next()
124
 
True
125
 
>>> p.has_previous()
126
 
False
127
 
>>> p.has_other_pages()
128
 
True
129
 
>>> p.next_page_number()
130
 
2
131
 
>>> p.previous_page_number()
132
 
0
133
 
>>> p.start_index()
134
 
1
135
 
>>> p.end_index()
136
 
5
137
 
 
138
 
# Paginator can be passed other objects with a count() method.
139
 
>>> class CountContainer:
140
 
...     def count(self):
141
 
...         return 42
142
 
>>> paginator = Paginator(CountContainer(), 10)
143
 
>>> paginator.count
144
 
42
145
 
>>> paginator.num_pages
146
 
5
147
 
>>> paginator.page_range
148
 
[1, 2, 3, 4, 5]
149
 
 
150
 
# Paginator can be passed other objects that implement __len__.
151
 
>>> class LenContainer:
152
 
...     def __len__(self):
153
 
...         return 42
154
 
>>> paginator = Paginator(LenContainer(), 10)
155
 
>>> paginator.count
156
 
42
157
 
>>> paginator.num_pages
158
 
5
159
 
>>> paginator.page_range
160
 
[1, 2, 3, 4, 5]
161
 
 
162
 
 
163
 
##################
164
 
# Orphan support #
165
 
##################
166
 
 
167
 
# Add a few more records to test out the orphans feature.
168
 
>>> for x in range(10, 13):
169
 
...     Article(headline="Article %s" % x, pub_date=datetime(2006, 10, 6)).save()
170
 
 
171
 
# With orphans set to 3 and 10 items per page, we should get all 12 items on a single page.
172
 
>>> paginator = Paginator(Article.objects.all(), 10, orphans=3)
173
 
>>> paginator.num_pages
174
 
1
175
 
 
176
 
# With orphans only set to 1, we should get two pages.
177
 
>>> paginator = Paginator(Article.objects.all(), 10, orphans=1)
178
 
>>> paginator.num_pages
179
 
2
180
 
"""}