| 1 |
# Copyright 2005 Gentoo Foundation
|
| 2 |
# Distributed under the terms of the GNU General Public License v2
|
| 3 |
#
|
| 4 |
|
| 5 |
"""Prints an HTML table with sorting and paging functionality."""
|
| 6 |
|
| 7 |
__revision__ = '$Id$'
|
| 8 |
__author__ = "Scott Hadfield <hadfield@gentoo.org>"
|
| 9 |
__modulename__ = 'table_listing'
|
| 10 |
|
| 11 |
from copy import deepcopy
|
| 12 |
import os
|
| 13 |
|
| 14 |
from glsr.setup import config
|
| 15 |
from glsr.site.basedomain import BaseDomain
|
| 16 |
|
| 17 |
# FIXME: This variable should be user configurable, not hardcoded.
|
| 18 |
items_per_page = 49
|
| 19 |
|
| 20 |
class TableListing(BaseDomain):
|
| 21 |
"""This class is a skeleton to be inherited by other page modules.
|
| 22 |
|
| 23 |
It's main goal is to add the '_make_table' functionality which prints
|
| 24 |
out a table based on a set of fields and data list.
|
| 25 |
"""
|
| 26 |
|
| 27 |
def __init__(self, harm):
|
| 28 |
|
| 29 |
BaseDomain.__init__(self, harm)
|
| 30 |
self._domain = ""
|
| 31 |
self._page = ""
|
| 32 |
self._list_title = ""
|
| 33 |
self._fields = []
|
| 34 |
self._hidden_fields = []
|
| 35 |
self._loop_data = []
|
| 36 |
|
| 37 |
def init(self, page = "", list_title = "Default Title", fields = None):
|
| 38 |
"""Initialize the extra variables used in this class."""
|
| 39 |
|
| 40 |
self._page = page
|
| 41 |
self._list_title = list_title
|
| 42 |
self._fields = fields
|
| 43 |
if self._fields is None:
|
| 44 |
self._fields = []
|
| 45 |
|
| 46 |
def _generate_link(self, link_info, data):
|
| 47 |
if type(link_info) not in (list, tuple):
|
| 48 |
return link_info
|
| 49 |
|
| 50 |
link = link_info[0]
|
| 51 |
for value in link_info[1:]:
|
| 52 |
link = link % (data[value])
|
| 53 |
|
| 54 |
return link
|
| 55 |
|
| 56 |
def _make_table(self):
|
| 57 |
"""The core member function.
|
| 58 |
|
| 59 |
Sets all variables necessary to display a nice neat sorted table.
|
| 60 |
"""
|
| 61 |
|
| 62 |
start = self._harm.request.queries.get("start", "0")
|
| 63 |
|
| 64 |
if not start.isdigit():
|
| 65 |
start = "0"
|
| 66 |
start = int(start)
|
| 67 |
|
| 68 |
loop_data = self._sort()
|
| 69 |
page_loop, current_page = self._pages(loop_data, start)
|
| 70 |
loop_data = loop_data[start:start + items_per_page]
|
| 71 |
|
| 72 |
# This is a bit of a hack, necessary to get the field data with the
|
| 73 |
# values to output in a single loop.
|
| 74 |
for record in loop_data:
|
| 75 |
fields = deepcopy(self._fields)
|
| 76 |
for field in fields:
|
| 77 |
field["value"] = record[field["name"]]
|
| 78 |
if field.has_key("link"):
|
| 79 |
field["link"] = self._generate_link(field["link"], record)
|
| 80 |
|
| 81 |
record.update({"fields": fields})
|
| 82 |
|
| 83 |
self._set_template(os.path.join(config.template_loc,
|
| 84 |
'table_listing.tpl'))
|
| 85 |
|
| 86 |
extra_fields = "".join(["&%s=%s" % (field["name"], field["value"])
|
| 87 |
for field in self._hidden_fields])
|
| 88 |
|
| 89 |
self._template.param("GLSR_URL", config.url)
|
| 90 |
self._template.param("LIST_TITLE", self._list_title)
|
| 91 |
self._template.param("PAGE_NUM", current_page)
|
| 92 |
self._template.param("DOMAIN", self._domain)
|
| 93 |
self._template.param("PAGE", self._page)
|
| 94 |
self._template.param("START", start)
|
| 95 |
self._template.param("NEXT_START", start + items_per_page)
|
| 96 |
self._template.param("PAGE_LOOP", page_loop)
|
| 97 |
self._template.param("EXTRA_FIELDS", extra_fields)
|
| 98 |
self._template.param("FIELDS", self._fields)
|
| 99 |
self._template.param("LOOP_DATA", loop_data)
|
| 100 |
|
| 101 |
|
| 102 |
def _pages(self, list_in, start):
|
| 103 |
"""Calculate the number of pages the results will be displayed on."""
|
| 104 |
|
| 105 |
page_list = [{"page": 1, "start": 0}]
|
| 106 |
|
| 107 |
for i in range(items_per_page, len(list_in), items_per_page):
|
| 108 |
page_list.append({"page": i/items_per_page + 1, "start": i})
|
| 109 |
|
| 110 |
current_page = start/items_per_page + 1
|
| 111 |
return page_list, current_page
|
| 112 |
|
| 113 |
|
| 114 |
def _sort(self):
|
| 115 |
"""Sorts the data list
|
| 116 |
|
| 117 |
The list is sorted by using the sortby and order form parameters
|
| 118 |
if the 'sort' submit button was pressed.
|
| 119 |
"""
|
| 120 |
|
| 121 |
rows = list(self._loop_data)
|
| 122 |
|
| 123 |
ascending = 1
|
| 124 |
|
| 125 |
def cmpfunc(val1, val2):
|
| 126 |
"""Simple sort function."""
|
| 127 |
if val1[sortby] > val2[sortby]:
|
| 128 |
return 1 * ascending
|
| 129 |
elif val1[sortby] < val2[sortby]:
|
| 130 |
return -1 * ascending
|
| 131 |
return 0
|
| 132 |
|
| 133 |
sortby = self._harm.request.queries.get("sortby",
|
| 134 |
self._fields[0]["name"])
|
| 135 |
order = self._harm.request.queries.get("order", "a")
|
| 136 |
|
| 137 |
if order == 'd':
|
| 138 |
ascending = -1
|
| 139 |
|
| 140 |
rows.sort(cmpfunc)
|
| 141 |
|
| 142 |
self._template.param("SORTBY", sortby)
|
| 143 |
self._template.param("ORDER", order)
|
| 144 |
|
| 145 |
return rows
|