| 1 |
import string |
|---|
| 2 |
|
|---|
| 3 |
from twisted.application import service, strports |
|---|
| 4 |
from nevow import appserver, inevow, rend, tags as T, loaders |
|---|
| 5 |
|
|---|
| 6 |
class ResultsPage(rend.Page): |
|---|
| 7 |
docFactory = loaders.xmlstr(""" |
|---|
| 8 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |
|---|
| 9 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|---|
| 10 |
<html xmlns:n="http://nevow.com/ns/nevow/0.1"> |
|---|
| 11 |
<body> |
|---|
| 12 |
<h1>Letters of the alphabet</h1> |
|---|
| 13 |
<p>Use the drop down list at the foot of the table to choose the number |
|---|
| 14 |
of items per page.</p> |
|---|
| 15 |
<form action="."> |
|---|
| 16 |
<table border="1"> |
|---|
| 17 |
<thead> |
|---|
| 18 |
<tr> |
|---|
| 19 |
<th>Number</th> |
|---|
| 20 |
<th>Letter</th> |
|---|
| 21 |
</tr> |
|---|
| 22 |
</thead> |
|---|
| 23 |
<tfoot> |
|---|
| 24 |
<tr> |
|---|
| 25 |
<td colspan="2"> |
|---|
| 26 |
Showing <select |
|---|
| 27 |
n:render="itemsPerPageOptions" |
|---|
| 28 |
name="itemsPerPage" |
|---|
| 29 |
onchange="this.form.submit();" /> items per page |
|---|
| 30 |
</td> |
|---|
| 31 |
</tr> |
|---|
| 32 |
</tfoot> |
|---|
| 33 |
<tbody n:render="sequence" n:data="alphabet"> |
|---|
| 34 |
<tr n:pattern="item" n:render="mapping"> |
|---|
| 35 |
<td><n:slot name="index" /></td> |
|---|
| 36 |
<td><n:slot name="letter" /></td> |
|---|
| 37 |
</tr> |
|---|
| 38 |
</tbody> |
|---|
| 39 |
</table> |
|---|
| 40 |
</form> |
|---|
| 41 |
</body> |
|---|
| 42 |
</html> |
|---|
| 43 |
""") |
|---|
| 44 |
itemsPerPageChoices = range(10, 60, 10) |
|---|
| 45 |
|
|---|
| 46 |
def beforeRender(self, ctx): |
|---|
| 47 |
sess = inevow.ISession(ctx) |
|---|
| 48 |
if not hasattr(sess, 'pagerPrefs'): |
|---|
| 49 |
sess.pagerPrefs = dict(itemsPerPage = self.itemsPerPageChoices[0]) |
|---|
| 50 |
|
|---|
| 51 |
try: |
|---|
| 52 |
itemsPerPage = abs(int(ctx.arg('itemsPerPage', 0))) |
|---|
| 53 |
except ValueError: |
|---|
| 54 |
itemsPerPage = 0 |
|---|
| 55 |
|
|---|
| 56 |
if itemsPerPage > 0 and itemsPerPage in self.itemsPerPageChoices: |
|---|
| 57 |
sess.pagerPrefs['itemsPerPage'] = itemsPerPage |
|---|
| 58 |
|
|---|
| 59 |
def render_itemsPerPageOptions(self, ctx, data): |
|---|
| 60 |
options = [T.option(value=i)[i] for i in self.itemsPerPageChoices] |
|---|
| 61 |
default = inevow.ISession(ctx).pagerPrefs.get('itemsPerPage') |
|---|
| 62 |
|
|---|
| 63 |
options[self.itemsPerPageChoices.index(default)](selected="selected") |
|---|
| 64 |
return ctx.tag.clear()[options] |
|---|
| 65 |
|
|---|
| 66 |
def data_alphabet(self, ctx, name): |
|---|
| 67 |
alphabet = string.ascii_lowercase |
|---|
| 68 |
|
|---|
| 69 |
data = [dict(index=i, letter=alphabet[i]) for i in range(len(alphabet))] |
|---|
| 70 |
return data[:inevow.ISession(ctx).pagerPrefs.get('itemsPerPage')] |
|---|
| 71 |
|
|---|
| 72 |
application = service.Application("items per page") |
|---|
| 73 |
strports.service("8080", appserver.NevowSite(ResultsPage())).setServiceParent(application) |
|---|