From a5080b113d974c05b59c04ae28dde9d6db2f6c02 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 13 Feb 2016 13:00:55 -0600 Subject: [PATCH] Various tweaks to products grid filters etc. Trying to troubleshoot some things...think we're good now. Some of the changes here were made and then discovered not to be necessary yet, but left in place since they're probably good ideas anyway. --- tailbone/newgrids/alchemy.py | 1 + tailbone/newgrids/core.py | 2 +- tailbone/newgrids/filters.py | 4 +++- tailbone/views/families.py | 1 + tailbone/views/products.py | 29 ++++++++++++++++++++++++----- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/tailbone/newgrids/alchemy.py b/tailbone/newgrids/alchemy.py index 80f727c1..0e31cb4e 100644 --- a/tailbone/newgrids/alchemy.py +++ b/tailbone/newgrids/alchemy.py @@ -104,6 +104,7 @@ class AlchemyGrid(Grid): factory = filters.AlchemyDateFilter elif isinstance(column.type, GPCType): factory = filters.AlchemyGPCFilter + factory = kwargs.pop('factory', factory) return factory(key, column=column, **kwargs) def iter_filters(self): diff --git a/tailbone/newgrids/core.py b/tailbone/newgrids/core.py index 38dc019a..511011fd 100644 --- a/tailbone/newgrids/core.py +++ b/tailbone/newgrids/core.py @@ -55,7 +55,7 @@ class Grid(object): self.data = data self.main_actions = main_actions self.more_actions = more_actions - self.joiners = joiners + self.joiners = joiners or {} # use new/different empty dict for each instance # Set extra attributes first, in case other init logic depends on any # of them (i.e. in subclasses). diff --git a/tailbone/newgrids/filters.py b/tailbone/newgrids/filters.py index 2916abab..06fd40a2 100644 --- a/tailbone/newgrids/filters.py +++ b/tailbone/newgrids/filters.py @@ -128,7 +128,7 @@ class GridFilter(object): value_renderer_factory = DefaultValueRenderer def __init__(self, key, label=None, verbs=None, value_renderer=None, - default_active=False, default_verb=None, default_value=None): + default_active=False, default_verb=None, default_value=None, **kwargs): self.key = key self.label = label or prettify(key) self.verbs = verbs or self.get_default_verbs() @@ -136,6 +136,8 @@ class GridFilter(object): self.default_active = default_active self.default_verb = default_verb self.default_value = default_value + for key, value in kwargs.iteritems(): + setattr(self, key, value) def __repr__(self): return "GridFilter({0})".format(repr(self.key)) diff --git a/tailbone/views/families.py b/tailbone/views/families.py index 5e1ceaa0..92a6103c 100644 --- a/tailbone/views/families.py +++ b/tailbone/views/families.py @@ -38,6 +38,7 @@ class FamiliesView(MasterView): model_class = model.Family model_title_plural = "Families" route_prefix = 'families' + grid_key = 'families' def configure_grid(self, g): g.filters['name'].default_active = True diff --git a/tailbone/views/products.py b/tailbone/views/products.py index 72f3378c..21b95b1d 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -43,15 +43,33 @@ from pyramid import httpexceptions from pyramid.renderers import render_to_response from webhelpers.html import tags -from tailbone import forms +from tailbone import forms, newgrids as grids from tailbone.db import Session from tailbone.views import MasterView, SearchableAlchemyGridView, AutocompleteView from tailbone.views.continuum import VersionView, version_defaults from tailbone.forms.renderers import products as products_forms -from tailbone.newgrids import GridAction from tailbone.progress import SessionProgress +# TODO: For a moment I thought this was going to be necessary, but now I think +# not. Leaving it around for a bit just in case... + +# class VendorAnyFilter(grids.filters.AlchemyStringFilter): +# """ +# Custom filter for "vendor (any)" so we can avoid joining on that unless we +# really have to. This is because it seems to throw off the number of +# records which are showed in the result set, when this filter is included in +# the active set but no criteria is specified. +# """ + +# def filter(self, query, **kwargs): +# original = query +# query = super(VendorAnyFilter, self).filter(query, **kwargs) +# if query is not original: +# query = self.joiner(query) +# return query + + class ProductsView(MasterView): """ Master view for the Product class. @@ -122,9 +140,9 @@ class ProductsView(MasterView): model.ProductPrice.uuid == model.Product.regular_price_uuid) g.joiners['current_price'] = lambda q: q.outerjoin(model.ProductPrice, model.ProductPrice.uuid == model.Product.current_price_uuid) + g.joiners['code'] = lambda q: q.outerjoin(model.ProductCode) g.joiners['vendor'] = join_vendor g.joiners['vendor_any'] = join_vendor_any - g.joiners['code'] = lambda q: q.outerjoin(model.ProductCode) g.sorters['brand'] = g.make_sorter(model.Brand.name) g.sorters['department'] = g.make_sorter(model.Department.name) @@ -143,9 +161,10 @@ class ProductsView(MasterView): default_active=True, default_verb='contains') g.filters['subdepartment'] = g.make_filter('subdepartment', model.Subdepartment.name) g.filters['report_code'] = g.make_filter('report_code', model.ReportCode.name) + g.filters['code'] = g.make_filter('code', model.ProductCode.code) g.filters['vendor'] = g.make_filter('vendor', model.Vendor.name, label="Vendor (preferred)") g.filters['vendor_any'] = g.make_filter('vendor_any', self.VendorAny.name, label="Vendor (any)") - g.filters['code'] = g.make_filter('code', model.ProductCode.code) + # factory=VendorAnyFilter, joiner=join_vendor_any) g.default_sortkey = 'description' @@ -167,7 +186,7 @@ class ProductsView(MasterView): # TODO: need to check for 'print labels' permission here also if self.print_labels: - g.more_actions.append(GridAction('print_label', icon='print')) + g.more_actions.append(grids.GridAction('print_label', icon='print')) def template_kwargs_index(self, **kwargs): if self.print_labels: