Add support for Product.not_for_sale flag.

This involved a couple of ancillary changes:

* The price field renderer will not display a price for products marked not
  for sale.

* The "grid" class now allows specifying a custom callable to provide
  additional CSS class for table rows.

* The products grid uses this to add a "not-for-sale" class to table rows
  for products which are marked thusly.
This commit is contained in:
Lance Edgar 2014-09-10 19:38:49 -07:00
parent 98f6a7377b
commit dfb5e83c7e
3 changed files with 39 additions and 21 deletions

View file

@ -61,6 +61,9 @@ class Grid(Object):
delete_route_name = None
delete_route_kwargs = None
# Set this to a callable to allow ad-hoc row class additions.
extra_row_class = None
def __init__(self, request, **kwargs):
kwargs.setdefault('fields', OrderedDict())
kwargs.setdefault('column_titles', {})
@ -118,6 +121,20 @@ class Grid(Object):
attrs = self.row_attrs(row, i)
return format_attrs(**attrs)
def row_attrs(self, row, i):
return {'class_': self.get_row_class(row, i)}
def get_row_class(self, row, i):
class_ = self.default_row_class(row, i)
if callable(self.extra_row_class):
extra = self.extra_row_class(row, i)
if extra:
class_ = '{0} {1}'.format(class_, extra)
return class_
def default_row_class(self, row, i):
return 'odd' if i % 2 else 'even'
def iter_fields(self):
return self.fields.itervalues()
@ -130,7 +147,3 @@ class Grid(Object):
def render_field(self, field):
raise NotImplementedError
def row_attrs(self, row, i):
attrs = {'class_': 'odd' if i % 2 else 'even'}
return attrs