From efd06fad4bd3b4b47b71881cfba0affad57eabcd Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 6 Aug 2012 14:55:48 -0700 Subject: [PATCH] more grid changes --- edbob/pyramid/grids/alchemy.py | 3 ++ edbob/pyramid/grids/core.py | 4 ++ edbob/pyramid/grids/search.py | 59 +++++++++++-------------- edbob/pyramid/static/js/edbob.js | 12 ++--- edbob/pyramid/templates/grids/grid.mako | 5 ++- edbob/pyramid/views/grids/alchemy.py | 15 ++++--- edbob/pyramid/views/grids/core.py | 2 + 7 files changed, 53 insertions(+), 47 deletions(-) diff --git a/edbob/pyramid/grids/alchemy.py b/edbob/pyramid/grids/alchemy.py index 9b586f8..5bb8842 100644 --- a/edbob/pyramid/grids/alchemy.py +++ b/edbob/pyramid/grids/alchemy.py @@ -56,6 +56,9 @@ class AlchemyGrid(Grid): def __getattr__(self, attr): return getattr(self._formalchemy_grid, attr) + def checkbox(self, row): + return tags.checkbox('check-'+row.uuid) + def column_header(self, field): cls = '' label = field.label() diff --git a/edbob/pyramid/grids/core.py b/edbob/pyramid/grids/core.py index 651cc59..2aeeb25 100644 --- a/edbob/pyramid/grids/core.py +++ b/edbob/pyramid/grids/core.py @@ -56,6 +56,10 @@ class Grid(edbob.Object): super(Grid, self).__init__(**kwargs) self.request = request + def add_column(self, name, label, callback): + self.extra_columns.append( + edbob.Object(name=name, label=label, callback=callback)) + def column_header(self, field): return literal('%s' % (field.name, field.label)) diff --git a/edbob/pyramid/grids/search.py b/edbob/pyramid/grids/search.py index 9477a6d..4e92fcd 100644 --- a/edbob/pyramid/grids/search.py +++ b/edbob/pyramid/grids/search.py @@ -26,7 +26,7 @@ ``edbob.pyramid.grids.search`` -- Grid Search Filters """ -import re +from sqlalchemy import or_ from webhelpers.html import tags from webhelpers.html import literal @@ -143,12 +143,20 @@ def filter_ilike(field): built in for "ILIKE" queries applied to ``field``. """ - return { - 'lk': - lambda q, v: q.filter(field.ilike('%%%s%%' % v)) if v else q, - 'nl': - lambda q, v: q.filter(~field.ilike('%%%s%%' % v)) if v else q, - } + def ilike(query, value): + if value: + query = query.filter(field.ilike('%%%s%%' % value)) + return query + + def not_ilike(query, value): + if value: + query = query.filter(or_( + field == None, + ~field.ilike('%%%s%%' % value), + )) + return query + + return {'lk': ilike, 'nl': not_ilike} def get_filter_config(name, request, filter_map, **kwargs): @@ -235,38 +243,23 @@ def get_search_form(request, filter_map, config): return search -# def filter_query(query, config, join_map={}): -# filter_map = config['filter_map'] -# if config.get('search'): -# search = config['search'].config -# joins = config.setdefault('joins', []) -# include_filter = re.compile(r'^include_filter_(.*)$') -# for key in search: -# m = include_filter.match(key) -# if m and search[key]: -# field = m.group(1) -# if field in join_map and field not in joins: -# query = join_map[field](query) -# joins.append(field) -# value = search.get(field) -# if value: -# f = filter_map[field][search['filter_type_'+field]] -# query = f(query, value) -# return query - - def filter_query(query, config, filter_map, join_map): + """ + Filters ``query`` according to ``config`` and ``filter_map``. ``join_map`` + is used, if necessary, to join additional tables to the base query. The + filtered query is returned. + """ + joins = config.setdefault('joins', []) - include_filter = re.compile(r'^include_filter_(.*)$') for key in config: - m = include_filter.match(key) - if m and config[key]: - field = m.group(1) + if key.startswith('include_filter_') and config[key]: + field = key[15:] if field in join_map and field not in joins: query = join_map[field](query) joins.append(field) value = config.get(field) if value: - f = filter_map[field][config['filter_type_'+field]] - query = f(query, value) + fmap = filter_map[field] + filt = fmap[config['filter_type_'+field]] + query = filt(query, value) return query diff --git a/edbob/pyramid/static/js/edbob.js b/edbob/pyramid/static/js/edbob.js index e9add59..d3a2447 100644 --- a/edbob/pyramid/static/js/edbob.js +++ b/edbob/pyramid/static/js/edbob.js @@ -269,16 +269,16 @@ $(function() { } }); - $('div.grid.checkable table thead th.checkbox input[type=checkbox]').live('click', function() { + $('div.grid table thead th.checkbox input[type=checkbox]').live('click', function() { var checked = $(this).is(':checked'); var table = $(this).parents('table:first'); table.find('tbody tr').each(function() { $(this).find('td.checkbox input[type=checkbox]').attr('checked', checked); - if (checked) { - $(this).addClass('selected'); - } else { - $(this).removeClass('selected'); - } + // if (checked) { + // $(this).addClass('selected'); + // } else { + // $(this).removeClass('selected'); + // } }); }); diff --git a/edbob/pyramid/templates/grids/grid.mako b/edbob/pyramid/templates/grids/grid.mako index 44ee62e..84c12f6 100644 --- a/edbob/pyramid/templates/grids/grid.mako +++ b/edbob/pyramid/templates/grids/grid.mako @@ -2,7 +2,7 @@ - % if checkboxes: + % if grid.checkboxes: % endif % for field in grid.iter_fields(): @@ -38,7 +38,8 @@ % if grid.pager:

- ${grid.pager.pager('showing $first_item thru $last_item of $item_count (page $page of $page_count)')} + showing ${grid.pager.first_item} thru ${grid.pager.last_item} of ${grid.pager.item_count} + (page ${grid.pager.page} of ${grid.pager.page_count})

${h.checkbox('check-all')}