1.1 --- a/ImprovedTableParser.py Sun Feb 19 20:26:25 2012 +0100
1.2 +++ b/ImprovedTableParser.py Sun Feb 19 21:07:13 2012 +0100
1.3 @@ -514,35 +514,62 @@
1.4
1.5 table_attrs, table = parse(text)
1.6
1.7 - # Override any region arguments with request parameters.
1.8 + # Define the table name and an anchor attribute.
1.9
1.10 table_name = attrs.get("name")
1.11 if table_name:
1.12 table_attrs["tableid"] = table_name
1.13 + else:
1.14 + table_name = table_attrs.get("tableid")
1.15
1.16 - # Get sorting criteria from the region and the request.
1.17 + # Get the underlying column types.
1.18 +
1.19 + column_types = get_column_types(get_sort_columns(attrs.get("columntypes", "")))
1.20 +
1.21 + # Get sorting criteria from the region.
1.22 +
1.23 + region_sortcolumns = attrs.get("sortcolumns", "")
1.24 +
1.25 + # Update the column types from the sort criteria.
1.26 +
1.27 + column_types.update(get_column_types(get_sort_columns(region_sortcolumns)))
1.28
1.29 - region_sortcolumns = attrs.get("sortcolumns")
1.30 - sortcolumns = table_name and getQualifiedParameter(request, table_name, "sortcolumns") or region_sortcolumns
1.31 + # Determine the applicable sort criteria using the request.
1.32 +
1.33 + if table_name:
1.34 + sortcolumns = getQualifiedParameter(request, table_name, "sortcolumns")
1.35 + else:
1.36 + sortcolumns = None
1.37 +
1.38 + if sortcolumns is None:
1.39 + sortcolumns = region_sortcolumns
1.40 +
1.41 + # Define the final sort criteria.
1.42 +
1.43 + sort_columns = get_sort_columns(sortcolumns)
1.44 +
1.45 + # Update the column types from the final sort criteria.
1.46 +
1.47 + column_types.update(get_column_types(sort_columns))
1.48
1.49 # Sort the rows according to the values in each of the specified columns.
1.50
1.51 data_start = int(table_name and getQualifiedParameter(request, table_name, "headers") or attrs.get("headers", "1"))
1.52
1.53 - if sortcolumns:
1.54 + if sort_columns:
1.55 headers = table[:data_start]
1.56 data = table[data_start:]
1.57
1.58 - # Get the sort columns using Unix sort-like notation.
1.59 -
1.60 - sort_columns = get_sort_columns(sortcolumns)
1.61 - region_sort_columns = get_sort_columns(region_sortcolumns)
1.62 + # Perform the sort and reconstruct the table.
1.63
1.64 sorter = Sorter(sort_columns, request)
1.65 data.sort(cmp=sorter)
1.66 + table = headers + data
1.67
1.68 - table = headers + data
1.69 - column_types = get_column_types(region_sort_columns)
1.70 + # Permit sorting because column types for sorting may have been defined.
1.71 +
1.72 + else:
1.73 + sort_columns = []
1.74
1.75 # Write the table.
1.76
1.77 @@ -551,7 +578,7 @@
1.78
1.79 for rownumber, (row_attrs, columns) in enumerate(table):
1.80 write(fmt.table_row(1, row_attrs))
1.81 - sortable = sortcolumns and rownumber == data_start - 1
1.82 + sortable = column_types and rownumber == data_start - 1
1.83
1.84 for columnnumber, (column_attrs, column_text) in enumerate(columns):
1.85 write(fmt.table_cell(1, column_attrs))