Refactor "departments by vendor" grid to use newer-style

This commit is contained in:
Lance Edgar 2017-07-06 14:34:35 -05:00
parent 3b97757d7f
commit 0befc46070
3 changed files with 48 additions and 32 deletions

View file

@ -325,6 +325,7 @@ $(function() {
}); });
/* /*
* TODO: this should be deprecated; for old grids only?
* Add "check all" functionality to tables with checkboxes. * Add "check all" functionality to tables with checkboxes.
*/ */
$('body').on('click', '.grid thead th.checkbox input[type="checkbox"]', function() { $('body').on('click', '.grid thead th.checkbox input[type="checkbox"]', function() {

View file

@ -28,7 +28,7 @@ from __future__ import unicode_literals, absolute_import
from rattail.db import model from rattail.db import model
from tailbone.views import MasterView, AutocompleteView, AlchemyGridView from tailbone.views import MasterView, AutocompleteView
from tailbone.newgrids import AlchemyGrid, GridAction from tailbone.newgrids import AlchemyGrid, GridAction
@ -79,31 +79,44 @@ class DepartmentsView(MasterView):
kwargs['employees'] = None kwargs['employees'] = None
return kwargs return kwargs
def list_by_vendor(self):
"""
View list of departments by vendor
"""
data = self.Session.query(model.Department)\
.outerjoin(model.Product)\
.join(model.ProductCost)\
.join(model.Vendor)\
.filter(model.Vendor.uuid == self.request.params['uuid'])\
.distinct()\
.order_by(model.Department.name)
class DepartmentsByVendorGrid(AlchemyGridView): def configure(g):
g.configure(include=[
mapped_class = model.Department
config_prefix = 'departments.by_vendor'
checkboxes = True
partial_only = True
def query(self):
return self.make_query()\
.outerjoin(model.Product)\
.join(model.ProductCost)\
.join(model.Vendor)\
.filter(model.Vendor.uuid == self.request.params['uuid'])\
.distinct()\
.order_by(model.Department.name)
def grid(self):
g = self.make_grid()
g.configure(
include=[
g.name, g.name,
], ], readonly=True)
readonly=True)
return g def row_attrs(row, i):
return {'data-uuid': row.uuid}
grid = self.make_grid(data=data, sortable=False, filterable=False, pageable=False,
configure=configure, width=None, checkboxes=True,
row_attrs=row_attrs, main_actions=[], more_actions=[])
self.request.response.content_type = b'text/html'
self.request.response.text = grid.render_grid()
return self.request.response
@classmethod
def defaults(cls, config):
route_prefix = cls.get_route_prefix()
url_prefix = cls.get_url_prefix()
permission_prefix = cls.get_permission_prefix()
config.add_route('{}.by_vendor'.format(route_prefix), '{}/by-vendor'.format(url_prefix))
config.add_view(cls, attr='list_by_vendor', route_name='{}.by_vendor'.format(route_prefix),
permission='{}.list'.format(permission_prefix))
cls._defaults(config)
class DepartmentsAutocomplete(AutocompleteView): class DepartmentsAutocomplete(AutocompleteView):
@ -119,9 +132,4 @@ def includeme(config):
config.add_view(DepartmentsAutocomplete, route_name='departments.autocomplete', config.add_view(DepartmentsAutocomplete, route_name='departments.autocomplete',
renderer='json', permission='departments.list') renderer='json', permission='departments.list')
# departments by vendor list
config.add_route('departments.by_vendor', '/departments/by-vendor')
config.add_view(DepartmentsByVendorGrid,route_name='departments.by_vendor',
permission='departments.list')
DepartmentsView.defaults(config) DepartmentsView.defaults(config)

View file

@ -1339,11 +1339,18 @@ class MasterView(View):
""" """
factory = self.get_grid_factory() factory = self.get_grid_factory()
key = self.get_grid_key() key = self.get_grid_key()
data = self.get_data(session=kwargs.get('session')) if 'data' in kwargs:
data = kwargs.pop('data')
else:
data = self.get_data(session=kwargs.get('session'))
configure = kwargs.pop('configure', None)
kwargs = self.make_grid_kwargs(**kwargs) kwargs = self.make_grid_kwargs(**kwargs)
grid = factory(key, self.request, data=data, model_class=self.get_model_class(error=False), **kwargs) grid = factory(key, self.request, data=data, model_class=self.get_model_class(error=False), **kwargs)
self._preconfigure_grid(grid) if configure:
self.configure_grid(grid) configure(grid)
else:
self._preconfigure_grid(grid)
self.configure_grid(grid)
grid.load_settings() grid.load_settings()
return grid return grid